简体   繁体   中英

pytest: is it possible to enable a marker from code?

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

eg pytest -m testsubset_a .

I wonder... is it also possible to enable one of these markers from inside the code? eg 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.

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"

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