简体   繁体   中英

pytest - Mark tests with certain parameter value as pytest.mark.skip

My goal is to mark as @pytest.mark.skip tests which are parametrized with given value.

Example test:

@pytest.mark.parametrize('param', [value_not_to_filter, value_to_filter])
def test_example(param):
    ...

I would like to filter tests in pytest_collection_modifyitems from conftest.py :

def pytest_collection_modifyitems(config, items):
    for item in items:
        if value_to_filter in item.params: # this line is pseudocode, I'm looking for way to check if item was parametrized by some value
            item.add_marker(pytest.mark.skip(reason='some reason'))

You can use the hooks here, but the code won't be nice as you will have to dig through pytest 's internals and private accessors. An autouse fixture skipping the tests is IMO a much more clean solution:

@pytest.fixture(autouse=True)
def skip_some_tests(request):
    if 'param' in request.fixturenames and request.getfixturevalue('param') == value_to_filter:
        pytest.skip(reason='param is value_to_filter')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM