Skip to main content
 首页 » 编程设计

python之pytest不跳过未标记的测试

2025年05月04日10sharpest

我们有一个标记测试,我们希望它不会被执行,因为 py.test 是用另一个标记调用的,但测试正在执行。

例如

@pytest.mark.stress 
def test_one(some_fixture): 
     pass 
 
@pytest.mark.myplatform 
def test_two(some_fixture): 
     pass 

如果我用 --colleclonly -m "myplatform and (not stress)"作为实验运行 pytest,我发现我可以解决这个问题。我假设使用 fixture 在某种程度上改变了标记的评估方式,但我们假设使用 fixture 不会影响使用标记收集测试的方式。 fixture 中有代码可以查看标记,但我们不会以任何方式更改 pytest args。 克里斯

请您参考如下方法:

尝试使用 -k 标志,并保持相同的过滤逻辑“myplatform and not stress”。

https://pytest.org/en/latest/example/markers.html#using-k-expr-to-select-tests-based-on-their-name