I have several markers in my python test code:
@pytest.mark.slowtest
@pytest.mark.webtest
@pytest.mark.stagingtest
I am able to selectively run tests with a marker using for example pytest -m slowtest
How can I run unmarked tests without resorting to pytest -m "not (slowtest or webtest or stagingtest)"
?
As you can imagine, we might use other markers in the future...
The simplest way to skip a test function is to mark it with the skip decorator which may be passed an optional reason : @pytest. mark.
To use markers, we have to import pytest module in the test file. We can define our own marker names to the tests and run the tests having those marker names. -m <markername> represents the marker name of the tests to be executed.
Multiple custom markers can be registered, by defining each one in its own line, as shown in above example. You can ask which markers exist for your test suite - the list includes our just defined webtest and slow markers: $ pytest --markers @pytest. mark.
Another option I found useful is to use pytest.ini
to add default options. We explicitly write which markers to skip. It entails mainlining the list of skipped markers in pytest.ini
. In other words, not 100% what the OP asked for but this is the best SO question I found when looking for default markers. Hopefully, this can help others.
From the docs:
addopts
Add the specified OPTS to the set of command line arguments as if they had been specified by the user. Example: if you have this ini file content:
# content of pytest.ini
[pytest]
addopts = --maxfail=2 -rf # exit after 2 failures, report fail info
issuing pytest test_hello.py actually means:
pytest --maxfail=2 -rf test_hello.py
Default is to add no options.
What I added to my pytest.ini
[pytest]
addopts = -m "not slow"
markers =
slow: marks tests as slow (deselect with '-m "not slow"')
Using it
﮸$ grep 'mark.slow' tests/*.py | wc -l
3
$ pytest -s
======================================================= test session starts ========================================================
platform linux -- Python 3.8.2, pytest-6.0.1, py-1.9.0, pluggy-0.13.1
rootdir: /tests, configfile: pytest.ini
collected 66 items / 3 deselected / 63 selected
../tests/test_app.py ...............................................................
================================================= 63 passed, 3 deselected in 8.70s =================================================
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With