當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Python unittest.IsolatedAsyncioTestCase用法及代碼示例


用法:

class unittest.IsolatedAsyncioTestCase(methodName='runTest')

此類提供類似於TestCase 的 API,並且還接受協程作為測試函數。

3.8 版中的新函數。

說明順序的示例:

from unittest import IsolatedAsyncioTestCase

events = []


class Test(IsolatedAsyncioTestCase):


    def setUp(self):
        events.append("setUp")

    async def asyncSetUp(self):
        self._async_connection = await AsyncConnection()
        events.append("asyncSetUp")

    async def test_response(self):
        events.append("test_response")
        response = await self._async_connection.get("https://example.com")
        self.assertEqual(response.status_code, 200)
        self.addAsyncCleanup(self.on_cleanup)

    def tearDown(self):
        events.append("tearDown")

    async def asyncTearDown(self):
        await self._async_connection.close()
        events.append("asyncTearDown")

    async def on_cleanup(self):
        events.append("cleanup")

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

運行測試後,events 將包含 ["setUp", "asyncSetUp", "test_response", "asyncTearDown", "tearDown", "cleanup"]

相關用法


注:本文由純淨天空篩選整理自python.org大神的英文原創作品 unittest.IsolatedAsyncioTestCase。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。