pytest将自动使用夹具参数化



我有大量使用pytest的测试(高100(,并且依赖于设置为自动使用的fixture。我需要运行同样的100个测试,其中有一个由夹具控制的微小变化。

考虑一下下面的设置,它演示了我试图使用的技术,但不起作用:

conftest.py

import pytest
def patch_0() -> int:
return 0
def patch_1() -> int:
return 1
@pytest.fixture(autouse=True)
@pytest.mark.parametrize("patch", [patch_0, patch_1])
def patch_time_per_test(monkeypatch, patch):
monkeypatch.setattr("time.time", patch)

my_test.py

import time

def test_00():
assert time.time() < 100

以下是我看到的错误示例:

file ../conftest.py, line 14
@pytest.fixture(autouse=True)
@pytest.mark.parametrize("patch", [patch_0, patch_1])
def patch_time_per_test(monkeypatch, patch):
E       fixture 'patch' not found

我看到了许多有点相关的问题,但我似乎找不到如何在autouse=True时对夹具进行参数化。似乎要做我想做的事情,我需要用@pytest.mark.parametrize装饰器更新100个测试,并独立地对每个测试进行参数化。想法?

我自己想好了。就这么简单:

conftest.py

import pytest
def patch_0() -> int:
return 0
def patch_1() -> int:
return 1
@pytest.fixture(autouse=True, params=[patch_0, patch_1])
def patch_time_per_test(monkeypatch, request): 
monkeypatch.setattr("time.time", request.param) 

最新更新