2

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

例如

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

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

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

4

2 回答 2

1

尝试-k使用 flag 并保持相同的过滤逻辑“myplatform 而不是压力”。

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

于 2016-06-02T05:21:35.327 回答
0

基于标记的测试选择/取消选择用于将测试运行限制为明确标记的测试。如果您使用该选项,您将无法识别它--collectonly(在下面的示例中总是collected 3 items)。

考虑测试文件test_markers.py

import pytest

@pytest.mark.stress
def test_stress():
     pass

@pytest.mark.myplatform
def test_myplatform():
     pass

def test_unmarked():
     pass

如果您只想执行“压力”测试,请使用 (-v用于详细输出)

pytest test_markers.py -v -m stress

你得到以下输出:

collected 3 items

test_markers.py::test_stress PASSED

如果要执行“压力”测试和未标记的测试,请使用:

pytest test_markers.py -v -m "not myplatform"

这给了你输出:

collected 3 items

test_markers.py::test_stress PASSED
test_markers.py::test_unmarked PASSED
于 2017-10-13T09:57:26.093 回答