Useful flags and patterns when using pytest
CLI arguments
Mute warnings
pytest tests/ -p no:warnings
Specific test and parameterization
Run a specific test using ::
pytest tests/engine/test_features.py::test_specific_feature
Use []
to specify a parameterization.
# tests/engine/test_features.py
import pytest
@pytest.mark.parametrize(
"param,number",
[("foo", 1), ("foo", 2), ("bar", 1), ("bar", 2)]
)
def test_features(param, number):
# ...
assert ...
For example, runs the test test_specific_feature
with param="bar"
and number=1
pytest tests/engine/test_features.py::test_specific_feature[bar-1]
You can also use the -k
flag to do pattern matching and select multiple tests. The following catches all the parameterization with bar
pytest -k bar tests/engine/test_features.py::test_specific_feature
This allows to select complex parameterization such as [foo-2-bar-2025-python3]
without specifying it in full
Run last failed
This will skip tests that succeeded on the previous runs. This allows to progressively narrow failing tests.
pytest tests/ --last-failed
New changes could make previously successful tests fail. You need to rerun the full suite at the end.
Stop at first failure
pytest tests/ -x
Not stopping at first failure may provide more context to improve the code.