Sub topic 3
Making your test runner write a report
This chapter assumes you can read a basic GitHub Actions workflow (jobs, steps, triggers), run a test suite from the command line, and read a short shell command. It covers how to make a test runner write a JUnit XML file and a coverage file that later steps can use.
Why the runner must write files
By default, a test runner prints results to the log. A log is readable by people but hard for tools to parse. A report file gives later steps something to upload, summarize, or check. The previous chapter described JUnit XML as the common format. Here you will produce it.
Two kinds of files matter:
- A test report, which lists each test and whether it passed, failed, errored, or was skipped.
- A coverage report, which lists which lines of your code ran during the tests.
Both are written by flags on the test command. You do not need extra steps for this.
Writing JUnit XML with pytest
The --junitxml flag tells pytest where to write the report. Coverage comes from the pytest-cov plugin. This job installs both, runs the tests, and writes three kinds of output:
name: tests
on: [push, pull_request]
permissions:
contents: read
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: "3.12"
- run: pip install -r requirements.txt pytest pytest-cov
- run: >-
pytest
--junitxml=reports/junit.xml
--cov=src
--cov-report=xml:reports/coverage.xml
--cov-report=term-missingThis assumes the project has a requirements.txt and a src package. Each flag does one job:
| Flag | Effect |
|---|---|
| `--junitxml=reports/junit.xml` | Writes the test report to that path. |
| `--cov=src` | Measures coverage for the `src` package only. |
| `--cov-report=xml:reports/coverage.xml` | Writes a coverage XML file. |
| `--cov-report=term-missing` | Prints a table in the log, with the line numbers that were not run. |
You can repeat --cov-report to get several outputs from one run. The XML file is for tools. The terminal table is for people reading the log.
Keep the files in one folder
Both files go into reports/. A single folder makes the next steps simple, because one upload step can collect everything. pytest creates the folder for the JUnit file if it does not exist, and coverage does the same for the XML file.
Common errors
- Report is missing after a failure. If the test step fails, later steps are skipped by default. The report file still exists on the runner, but nothing uploads it. The next chapter fixes this with status conditions.
- Coverage shows 0 percent or the wrong files.
--cov=srctakes a package or directory name. If your code lives elsewhere, the name does not match and nothing is measured. - Plugin not found. An error such as
unrecognized arguments: --covmeanspytest-covwas not installed. Check thepip installstep. - Relative paths differ from what you expect. Paths are relative to the working directory of the step. Keep the default working directory unless you have a reason to change it.
Check the output
Before relying on a report, confirm it exists and has content. Add a temporary step after the tests:
ls -l reports/
head -n 5 reports/junit.xmlYou should see both files and a first line that starts with <?xml. If the files are empty or absent, fix the test command before building summaries on top of it.
Exercise
Your workflow runs pytest --junitxml=reports/junit.xml --cov=src, but no coverage.xml file appears in reports/. What is missing, and why does the job log still show coverage numbers if you also have another flag?
Show answer
The command has no --cov-report=xml:reports/coverage.xml flag, so pytest-cov never writes an XML file. By default, pytest-cov prints a terminal table, which is why numbers can still appear in the log. Adding --cov-report=xml:reports/coverage.xml writes the file.