Skip to content

Commit 42efe77

Browse files
author
Jeny Sadadia
committed
tests/unit_tests: fix tests broken by fastapi upgrade
The `fastapi` version upgrade affected unit tests as `TestClient` has been upgraded to use `httpx`. Reference to the release note: https://fastapi.tiangolo.com/release-notes/#0870 This introduced the below failure in the API unit tests: ``` > raise RuntimeError('Event loop is closed') E RuntimeError: Event loop is closed /usr/local/lib/python3.11/asyncio/base_events.py:520: RuntimeError ``` Based on the observation, it seems like somehow `TestClient` is closing event loop after executing the first test. It is backed by the GH issue: encode/starlette#2069 Fix the issue by using the same test client for all the synchronous tests. Fixture `event_loop` is not required anymore after changing `TestClient` scope to `session`. Hence, drop it. Signed-off-by: Jeny Sadadia <jeny.sadadia@collabora.com>
1 parent 86ae579 commit 42efe77

File tree

1 file changed

+2
-17
lines changed

1 file changed

+2
-17
lines changed

tests/unit_tests/conftest.py

+2-17
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
"""pytest fixtures for KernelCI API"""
1212

1313
from unittest.mock import AsyncMock
14-
import asyncio
1514
import fakeredis.aioredis
1615
from fastapi.testclient import TestClient
1716
from fastapi import Request, HTTPException, status
@@ -96,12 +95,12 @@ def mock_get_current_admin_user(request: Request):
9695
app.dependency_overrides[get_current_superuser] = mock_get_current_admin_user
9796

9897

99-
@pytest.fixture
98+
@pytest.fixture(scope='session')
10099
def test_client():
101100
"""Fixture to get FastAPI Test client instance"""
102101
# Mock dependency callables for getting current user
103102
with TestClient(app=versioned_app, base_url=BASE_URL) as client:
104-
return client
103+
yield client
105104

106105

107106
@pytest.fixture
@@ -113,20 +112,6 @@ async def test_async_client():
113112
await versioned_app.router.shutdown()
114113

115114

116-
@pytest.fixture
117-
def event_loop():
118-
"""Create an instance of the default event loop for each test case.
119-
This is a workaround to prevent the default event loop to be closed by
120-
async pubsub tests. It was causing other tests unable to run.
121-
The issue has already been reported here:
122-
https://github.com/pytest-dev/pytest-asyncio/issues/371
123-
"""
124-
loop = asyncio.new_event_loop()
125-
asyncio.set_event_loop(loop)
126-
yield loop
127-
loop.close()
128-
129-
130115
@pytest.fixture
131116
def mock_db_create(mocker):
132117
"""Mocks async call to Database class method used to create object"""

0 commit comments

Comments
 (0)