Skip to content
Latchkey

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.xml

Common 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

Run this faster and cheaper on Latchkey managed runners. Start free →