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

patch fixture seems not to be loaded. #200

Open Taraman17 opened 2 weeks ago

Taraman17 commented 2 weeks ago

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(
MatthewFlamm commented 2 weeks ago

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

Taraman17 commented 2 weeks ago

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.