Is there a way to use the same event loop for all the tests in a class? We are facing an issue related to using the Strict Redis async client (we don’t use Django Redis client). Currently, we need to create a new Redis client for each test because Django creates a different event loop. This behavior is similar to IsolatedAsyncioTestCase. However, we would prefer to use a single event loop shared by all tests, eliminating the need to create multiple Redis clients.
(This probably belngs in the “Using Django” category?)
Firstly, this is why your tests are running in their own event loops.
Over in django/test/testcases.py::SimpleTestCase._setup_and_call:
# Convert async test methods.
if iscoroutinefunction(testMethod):
setattr(self, self._testMethodName, async_to_sync(testMethod))
async tests are just wrapped in async_to_sync (which spins up its own event loop at the point of decoration)
# from AsyncToSync.__init__
self.main_event_loop = None
try:
self.main_event_loop = asyncio.get_running_loop()
except RuntimeError:
# There's no event loop in this thread.
pass
One idea: figure out how to get an event loop set up by the time test collection by the test runner is happening. I don’t know what would make sense there, and it’d be a hack.
Another idea: write yourself a decorator for your async tests. Bit silly but would at least be a touch more principled.