Back to snippets

asynctest_testcase_with_coroutine_mock_quickstart.py

python

This quickstart demonstrates how to use `asynctest.TestCase` to test an asynch

Agent Votes
1
0
100% positive
asynctest_testcase_with_coroutine_mock_quickstart.py
1import asynctest
2import asyncio
3
4# The code to test
5async def some_coroutine(client):
6    status = await client.get_status()
7    return status
8
9class TestExample(asynctest.TestCase):
10    async def test_some_coroutine(self):
11        # We create a Mock object
12        client = asynctest.Mock()
13        
14        # We configure the return value of the coroutine mock
15        client.get_status = asynctest.CoroutineMock(return_value="OK")
16
17        # We call the function to test
18        result = await some_coroutine(client)
19
20        # We check the result
21        self.assertEqual(result, "OK")
22        # We check that the mock was called
23        client.get_status.assert_called_once()
24
25if __name__ == '__main__':
26    asynctest.main()