为何多Fixture参数标记不生效?pytest 8.3.3技术咨询
pytest多Fixture参数标记筛选的逻辑与解决办法
首先,先明确你修改后的代码应该是给两个Fixture的smoke参数添加了标记,类似这样:
import pytest @pytest.fixture(params=[ "opt1_notsmoke", pytest.param("opt1_smoke", marks=pytest.mark.smoke) ]) def opt1(request): return request.param @pytest.fixture(params=[ "opt2_notsmoke", pytest.param("opt2_smoke", marks=pytest.mark.smoke) ]) def opt2(request): return request.param def test_1(opt1, opt2): print(opt1) print(opt2)
为什么会收集到3组用例?
pytest的标记筛选逻辑是:测试用例实例的标记集合是它依赖的所有Fixture参数标记的并集。当你执行-m smoke时,只要用例实例的标记集合中包含smoke标记,就会被选中。
对应你的场景,4组用例组合的标记情况如下:
opt1_notsmoke + opt2_notsmoke:无smoke标记 → 被排除opt1_smoke + opt2_notsmoke:opt1参数带smoke标记 → 符合筛选条件opt1_notsmoke + opt2_smoke:opt2参数带smoke标记 → 符合筛选条件opt1_smoke + opt2_smoke:两个参数都带smoke标记 → 符合筛选条件
所以最终会收集到3组用例,这是pytest标记继承的默认逻辑导致的。
如何筛选出仅两个参数都带smoke标记的组合?
如果你需要的是只有当两个Fixture参数都带smoke标记时才选中用例,可以用以下几种方法:
方法1:测试用例内条件跳过
直接在测试用例中判断参数是否符合smoke组合,不符合则跳过:
def test_1(opt1, opt2): if not (opt1.endswith("_smoke") and opt2.endswith("_smoke")): pytest.skip("Skip non-smoke combination") print(opt1) print(opt2)
执行时无需加-m smoke,直接运行即可自动跳过不符合的组合。
方法2:自定义钩子添加复合标记
在项目根目录创建conftest.py,通过钩子函数给符合条件的用例添加专属标记:
import pytest def pytest_collection_modifyitems(items): for item in items: # 获取当前用例组合的Fixture参数值 opt1_val = item.callspec.params.get("opt1") opt2_val = item.callspec.params.get("opt2") # 判断是否为双smoke组合 if opt1_val and opt2_val and opt1_val.endswith("_smoke") and opt2_val.endswith("_smoke"): item.add_marker(pytest.mark.full_smoke)
之后执行筛选命令时指定自定义标记:
pytest pt.py --collect-only -m full_smoke
这样就只会收集opt1_smoke + opt2_smoke这一组用例。
方法3:重构Fixture参数(适合简单场景)
如果场景固定,可以直接把双smoke组合单独作为一组参数,避免标记逻辑混淆:
import pytest @pytest.fixture(params=[ ("opt1_notsmoke", "opt2_notsmoke"), ("opt1_smoke", "opt2_notsmoke"), ("opt1_notsmoke", "opt2_smoke"), pytest.param(("opt1_smoke", "opt2_smoke"), marks=pytest.mark.smoke) ]) def smoke_options(request): return request.param def test_1(smoke_options): opt1, opt2 = smoke_options print(opt1) print(opt2)
此时执行-m smoke就只会选中双smoke的组合。
内容的提问来源于stack exchange,提问作者volnushka
相关产品推荐
相关产品推荐

