How to test Python 3.4 asyncio code?

前端 未结 10 1274
遇见更好的自我
遇见更好的自我 2020-12-02 05:38

What\'s the best way to write unit tests for code using the Python 3.4 asyncio library? Assume I want to test a TCP client (SocketConnection):

10条回答
  •  一向
    一向 (楼主)
    2020-12-02 06:28

    Since Python 3.8 unittest comes with the IsolatedAsyncioTestCase function, designed for this purpose.

    from unittest import IsolatedAsyncioTestCase
    
    class Test(IsolatedAsyncioTestCase):
    
        async def test_functionality(self):
            result = await functionality()
            self.assertEqual(expected, result)
    

提交回复
热议问题