我使用pytest-asyncio版本0.10.0运行了一个测试,用于验证异步响应是否抛出了异常。
代码基本上是:
class TestThis:
@pytest.mark.asyncio
def test_the_thing(self):
arg1 = "cmd"
arg2 = "second command"
with pytest.raises(CustomException):
await do_thing(arg1, arg2)现在真正奇怪的事情是,如果我单独运行它,或者如果我单独运行类,这个测试就可以正常工作。但是,当我运行所有测试(项目根目录下的pytest)时,每次都会失败,并出现运行时错误,说明循环已关闭。
发布于 2021-04-29 03:52:18
https://pypi.org/project/pytest-asyncio/
显然,您可以覆盖pytest-asyncio版本的事件循环fixture。它们是这样的:
@pytest.fixture
def event_loop():
loop = asyncio.get_event_loop()
yield loop
loop.close()我把它写成这样:
@pytest.fixture
def event_loop():
loop = asyncio.get_event_loop()
yield loop
cleanly_shutdown(loop)或者在其他类似的情况下:
@pytest.fixture
def event_loop():
yield asyncio.get_event_loop()
def pytest_sessionfinish(session, exitstatus):
asyncio.get_event_loop().close()https://stackoverflow.com/questions/61022713
复制相似问题