unitestisolatedAsyncioTestCase不起作用

问题描述 投票:0回答:1

我需要使用IsolatedAsyncioTestCase来执行异步验证 为什么我的代码不并行或异步运行,我需要 test2 、test3、test4 异步运行

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()

这是输出,仍然同步

test_002
before test_002
after test_002
test_003
before test_003
after test_003
test_004
before test_004
after test_004
python unit-testing python-asyncio
1个回答
0
投票
import unittest

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


class TestAsync(unittest.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()
© www.soinside.com 2019 - 2024. All rights reserved.