Skip to content

Commit c576a68

Browse files
authored
Upgrade pytest-aiohttp (#82475)
* Upgrade pytest-aiohttp * Make sure executors, tasks and timers are closed Some test will trigger warnings on garbage collect, these warnings spills over into next test. Some test trigger tasks that raise errors on shutdown, these spill over into next test. This is to mimic older pytest-aiohttp and it's behaviour on test cleanup. Discussions on similar changes for pytest-aiohttp are here: pytest-dev/pytest-asyncio#309 * Replace loop with event_loop * Make sure time is frozen for tests * Make sure the ConditionType is not async /home-assistant/homeassistant/helpers/template.py:2082: RuntimeWarning: coroutine 'AsyncMockMixin._execute_mock_call' was never awaited def wrapper(*args, **kwargs): Enable tracemalloc to get traceback where the object was allocated. See https://docs.pytest.org/en/stable/how-to/capture-warnings.html#resource-warnings for more info. * Increase litejet press tests with a factor 10 The times are simulated anyway, and we can't stop the normal event from occuring. * Use async handlers for aiohttp tests/components/motioneye/test_camera.py::test_get_still_image_from_camera tests/components/motioneye/test_camera.py::test_get_still_image_from_camera tests/components/motioneye/test_camera.py::test_get_stream_from_camera tests/components/motioneye/test_camera.py::test_get_stream_from_camera tests/components/motioneye/test_camera.py::test_camera_option_stream_url_template tests/components/motioneye/test_camera.py::test_camera_option_stream_url_template /Users/joakim/src/hass/home-assistant/venv/lib/python3.9/site-packages/aiohttp/web_urldispatcher.py:189: DeprecationWarning: Bare functions are deprecated, use async ones warnings.warn( * Switch to freezegun in modbus tests The tests allowed clock to tick in between steps * Make sure skybell object are fully mocked Old tests would trigger attempts to post to could services: ``` DEBUG:aioskybell:HTTP post https://cloud.myskybell.com/api/v3/login/ Request with headers: {'content-type': 'application/json', 'accept': '*/*', 'x-skybell-app-id': 'd2b542c7-a7e4-4e1e-b77d-2b76911c7c46', 'x-skybell-client-id': '1f36a3c0-6dee-4997-a6db-4e1c67338e57'} ``` * Fix sorting that broke after rebase
1 parent b7652c7 commit c576a68

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+263
-226
lines changed

homeassistant/package_constraints.txt

-3
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,6 @@ hyperframe>=5.2.0
9898
# Ensure we run compatible with musllinux build env
9999
numpy==1.23.2
100100

101-
# pytest_asyncio breaks our test suite. We rely on pytest-aiohttp instead
102-
pytest_asyncio==1000000000.0.0
103-
104101
# Prevent dependency conflicts between sisyphus-control and aioambient
105102
# until upper bounds for sisyphus-control have been updated
106103
# https://github.com/jkeljo/sisyphus-control/issues/6

pyproject.toml

+1
Original file line numberDiff line numberDiff line change
@@ -227,3 +227,4 @@ norecursedirs = [
227227
]
228228
log_format = "%(asctime)s.%(msecs)03d %(levelname)-8s %(threadName)s %(name)s:%(filename)s:%(lineno)s %(message)s"
229229
log_date_format = "%Y-%m-%d %H:%M:%S"
230+
asyncio_mode = "auto"

requirements_test.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ mypy==0.991
1616
pre-commit==2.20.0
1717
pylint==2.15.6
1818
pipdeptree==2.3.1
19-
pytest-aiohttp==0.3.0
19+
pytest-asyncio==0.20.2
20+
pytest-aiohttp==1.0.4
2021
pytest-cov==3.0.0
2122
pytest-freezegun==0.4.2
2223
pytest-socket==0.5.1

script/gen_requirements_all.py

-3
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,6 @@
110110
# Ensure we run compatible with musllinux build env
111111
numpy==1.23.2
112112
113-
# pytest_asyncio breaks our test suite. We rely on pytest-aiohttp instead
114-
pytest_asyncio==1000000000.0.0
115-
116113
# Prevent dependency conflicts between sisyphus-control and aioambient
117114
# until upper bounds for sisyphus-control have been updated
118115
# https://github.com/jkeljo/sisyphus-control/issues/6

tests/auth/test_init.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828

2929

3030
@pytest.fixture
31-
def mock_hass(loop):
31+
def mock_hass(event_loop):
3232
"""Home Assistant mock with minimum amount of data set to make it work with auth."""
3333
hass = Mock()
3434
hass.config.skip_pip = True

tests/common.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ def stop_hass():
160160

161161

162162
# pylint: disable=protected-access
163-
async def async_test_home_assistant(loop, load_registries=True):
163+
async def async_test_home_assistant(event_loop, load_registries=True):
164164
"""Return a Home Assistant object pointing at test config dir."""
165165
hass = ha.HomeAssistant()
166166
store = auth_store.AuthStore(hass)

tests/components/alexa/test_flash_briefings.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@
2121

2222

2323
@pytest.fixture
24-
def alexa_client(loop, hass, hass_client):
24+
def alexa_client(event_loop, hass, hass_client):
2525
"""Initialize a Home Assistant server for testing this module."""
26+
loop = event_loop
2627

2728
@callback
2829
def mock_service(call):

tests/components/alexa/test_intent.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@
2727

2828

2929
@pytest.fixture
30-
def alexa_client(loop, hass, hass_client):
30+
def alexa_client(event_loop, hass, hass_client):
3131
"""Initialize a Home Assistant server for testing this module."""
32+
loop = event_loop
3233

3334
@callback
3435
def mock_service(call):

tests/components/auth/conftest.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33

44

55
@pytest.fixture
6-
def aiohttp_client(loop, aiohttp_client, socket_enabled):
6+
def aiohttp_client(event_loop, aiohttp_client, socket_enabled):
77
"""Return aiohttp_client and allow opening sockets."""
88
return aiohttp_client

tests/components/config/test_init.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from homeassistant.setup import async_setup_component
33

44

5-
async def test_config_setup(hass, loop):
5+
async def test_config_setup(hass, event_loop):
66
"""Test it sets up hassbian."""
77
await async_setup_component(hass, "config", {})
88
assert "config" in hass.config.components

tests/components/emulated_hue/test_upnp.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def sendto(self, response, addr):
3131

3232

3333
@pytest.fixture
34-
def aiohttp_client(loop, aiohttp_client, socket_enabled):
34+
def aiohttp_client(event_loop, aiohttp_client, socket_enabled):
3535
"""Return aiohttp_client and allow opening sockets."""
3636
return aiohttp_client
3737

tests/components/emulated_roku/test_binding.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ async def test_events_fired_properly(hass):
2525
roku_event_handler = None
2626

2727
def instantiate(
28-
loop,
28+
event_loop,
2929
handler,
3030
roku_usn,
3131
host_ip,

tests/components/energy/test_sensor.py

+8
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from datetime import timedelta
44
from unittest.mock import patch
55

6+
from freezegun import freeze_time
67
import pytest
78

89
from homeassistant.components.energy import data
@@ -41,6 +42,13 @@ async def setup_integration(hass):
4142
return setup_integration
4243

4344

45+
@pytest.fixture(autouse=True)
46+
@freeze_time("2022-04-19 07:53:05")
47+
def frozen_time():
48+
"""Freeze clock for tests."""
49+
yield
50+
51+
4452
def get_statistics_for_entity(statistics_results, entity_id):
4553
"""Get statistics for a certain entity, or None if there is none."""
4654
for statistics_result in statistics_results:

tests/components/fido/test_sensor.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ async def fetch_data(self):
3838
raise PyFidoError("Fake Error")
3939

4040

41-
async def test_fido_sensor(loop, hass):
41+
async def test_fido_sensor(event_loop, hass):
4242
"""Test the Fido number sensor."""
4343
with patch("homeassistant.components.fido.sensor.FidoClient", new=FidoClientMock):
4444
config = {

tests/components/frontend/test_init.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ async def frontend_themes(hass):
8080

8181

8282
@pytest.fixture
83-
def aiohttp_client(loop, aiohttp_client, socket_enabled):
83+
def aiohttp_client(event_loop, aiohttp_client, socket_enabled):
8484
"""Return aiohttp_client and allow opening sockets."""
8585
return aiohttp_client
8686

tests/components/geofency/test_init.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ def mock_dev_track(mock_device_tracker_conf):
117117

118118

119119
@pytest.fixture
120-
async def geofency_client(loop, hass, hass_client_no_auth):
120+
async def geofency_client(event_loop, hass, hass_client_no_auth):
121121
"""Geofency mock client (unauthenticated)."""
122122

123123
assert await async_setup_component(
@@ -130,7 +130,7 @@ async def geofency_client(loop, hass, hass_client_no_auth):
130130

131131

132132
@pytest.fixture(autouse=True)
133-
async def setup_zones(loop, hass):
133+
async def setup_zones(event_loop, hass):
134134
"""Set up Zone config in HA."""
135135
assert await async_setup_component(
136136
hass,

tests/components/google_assistant/test_google_assistant.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,9 @@ def auth_header(hass_access_token):
4242

4343

4444
@pytest.fixture
45-
def assistant_client(loop, hass, hass_client_no_auth):
45+
def assistant_client(event_loop, hass, hass_client_no_auth):
4646
"""Create web client for the Google Assistant API."""
47+
loop = event_loop
4748
loop.run_until_complete(
4849
setup.async_setup_component(
4950
hass,
@@ -66,8 +67,10 @@ def assistant_client(loop, hass, hass_client_no_auth):
6667

6768

6869
@pytest.fixture
69-
def hass_fixture(loop, hass):
70+
def hass_fixture(event_loop, hass):
7071
"""Set up a Home Assistant instance for these tests."""
72+
loop = event_loop
73+
7174
# We need to do this to get access to homeassistant/turn_(on,off)
7275
loop.run_until_complete(setup.async_setup_component(hass, core.DOMAIN, {}))
7376

tests/components/gpslogger/test_init.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def mock_dev_track(mock_device_tracker_conf):
2626

2727

2828
@pytest.fixture
29-
async def gpslogger_client(loop, hass, hass_client_no_auth):
29+
async def gpslogger_client(event_loop, hass, hass_client_no_auth):
3030
"""Mock client for GPSLogger (unauthenticated)."""
3131

3232
assert await async_setup_component(hass, DOMAIN, {DOMAIN: {}})
@@ -38,7 +38,7 @@ async def gpslogger_client(loop, hass, hass_client_no_auth):
3838

3939

4040
@pytest.fixture(autouse=True)
41-
async def setup_zones(loop, hass):
41+
async def setup_zones(event_loop, hass):
4242
"""Set up Zone config in HA."""
4343
assert await async_setup_component(
4444
hass,

tests/components/homekit/conftest.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def iid_storage(hass):
2121

2222

2323
@pytest.fixture()
24-
def run_driver(hass, loop, iid_storage):
24+
def run_driver(hass, event_loop, iid_storage):
2525
"""Return a custom AccessoryDriver instance for HomeKit accessory init.
2626
2727
This mock does not mock async_stop, so the driver will not be stopped
@@ -41,12 +41,12 @@ def run_driver(hass, loop, iid_storage):
4141
bridge_name=BRIDGE_NAME,
4242
iid_storage=iid_storage,
4343
address="127.0.0.1",
44-
loop=loop,
44+
loop=event_loop,
4545
)
4646

4747

4848
@pytest.fixture
49-
def hk_driver(hass, loop, iid_storage):
49+
def hk_driver(hass, event_loop, iid_storage):
5050
"""Return a custom AccessoryDriver instance for HomeKit accessory init."""
5151
with patch("pyhap.accessory_driver.AsyncZeroconf"), patch(
5252
"pyhap.accessory_driver.AccessoryEncoder"
@@ -65,12 +65,12 @@ def hk_driver(hass, loop, iid_storage):
6565
bridge_name=BRIDGE_NAME,
6666
iid_storage=iid_storage,
6767
address="127.0.0.1",
68-
loop=loop,
68+
loop=event_loop,
6969
)
7070

7171

7272
@pytest.fixture
73-
def mock_hap(hass, loop, iid_storage, mock_zeroconf):
73+
def mock_hap(hass, event_loop, iid_storage, mock_zeroconf):
7474
"""Return a custom AccessoryDriver instance for HomeKit accessory init."""
7575
with patch("pyhap.accessory_driver.AsyncZeroconf"), patch(
7676
"pyhap.accessory_driver.AccessoryEncoder"
@@ -93,7 +93,7 @@ def mock_hap(hass, loop, iid_storage, mock_zeroconf):
9393
bridge_name=BRIDGE_NAME,
9494
iid_storage=iid_storage,
9595
address="127.0.0.1",
96-
loop=loop,
96+
loop=event_loop,
9797
)
9898

9999

tests/components/http/conftest.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33

44

55
@pytest.fixture
6-
def aiohttp_client(loop, aiohttp_client, socket_enabled):
6+
def aiohttp_client(event_loop, aiohttp_client, socket_enabled):
77
"""Return aiohttp_client and allow opening sockets."""
88
return aiohttp_client

tests/components/http/test_cors.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,12 @@ async def mock_handler(request):
4949

5050

5151
@pytest.fixture
52-
def client(loop, aiohttp_client):
52+
def client(event_loop, aiohttp_client):
5353
"""Fixture to set up a web.Application."""
5454
app = web.Application()
5555
setup_cors(app, [TRUSTED_ORIGIN])
5656
app["allow_configured_cors"](app.router.add_get("/", mock_handler))
57-
return loop.run_until_complete(aiohttp_client(app))
57+
return event_loop.run_until_complete(aiohttp_client(app))
5858

5959

6060
async def test_cors_requests(client):

tests/components/image_processing/test_init.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616

1717
@pytest.fixture
18-
def aiohttp_unused_port(loop, aiohttp_unused_port, socket_enabled):
18+
def aiohttp_unused_port(event_loop, aiohttp_unused_port, socket_enabled):
1919
"""Return aiohttp_unused_port and allow opening sockets."""
2020
return aiohttp_unused_port
2121

0 commit comments

Comments
 (0)