Pytest: Deselecting tests

If you are trying to run the tests from inside a python file, that is, you run your tests by calling

$ python testfile.py

which has contents

import pytest

pytest.main()

and you want to know how to pass the CLI flag in to pytest.main, the answer is:

pytest.main(["-m", "not slow"])

PS - yes, there are legitimate reasons to call tests this way. Pray you never have to learn them.


Additionally, with the recent addition of the "-m" command line option you should be able to write:

py.test -m "not (slow or long)"

IOW, the "-m" option accepts an expression which can make use of markers as boolean values (if a marker does not exist on a test function it's value is False, if it exists, it is True).


It's also possible to stack the mark decorators.

@pytest.mark.slow
@pytest.mark.main
def test_myfunction():
    pass

I then called py.test -m "slow and main" and only the tests with both decorators were called.

py.test -m "not (slow and main)" resulted in the other tests running


Looking through the pytest code (mark.py) and further experimentation shows the following seems to work:

pytest -k "-slow -long"

(Using the --collect-only option speeds up experimentation)

Tags:

Python

Pytest