1

I need use IsolatedAsyncioTestCase to Perform asynchronous verification why my code does not run in parallel or asynchronous, i need test2 、test3、test4 run in asynchronous

async def check_status(name:str):
    print(f"before {name}")
    await asyncio.sleep(5)
    print(f"after {name}")
    return True


class TestAsync(IsolatedAsyncioTestCase):

    async def test_002(self):
        print("test_002")
        res = await check_status("test_002")
        self.assertTrue(res)

    async def test_003(self):
        print("test_003")
        res = await check_status("test_003")
        self.assertTrue(res)

    async def test_004(self):
        print("test_004")
        res = await check_status("test_004")
        self.assertTrue(res)


if __name__ == "__main__":
    unittest.main()

here is the output, still synchronized

test_002
before test_002
after test_002
test_003
before test_003
after test_003
test_004
before test_004
after test_004
fox
  • 11
  • 2
  • 1
    `IsolatedAsyncioTestCase` allows you to await within your tests, but it still runs them one at a time. In general you don’t want one test to be able to affect another, so running multiple tests at a time like that isn’t encouraged. If you want to parallelize your tests, you should look into something like [pytest-xdist](https://pypi.org/p/pytest-xdist). – dirn Jan 06 '22 at 13:45
  • @fox, you are confusing parallel and asynchronous. Next posts should help you https://medium.com/plain-and-simple/synchronous-vs-asynchronous-vs-concurrent-vs-parallel-4342bfb8b9f2 or https://stackoverflow.com/questions/6133574/how-to-articulate-the-difference-between-asynchronous-and-parallel-programming As mentioned above `IsolatedAsyncioTestCase` allows you to call awaitables. You can't use `wait check_status` in tests without `async def test_002`. So to have async tests you should use `IsolatedAsyncioTestCase` instead `TestCase`. – Aleksei Semidotskii Mar 22 '22 at 19:25

0 Answers0