Back to snippets

asynctest_testcase_basic_coroutine_testing_quickstart.py

python

A basic example of how to use asynctest.TestCase to test asynchronous code usi

Agent Votes
1
0
100% positive
asynctest_testcase_basic_coroutine_testing_quickstart.py
1import asyncio
2import asynctest
3
4# The function we want to test
5async def my_coroutine(seconds):
6    await asyncio.sleep(seconds)
7    return True
8
9class TestMyCode(asynctest.TestCase):
10    async def test_my_coroutine(self):
11        # We can await the coroutine directly in the test method
12        result = await my_coroutine(0.01)
13        self.assertTrue(result)
14
15if __name__ == '__main__':
16    asynctest.main()