MatthewFlamm / pytest-homeassistant-custom-component

Package to automatically extract testing plugins from Home Assistant for custom component testing
MIT License
67 stars 10 forks source link

Plugin loading order with Syrupy #198

Open wbyoung opened 1 month ago

wbyoung commented 1 month ago

I have encountered a situation where tests pass locally and fail in CI because the snapshots generated by Syrupy can't be found. I've found a solution to this issue which is to run the tests as pytest -p syrupy (or adding it to addopts under [tool:pytest] in my config).

I'm guessing that if Syrupy is loaded second, that the options to configure the directory override what's setup by pytest_homeassistant_custom_component, but I can't be sure.

I don't know if there's a way to configure this to say it must be loaded after Syrupy, but that would probably be good. If not, maybe it may be useful to document it.

MatthewFlamm commented 1 month ago

I'm guessing that if Syrupy is loaded second, that the options to configure the directory override what's setup by pytest_homeassistant_custom_component, but I can't be sure.

This seems very plausible, see

https://github.com/home-assistant/core/blob/1f488b00f85003a17752b5c15f86495c029bc382/tests/conftest.py#L1856-L1859

where homeassistant uses the same name of the fixture as syrupy itself. Given that this package is a plugin whereas the homeassistant tests conftest.py is not a plugin, this could explain the difference why homeassistant CI does not have this problem. It would be ideal if we could fix it in this package somehow, but I'm not sure it is possible.

Your fix is mentioned in this thread https://github.com/pytest-dev/pytest/issues/935, which still seems unresolved. I'm not sure we can do anything about it other than putting it in the README for now.

wbyoung commented 1 month ago

Yup, I was reading that thread earlier. Your assessment seems in line with the (little) I know about this. Putting it in the Readme seems like a good resolution.

zxdavb commented 1 day ago

I have encountered a situation where tests pass locally and fail in CI because the snapshots generated by Syrupy can't be found.

I am getting the same symptom, but also it fails because the custom serializer is not used (e.g. timestamps are not set to <ANY>).

The workaround that works for me is to duplicate the snapshot fixture in my CC's conftest.py:

from pytest_homeassistant_custom_component.syrupy import HomeAssistantSnapshotExtension
from syrupy.assertion import SnapshotAssertion

   ...

@pytest.fixture
def snapshot(snapshot: SnapshotAssertion) -> SnapshotAssertion:
    """Return snapshot assertion fixture with the Home Assistant extension."""
    return snapshot.use_extension(HomeAssistantSnapshotExtension)