I wish to configure pytest
such that it excludes some tests by default; but it should be easy to include them again with some command line option. I only found -k
, and I have the impression that that allows complex specifications, but am not sure how to go about my specific need…
The exclusion should be part of the source or a config file (it’s permanent – think about very long-running tests which should only be included as conscious choice, certainly never in a build pipeline…).
Bonus question: if that is not possible, how would I use -k
to exclude specific tests? Again, I saw hints in the documentation about a way to use not
as a keyword, but that doesn’t seem to work for me. I.e., -k "not longrunning"
gives an error about not being able to find a file “notrunning”, but does not exclude anything…
Advertisement
Answer
You can use pytest to mark some tests and use -k arg to skip or include them.
For example consider following tests,
import pytest def test_a(): assert True @pytest.mark.never_run def test_b(): assert True def test_c(): assert True @pytest.mark.never_run def test_d(): assert True
you can run pytest like this to run all the tests
pytest
To skip the marked tests you can run pytest like this,
pytest -m "not never_run"
If you want to run the marked tests alone,
pytest -m "never_run"