Middle+
What are markers in pytest?
sobes.tech AI
Answer from AI
In pytest, markers are a way to label tests for grouping, filtering, or changing behavior during execution.
Main uses of markers:
- Grouping tests: for example, marking tests as "slow", "database", "api" and running only specific groups.
- Skipping tests: using the
@pytest.mark.skipor@pytest.mark.skipifmarker to skip tests under certain conditions. - Parameterization: although there is a separate mechanism for parameterization, markers can be used for additional logic.
Example of using a marker:
import pytest
@pytest.mark.slow
def test_heavy_computation():
# a test that takes a lot of time
assert True
@pytest.mark.skipif(condition, reason="Reason")
def test_skip_example():
assert False
To run tests with a specific marker, use the command line option:
pytest -m slow
Markers help organize tests and manage their execution in large projects.