MatthewFlamm / pytest-homeassistant-custom-component

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

Starting with 0.13.24, get AttributeError on hass.data #161

Closed craigbarratt closed 1 year ago

craigbarratt commented 1 year ago

pyscript is a custom integration and uses pytest-homeassistant-custom-component for all its tests.

Starting with 0.13.24 (which upgrades to homeassistant==2023.5.0b4 and I'm using python 3.11.3) all my tests get this error:

hass = <async_generator object _hass at 0x1084d7ba0>

    @pytest.fixture
    def enable_custom_integrations(hass: HomeAssistant) -> None:
        """Enable custom integrations defined in the test dir."""
>       hass.data.pop(loader.DATA_CUSTOM_COMPONENTS)
E       AttributeError: 'async_generator' object has no attribute 'data'

venv/lib/python3.11/site-packages/pytest_homeassistant_custom_component/plugins.py:1145: AttributeError

The 086baf1 commit appears to have changed how the hass object is created.

Here's how a typical test is defined:

@pytest.mark.asyncio
async def test_eval(hass):
    """Test interpreter."""
    hass.data[DOMAIN] = {CONFIG_ENTRY: MockConfigEntry(domain=DOMAIN, data={CONF_ALLOW_ALL_IMPORTS: True})}
    ...

What am I missing to make my tests work again?

craigbarratt commented 1 year ago

Oops, duplicate of #158. Let me review that for the solution.

craigbarratt commented 1 year ago

Confirmed that pytest --asyncio-mode=auto solves it for me too. I would still like to understand why that is necessary - why does the 086baf1 commit now mean that --asyncio-mode=auto is required? I believe I have the right decorators on all my async test functions.

MatthewFlamm commented 1 year ago

As with anything async, it is a bit mysterious (at least to me) since it is one more layer obfuscated. It starts around #141 when pytest-asyncio version was bumped, and then I think there were later changes in the core fixtures that caused this error to pop up as the symptom.

I'm not sure about this, but pytest-asyncio might have different behavior for fixtures depending on the mode (or decorations on the fixtures themselves). Note that downstream users do not mark the fixtures in this package, which are more or less directly extracted from homeassistant/core. I believe homeassistant/core also uses the auto mode in configuration. My thinking is that you actually need to use the auto mode to get the async fixtures to behave properly even if the fixtures come from a different package. I'm not sure there is an easy way to mark them here.

craigbarratt commented 1 year ago

Thanks for the explanation. I definitely don't understand any of the test harness magic (so many layers!), but using pytest --asyncio-mode=auto seems like a decent solution.

Thanks for your work on pytest-homeassistant-custom-component, without which pyscript would have no tests.