Sub topic 7
Generating coverage reports (XML, lcov, terminal)
This chapter assumes you can read a GitHub Actions workflow, run pytest locally, and know what line and branch coverage mean from the previous chapter. It shows how to make coverage.py and pytest-cov write the report formats that CI tools and people read.
One measurement, many reports
Coverage tools work in two steps. First they record which lines ran, and they store that data in a file named .coverage. Then they render the data into one or more report formats. Reports are cheap to generate, so you can ask for several in the same run.
| Format | Typical use | Flag with pytest-cov |
|---|---|---|
| Terminal | Quick read in the job log | `--cov-report=term-missing` |
| XML (Cobertura) | Coverage services, check-run actions, PR comment actions | `--cov-report=xml:reports/coverage.xml` |
| lcov | Tools that read `lcov.info`, such as many editor and dashboard integrations | `--cov-report=lcov:reports/lcov.info` |
| Markdown | Pasting into a job summary | `coverage report --format=markdown` |
The lcov output needs coverage.py 6.3 or later. The Markdown format needs 7.0 or later. If a flag is rejected, check the installed version with coverage --version.
Asking pytest-cov for several reports
You can repeat --cov-report to get more than one output from a single run. This step writes the terminal table, an XML file, and an lcov file:
- run: >-
pytest
--cov=src
--cov-report=term-missing
--cov-report=xml:reports/coverage.xml
--cov-report=lcov:reports/lcov.infoterm-missing adds a column listing the line numbers that were not run. That column is often all a developer needs to find the gap, and it appears directly in the job log.
If you do not name a path, the XML file lands in the working directory as coverage.xml. Naming a path inside reports/ keeps all outputs together, so one upload-artifact step can collect them.
Rendering reports after the tests
Sometimes you want a report that the test command did not produce, such as the Markdown table for a job summary. Run coverage after pytest, since .coverage is still on disk:
coverage report --format=markdown
coverage xml -o reports/coverage.xml
coverage lcov -o reports/lcov.infoThis works only if the data file exists. A test run that crashes before collecting data leaves you with nothing to render, and coverage report then fails with "No data to report."
Branch coverage
By default, coverage.py measures lines only. To also record whether each if took both paths, add --cov-branch. The XML report then includes branch rates, and the terminal table gains Branch and BrPart columns.
- run: pytest --cov=src --cov-branch --cov-report=xml:reports/coverage.xmlCommon errors
- Wrong source path.
--cov=srcmeasures the package or directory namedsrc. If your code lives elsewhere, the report shows 0 files or "Module src was never imported." - Report lists test files. Point
--covat your application code, not the whole repository. - Paths do not match in tools. XML reports record paths relative to the source root. A coverage service may need the checkout path to match, so generate reports on the runner that checked out the code.
- Missing report after a failure. pytest still writes coverage reports when tests fail. The upload step is what needs
if: ${{ !cancelled() }}, as covered earlier.
Exercise
A job runs pytest --cov=src --cov-report=term-missing. You now need an XML file at reports/coverage.xml and a Markdown table for the job summary, without running the tests again. Which commands do you add after the pytest step?
Show answer
Run coverage xml -o reports/coverage.xml and coverage report --format=markdown >> "$GITHUB_STEP_SUMMARY". Both read the existing .coverage data file, so the tests do not need to run again. Alternatively, add --cov-report=xml:reports/coverage.xml to the original pytest command for the XML file.