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

All tests can be run inside the same event loop by marking them with pytest.mark.asyncio(scope="class"). This is easily achieved by using the asyncio marker as a class decorator.

import asyncio

import pytest


@pytest.mark.asyncio(scope="class")
class TestInOneEventLoopPerClass:
    loop: asyncio.AbstractEventLoop

    async def test_remember_loop(self):
        TestInOneEventLoopPerClass.loop = asyncio.get_running_loop()

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