我需要标记某些要跳过的测试。但是,某些测试是参数化的,我需要能够仅跳过某些场景。
py.test -m "hermes_only"我根据需要使用或调用测试py.test -m "not hermes_only"。
简单的测试用例使用以下标记:
@pytest.mark.hermes_only
def test_blah_with_hermes(self):
Run Code Online (Sandbox Code Playgroud)
但是,我有一些参数化测试:
outfile_scenarios = [('buildHermes'),
('buildTrinity')]
@pytest.mark.parametrize('prefix', outfile_scenarios)
def test_blah_build(self, prefix):
self._activator(prefix=prefix)
Run Code Online (Sandbox Code Playgroud)
我想要一种机制来过滤场景列表,或者如果定义了 pytest 标记则跳过某些测试。
更一般地说,如何测试 pytest 标记的定义?
Mig*_*ell 23
文档中的一个很好的解决方案是:
import pytest
@pytest.mark.parametrize(
("n", "expected"),
[
(1, 2),
pytest.param(1, 0, marks=pytest.mark.xfail),
pytest.param(1, 3, marks=pytest.mark.xfail(reason="some bug")),
(2, 3),
(3, 4),
(4, 5),
pytest.param(
10, 11, marks=pytest.mark.skipif(sys.version_info >= (3, 0), reason="py2k")
),
],
)
def test_increment(n, expected):
assert n + 1 == expected
Run Code Online (Sandbox Code Playgroud)
[更新的答案:使用较新版本的 python,现在需要使用@Migwell 描述的模式]
@Migwell 引用的文档使用skipif但如果您想使用其他地方定义的自定义标记,这里是我之前示例的更新版本:
import pytest
scenarios = [pytest.param('trinity', 'buildTrinity',
marks=pytest.mark.trinity_only),
pytest.param('legacy', 'buildLegacy',
marks=pytest.mark.legacy_only)]
@pytest.mark.parametrize('tcid, prefix', scenarios,
ids=[id[0] for id in scenarios])
def test_sample(self, tcid, prefix):
pass
Run Code Online (Sandbox Code Playgroud)
此外,应注册定制标记以避免PytestUknownMarkWarning投诉。在 中conftest.py,添加以下内容:
def pytest_configure(config):
config.addinivalue_line("markers", "trinity_only: Runs trinity_only tests.")
config.addinivalue_line("markers", "legacy_only: Runs legacy_only tests.")
Run Code Online (Sandbox Code Playgroud)
或者,他们可以在以下位置注册pytest.ini:
[pytest]
markers =
trinity_only: Runs trinity_only tests.
legacy_only: Runs legacy_only tests.
Run Code Online (Sandbox Code Playgroud)
通过使用 pytest 的-m选项来使用自定义标记。例如, pytest -m trinity_only
我希望这可以用我的原始答案解决您的问题@adam-hoelscher
| 归档时间: |
|
| 查看次数: |
8562 次 |
| 最近记录: |