python - pytest 参数化一个自动使用的夹具
问题描述
我有大量使用 pytest 并依赖于设置为自动使用的夹具的测试(高 100 秒)。我需要运行相同的 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)
推荐阅读
- python - 报告变量未定义
- css - 当 @include 一个 mixin 时,它引用了一个之前传递的变量
- python - 如何拉伸y轴
- java - 通过变量中的数字更改小数位 - JAVA
- xamarin - 无法将按钮添加到已创建的 FlexLayout
- python - 从本地主机移植我的烧瓶应用程序时无法弄清楚它有什么问题
- android - Chat Android - Firebase,同步延迟问题
- web-scraping - 带有 .NET Core 3.1 的 HtmlAgilityPack:UTF-8,text/html' 不是受支持的编码名称
- momentjs - moment.js 日期时间添加中的无效日期
- php - 如何正确找到匹配的 php.ini 文件