如何解决pytest参数化一个自动使用的夹具
我有大量使用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)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。