Python 3.x 为了什么';pytest.mark.asyncio';使用了什么?
我不明白decoratorPython 3.x 为了什么';pytest.mark.asyncio';使用了什么?,python-3.x,pytest,pytest-asyncio,Python 3.x,Pytest,Pytest Asyncio,我不明白decorator@pytest.mark.asyncio可以用于什么目的 我尝试在安装了pytest和pytest asyncio插件的情况下运行以下代码段,但失败了,因此我得出结论,pytest在不使用decorator的情况下收集测试协程。它为什么如此存在 async def test_div(): 返回1/0 当您的测试用@pytest.mark.asyncio标记时,它们与正文中的关键字wait一起变为 pytest将使用event\u loopfixture提供的事件循环作为
@pytest.mark.asyncio可以用于什么目的
我尝试在安装了pytest
和pytest asyncio
插件的情况下运行以下代码段,但失败了,因此我得出结论,pytest在不使用decorator的情况下收集测试协程。它为什么如此存在
async def test_div():
返回1/0
当您的测试用@pytest.mark.asyncio
标记时,它们与正文中的关键字wait
一起变为
pytest
将使用event\u loop
fixture提供的事件循环作为异步IO任务执行它:
这段代码包含decorator
@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()
非常感谢。但是如果我省略了decorator,会有什么不同呢?@EgorOsokin:比较两个版本,它们是等价的,使用decorator比使用事件循环更容易