问题描述
我有大量使用pytest的测试(最高100s),并且依赖于设置为自动使用的夹具。我需要运行相同的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_1])
def patch_time_per_test(monkeypatch,patch):
E fixture 'patch' not found
我看到许多somewhat related questions,但是当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)