pytestpython-asynciopytest-asyncio

pytest-asyncio has a closed event loop, but only when running all tests


I have a test to verify an exception is thrown from an async response, which I'm using pytest-asyncio version 0.10.0 to run.

Code is basically:

class TestThis:
    @pytest.mark.asyncio
    def test_the_thing(self):
       arg1 = "cmd"
       arg2 = "second command"
       with pytest.raises(CustomException):
           await do_thing(arg1, arg2)

Now the really weird thing is this test works fine if I run it alone, or if I run the class alone. However when I run all tests (pytest at the project root), it fails every time with a runtime error, saying the loop is closed.


Solution

  • There is an update available after the pytest-asyncio>=0.23.0 release.

    After that we can have multiple asyncio tests.

    import pytest
    
    @pytest.mark.asyncio(scope="session")
    async def test_1():
        ...
    
    @pytest.mark.asyncio(scope="session")
    async def test_2():
        ...