Running pytest in GitHub Actions
Run pytest in CI across Python versions with coverage and JUnit results.
pytest in CI usually pairs with a Python version matrix, pytest-cov for coverage, and JUnit XML output so the test results render nicely. This guide covers setup-python with caching and the common flags.
What you need
- pytest and pytest-cov in your dependencies.
- A requirements file or pyproject for setup-python caching.
- A Python version matrix if you support several.
The workflow
Test across versions with coverage and JUnit output.
.github/workflows/ci.yml
strategy:
matrix:
python-version: ["3.10", "3.11", "3.12"]
steps:
- uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
cache: pip
- run: pip install -r requirements.txt
- run: pytest --cov --junitxml=results.xmlCommon gotchas
- Without setup-python caching, each matrix leg reinstalls everything from scratch.
- A matrix multiplies CI minutes per version; faster managed runners (Latchkey) blunt that cost.
- pytest exits 5 when no tests are collected - that can fail CI unexpectedly.
Key takeaways
- Pair pytest with a Python version matrix and pip caching.
- Use pytest-cov for coverage and --junitxml for reporting.
- Watch for exit code 5 when no tests are collected.
Related guides
Running Jest in GitHub ActionsRun Jest tests in GitHub Actions with CI mode, coverage, and sharding so your Node test suite is fast and det…
Running Vitest in GitHub ActionsRun Vitest in GitHub Actions with run mode, coverage via v8, and sharding for fast Vite-based test suites.
Running Testcontainers in GitHub ActionsRun integration tests that spin up real databases with Testcontainers in GitHub Actions, where Docker is alre…