是否可以跳过从多个 @pytest.mark.parametrize 行生成的测试? [英] Is it possible to skip a test generated from multiple @pytest.mark.parametrize lines?
问题描述
我有一个带有多个 @pytest.mark.parametrize 行的 pytest 测试函数.每个参数化变量的值可以是 0 或 1.某些生成的组合不会发生,应该跳过,即在我的示例中,当 subcontact 取值 0 时,当快速跟踪取值 1 时,应该跳过它.是否可以使用多个 @pytest.mark.parametrize 固定装置来实现这一点?
I have a pytest test function with multiple @pytest.mark.parametrize lines. The values for each parameterised variable can take either 0 or 1. Some of the combination generated cannot happen and should be skipped i.e. in my example when subcontact takes the value 0 and when fast track takes the value 1, it should be skipped. Is it possible to achieve this with the multiple @pytest.mark.parametrize fixture?
我用下面的代码尝试了这个,但是当我运行测试时没有报告被跳过.
I attempted this with the code below but no tests were reported as skipped when I ran the tests.
@pytest.mark.parametrize('prospect', [0, 1])
@pytest.mark.parametrize('client', [0, 1])
@pytest.mark.parametrize('subcontact', [0, 1])
@pytest.mark.parametrize('default', [0, 1])
@pytest.mark.parametrize('primary_contact', [0, 1])
@pytest.mark.parametrize('fast_track', [0, pytest.param(1, marks=pytest.mark.skipif('subcontact' == 0,
reason='Cannot happen'))])
def test_prospect_registration(prospect, client, subcontact, default,
primary_contact,
fast_track):
pass
推荐答案
最简单的可能是跳过测试主体内的特定测试:
The easiest possibililty is probably to skip the specific test inside the test body:
...
def test_prospect_registration(prospect, client, subcontact, default,
primary_contact,
fast_track):
if fast_track == 1 and subcontact == 0:
pytest.skip('Cannot happen')
...
您也可以根据测试名称在夹具中进行跳过(参数从最后一个 mark.parametrize
装饰器向上列出):
You could also do the skipping in a fixture based on th test name (the parameters are listed from the last mark.parametrize
decorator up):
import re
@pytest.fixture(autouse=True)
def skip_unwanted(request):
if re.match(r'test_prospect_registration\[1-.*-.*-.0.*-.*\]', item.name)
pytest.skip('Cannot happen')
或者你可以在 pytest_collection_modifyitems
中做同样的事情:
or you could do the same in pytest_collection_modifyitems
:
conftest.py
def pytest_collection_modifyitems(config, items):
for item in items:
if re.match(r'test_prospect_registration\[1-.*-.*-.0.*-.*\]', item.name):
item.add_marker('skip')
这篇关于是否可以跳过从多个 @pytest.mark.parametrize 行生成的测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!