Py.Test 为所有测试添加标记

Nic*_*ich 5 pytest python-3.x python-asyncio

我将 python asyncio 与 pytest-aysncio 一起使用,这意味着我的所有测试都如下所示:

@pytest.mark.asyncio
async def test_my_func():
    result = await my_func()
    assert result == expected
Run Code Online (Sandbox Code Playgroud)

这很好用,但我宁愿不必装饰每一个函数来让它们工作。pytest 中有没有办法将此标记添加到每个测试函数中?

The*_*ler 12

您可以pytest_collection_modifyitems在您的conftest.py:

def pytest_collection_modifyitems(items):
    for item in items:
        item.add_marker('asyncio')
Run Code Online (Sandbox Code Playgroud)

或者要按pytestmark = pytest.mark.asyncio文件执行此操作,您可以在该文件中进行全局设置。