How to run all tests in a module in the same event loop

All tests can be run inside the same event loop by marking them with pytest.mark.asyncio(scope="module"). This is easily achieved by adding a pytestmark statement to your module.

import asyncio

import pytest

pytestmark = pytest.mark.asyncio(scope="module")

loop: asyncio.AbstractEventLoop


async def test_remember_loop():
    global loop
    loop = asyncio.get_running_loop()


async def test_assert_same_loop():
    global loop
    assert asyncio.get_running_loop() is loop