pytest-homeassistant-custom-component icon indicating copy to clipboard operation
pytest-homeassistant-custom-component copied to clipboard

patch fixture seems not to be loaded.

Open Taraman17 opened this issue 1 year ago • 2 comments
trafficstars

I get an error in my tests, that worked up to last week.

What I get in the end is

pytest_socket.SocketBlockedError: A test tried to use socket.socket.

but digging deeper, this is not the problem here. It originates from a library function, that should not be called at all, because it is patched in conftest.py:

@pytest.fixture
def mock_homee() -> Generator[MagicMock]:
    """Return a mock Homee instance."""
    with patch(
        "custom_components.homee.config_flow.validate_and_connect"
    ) as mocked_homee:
        homee = mocked_homee.return_value
...

And that is passed into the test function:

async def test_flow(hass: HomeAssistant, mock_homee: MagicMock)

So my assumption is, that this patched function is not used in the test, but the original is called, which causes the try to connect via a socket. Also from the log:

File "/home/vscode/.local/ha-venv/lib/python3.12/site-packages/pymee/__init__.py", line 145, in run
    |     await self.get_access_token()
    |   File "/home/vscode/.local/ha-venv/lib/python3.12/site-packages/pymee/__init__.py", line 95, in get_access_token
    |     req = await client.post(

Taraman17 avatar Sep 23 '24 12:09 Taraman17

So my assumption is, that this patched function is not used in the test, but the original is called, which causes the try to connect via a socket.

It is possible this plugin has weirdness with sockets because of the difference in plugin ordering, for example. But this sentence above makes me think that you have a problem in how you use patch, which is not part of this package.

See also #154

MatthewFlamm avatar Sep 23 '24 13:09 MatthewFlamm

It might be the way that I use patch - since I'm quite new to pytest, I usually look for the error between my ears first. But as I said, it was working about a week ago with the exact same code, so I must assume something changed in HA or the DevConatiner that affects the workings of this plugin.

Taraman17 avatar Sep 23 '24 15:09 Taraman17

Meanwhile I tried everything to get it working and it doesn't. I still have the impression I'm trying to fix an error which isn't mine.

#154 does not quite match, since I don't intend to use sockets - the respective function is the one I want to bypass by patching it. I can access the mocked Object within the test, so it is loaded - it just doesn't replace the patched function.

I also tried to alter the function name and then get an error, that the function is not present - so it correctly checks for it in the tested code, but again - the test still calls the original function.

I checked the patch code several times and I'm still convinced it is correct.

I wonder if no one else has similar problems here...

Taraman17 avatar Oct 18 '24 16:10 Taraman17

I'm also not convinced based on the snipped tracebacks that you aren't calling the library after the config flow finishes. It could be that you are mocking this out correctly, but you need to mock additional things. Did you mock out the component setup in the config flow test, which is best practice, for example?

MatthewFlamm avatar Oct 18 '24 16:10 MatthewFlamm

That seemed to be the right hint. I additionally mocked "async_setup_entry" and with that the test passes. I still wonder, why it worked before though...

Anyway, sorry for taking your time, but thank you for pointing me in the right direction!!!

Taraman17 avatar Oct 19 '24 10:10 Taraman17

Glad it worked out.

MatthewFlamm avatar Oct 19 '24 12:10 MatthewFlamm