9

I need to flag certain tests to be skipped. However, some of the tests are parameterized and I need to be able to skip only certain of the scenarios.

I invoke the test using py.test -m "hermes_only" or py.test -m "not hermes_only" as appropriate.

Simple testcases are marked using:

@pytest.mark.hermes_only
def test_blah_with_hermes(self):

However, I have some parameterized tests:

outfile_scenarios = [('buildHermes'),
                     ('buildTrinity')]

@pytest.mark.parametrize('prefix', outfile_scenarios)
def test_blah_build(self, prefix):
    self._activator(prefix=prefix)

I would like a mechanism to filter the scenario list or otherwise skip certain tests if a pytest mark is defined.

More generally, how can I test for the definition of a pytest mark?

2 Answers 2

24

A nice solution from the documentation is this:

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
0
6

[Updated answer: With newer versions of python, it is now necessary to use the pattern described by @Migwell]

The documentation cited by @Migwell uses skipif but if you want to use a custom mark defined elsewhere, here is an updated version of my previous example:

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        

Additionally, custom marks should be registered to avoid PytestUknownMarkWarning complaints. In conftest.py, add the following:

def pytest_configure(config):
    config.addinivalue_line("markers", "trinity_only: Runs trinity_only tests.")
    config.addinivalue_line("markers", "legacy_only: Runs legacy_only tests.")

Alternatively, they can be registered in pytest.ini:

[pytest]
markers =
    trinity_only: Runs trinity_only tests.
    legacy_only: Runs legacy_only tests.

Custom marks are used by using pytest's -m option. For example, pytest -m trinity_only

I hope this addresses your problem with my original answer @adam-hoelscher

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.