我不明白装饰器的用途 @pytest.mark.asyncio
可以使用。
我尝试使用 pytest
运行以下代码片段和 pytest-asyncio
插件安装但失败了,所以我得出结论,pytest 在没有装饰器的情况下收集测试协程。为什么会这样存在?
async def test_div():
return 1 / 0
最佳答案
当您的测试标记为 @pytest.mark.asyncio
时,他们变成 coroutines , 连同关键字 await
在体内pytest
将使用 event_loop
提供的事件循环将其作为异步任务执行 fixture :
这段代码带有装饰器
@pytest.mark.asyncio
async def test_example(event_loop):
do_stuff()
await asyncio.sleep(0.1, loop=event_loop)
等于写这个:
def test_example():
loop = asyncio.new_event_loop()
try:
do_stuff()
asyncio.set_event_loop(loop)
loop.run_until_complete(asyncio.sleep(0.1, loop=loop))
finally:
loop.close()
关于python-3.x - 'pytest.mark.asyncio' 用于什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57461868/