Is it possible to enable a marker from code?

725 views Asked by At

It is possible to only run tests marked by a certain marker by using the -m argument when calling pytest.

e.g. pytest -m testsubset_a.

I wonder... Is it also possible to enable one of these markers from inside the code? e.g. somewhere during pytest_collection_modifyitems()?

The reason why I'm asking is because I'm working on a test-harnass that will use pytest markers to enable a specific subset of tests, but did not do so in the past. Since we want to be backwards compatible, I want to enable a default subset (marker) when no -m argument was given.

1

There are 1 answers

5
Opifex On BEST ANSWER

You can access the markers passed to -m from inside the code quite easily with config.option.markerexpr.

So... defaulting when no option was passed is as easy as

def pytest_collection_modifyitems(config):
    if config.option.markerexpr == "":
        config.option.markerexpr = "mydefaultmarker"