Sub topic 10
Turning test and coverage data into a summary table
This chapter assumes you can read a basic workflow file, run pytest from the command line, and write Markdown tables. It also assumes you know that a job summary is a Markdown file that GitHub shows on the run page. Earlier chapters showed how to write to it. Here you will build its content from real test and coverage data.
The working model
A summary is only useful if it is generated from the same files your test runner already produced. The flow has three parts:
- The test runner writes JUnit XML and coverage data to disk.
- A small script reads those files and prints Markdown.
- The Markdown is appended to the file named by
GITHUB_STEP_SUMMARY.
Because the summary step reads files and not live output, it works only if the reports exist. That is why it must run after the tests and must not be skipped when they fail.
Summarizing JUnit XML
This script reads a JUnit XML file, adds up the counts from every testsuite element, and appends a table to the summary file.
import os
import sys
import xml.etree.ElementTree as ET
root = ET.parse(sys.argv[1]).getroot()
suites = list(root.iter("testsuite"))
totals = {k: sum(int(s.get(k, 0)) for s in suites)
for k in ("tests", "failures", "errors", "skipped")}
lines = [
"### Test results",
"",
"| Tests | Failures | Errors | Skipped |",
"|---|---|---|---|",
f"| {totals['tests']} | {totals['failures']} | {totals['errors']} | {totals['skipped']} |",
]
with open(os.environ["GITHUB_STEP_SUMMARY"], "a", encoding="utf-8") as f:
f.write("\n".join(lines) + "\n")Three details matter:
root.iter("testsuite")finds the suites whether the root element istestsuitesor a singletestsuite.- The file is opened with
"a"(append). Opening it with"w"would erase what earlier steps wrote. - Summing assumes suites are not nested. Pytest output is flat, but other runners can nest suites and double count.
Save the script as .github/scripts/junit_summary.py.
Adding coverage and wiring the step
coverage.py can print its report as Markdown, so you do not need a second script. Add this step after the test step.
- name: Job summary
if: ${{ !cancelled() }}
run: |
python .github/scripts/junit_summary.py reports/junit.xml
echo "### Coverage" >> "$GITHUB_STEP_SUMMARY"
coverage report --format=markdown >> "$GITHUB_STEP_SUMMARY"The condition !cancelled() lets the step run after a test failure but not after a manual cancel. Without it, a failing test step would skip the summary, and you would lose the table exactly when you need it.
Common errors
| Symptom | Likely cause |
|---|---|
| Summary is empty after a failed run | The summary step has no `if:` condition, so it was skipped |
| Script fails with a missing file | The test step crashed before writing `junit.xml`, or `--junitxml` points to a different path |
| `KeyError: 'GITHUB_STEP_SUMMARY'` | You ran the script locally. The variable exists only on the runner |
| Table renders as plain text | There is no blank line before the table, or the separator row is missing |
| `coverage report` says no data | `pytest-cov` wrote its data elsewhere, or the tests never ran |
Exercise
A teammate's summary step has no if: line. Tests fail, and the run page shows no summary. Why, and what is the smallest fix?
Show answer
By default a step runs only if all earlier steps succeeded, so the failed test step caused the summary step to be skipped. Add if: ${{ !cancelled() }} to the summary step so it runs after failures but not after a cancellation.
Exercise
What happens to earlier summary content if the script opens GITHUB_STEP_SUMMARY with mode "w" instead of "a"?
Show answer
The file is truncated, so Markdown written by earlier steps in the same step's file is lost. Use append mode.