0

我想测试一个调用底层异步方法的数据类。这是一个不起作用的小测试示例。我已经看到您还可以模拟数据类类,如此处所述:https : //stackoverflow.com/a/51641747/1998288 ,但这不能与 asyncio 结合使用。

下面是一个不起作用的最小示例。

import pytest

import asynctest
import asyncio
from dataclasses import dataclass

async def long_async_func():
    asyncio.wait(10)
    return 10

@dataclass
class Example:
    a: str
    b: str

    def __post_init__(self):
        self.c = self.a + self.b

    async def start(self):
        self.c = "started"
        res = await long_async_func()
        self.c = f"finished {res}"




@pytest.mark.asyncio
async def test_start():

    with asynctest.patch('__main__.long_async_func') as async_mock:
        async_mock.side_effect = TimeoutError
        ex = Example("a", "b")
        assert ex.c == "ab"
        await ex.start()


4

0 回答 0