pytest: Run Tests Command Reference for CI
The test runner at the center of most Python CI suites.
pytest discovers and runs tests, reporting failures with rich tracebacks. This page covers the flags and CI patterns you reach for when wiring it into a pipeline.
Common flags / usage
- pytest tests/ -- run tests under a directory
- -q, --quiet -- less verbose output for CI logs
- -v -- verbose, one line per test
- -ra -- summary of all non-passing outcomes at the end
- --strict-markers -- error on unregistered markers
- python -m pytest -- run via the interpreter so the project is importable
Example
shell
- run: pip install -e ".[test]"
- run: python -m pytest -q -ra --strict-markersIn CI
A bare "pytest" can fail collection with ModuleNotFoundError because the project root is not on sys.path. Run "python -m pytest" (which adds the working directory) or install the package with pip install -e . first.
Key takeaways
- pytest discovers and runs tests with detailed failure output.
- python -m pytest adds the working directory to sys.path, fixing import errors.
- Use -q -ra in CI for compact logs with a clear failure summary.
Related guides
pytest -k: Select Tests by Name Command ReferenceReference for pytest -k in CI: selecting tests by a name expression with and/or/not operators, sharding a sui…
pytest -x and --maxfail: Fail-Fast Command ReferenceReference for pytest fail-fast flags in CI: -x to stop on the first failure and --maxfail=N to stop after N,…
pytest --cov: Coverage Command Reference for CIReference for pytest --cov from pytest-cov in CI: measuring coverage, enforcing a threshold with --cov-fail-u…