Sub topic 11
Selecting and controlling tests with markers
This chapter assumes you can write and run pytest tests, use assert, and write parametrized tests. A marker is a label attached to a test with the @pytest.mark.<name> decorator. Markers let you skip tests, expect failures, or pick groups of tests from the command line.
Skipping tests
Use skip when a test should not run at all. Use skipif when it should run only under some condition. Both take a reason, which -ra prints in the summary.
import sys
import pytest
@pytest.mark.skip(reason="feature not implemented yet")
def test_export_to_pdf():
assert False
@pytest.mark.skipif(sys.platform == "win32", reason="needs POSIX paths")
def test_posix_path():
assert "/tmp/data".startswith("/")A skipped test is reported as s. It is not a failure.
Expecting failures with xfail
xfail marks a test you expect to fail, such as a known bug. The test still runs, but a failure is reported as xfailed and does not fail the run.
import pytest
@pytest.mark.xfail(reason="rounding bug, see issue 42")
def test_known_rounding_bug():
assert round(2.675, 2) == 2.68
@pytest.mark.xfail(strict=True, reason="must fail until fixed")
def test_strict_example():
assert 1 + 1 == 3If an xfail test passes, pytest reports XPASS. By default that does not fail the run. With strict=True, an unexpected pass is a failure. This alerts you when a bug is fixed and the marker can be removed.
Custom markers
You can invent your own marker names. Register them in the configuration so pytest knows they are intentional.
[tool.pytest.ini_options]
markers = [
"slow: marks tests as slow (deselect with -m 'not slow')",
]Then apply the marker to a test:
import time
import pytest
@pytest.mark.slow
def test_big_computation():
time.sleep(2)
assert sum(range(1000)) == 499500Selecting tests from the command line
The -m option takes a marker expression. The -k option matches a substring of the test name.
pytest -m "not slow"
pytest -m "slow or integration"
pytest -k total --maxfail=1Marker expressions can combine names with and, or, and not.
Common errors
- Typos in marker names.
@pytest.mark.slwosilently creates a new marker, so-m "not slow"still runs the test. Add--strict-markerstoaddoptsso unregistered markers raise an error. - Confusing deselected with skipped. Deselected tests are filtered out before the run. Skipped tests are collected and then not executed.
- Marking fixtures. Markers applied to a fixture function have no effect. Mark the test instead.
- Using
skipfor bugs. Preferxfail, which tells you when the problem disappears.
Exercise
You mark a slow test with @pytest.mark.slow and register the marker. Which command runs every test except the slow ones, and what does pytest do with the slow test?
Show answer
Run pytest -m "not slow". The slow test is collected but deselected, so it does not run and is not reported as skipped or failed.
Exercise
A test marked @pytest.mark.xfail(strict=True) unexpectedly passes. What is the result of the run?
Show answer
The test is reported as a failure, because strict mode turns an unexpected pass (XPASS) into a failure. Without strict=True, it would be reported as XPASS and would not fail the run.