Sub topic 4
Keeping reporting steps running after a test failure
This chapter assumes you can read a GitHub Actions workflow (jobs, steps, triggers) and run a test suite from the command line. It also assumes you know that a test runner can write a report file, such as JUnit XML, which the previous chapter covered.
Why reporting steps get skipped
By default, a step runs only if every earlier step in the job succeeded. This is the same as writing if: ${{ success() }}. When pytest exits with a non-zero status, the job moves to a failed state and skips all later steps.
That is a problem for reporting. The run where you most need the JUnit file, the coverage report, and the summary table is the run where the tests failed. A skipped upload step leaves you with only the raw log.
The status check functions
You control this with an if: condition on the step. GitHub provides four status functions.
| Function | The step runs when |
|---|---|
| `success()` | All earlier steps succeeded. This is the default. |
| `failure()` | An earlier step in the job failed. |
| `always()` | Always, even if the workflow run was cancelled. |
| `cancelled()` | The workflow run was cancelled. |
For reporting, !cancelled() is usually the best choice. It runs after success and after failure, but it respects a manual cancel, so you do not keep a cancelled run busy.
- uses: actions/upload-artifact@v4
if: ${{ !cancelled() }}
with:
name: test-reports
path: reports/<br>
Compare this with always(). A step with always() can still run after you cancel the workflow. It can also hold up a runner that you expected to stop. Use always() only for cleanup that must happen no matter what.
Use failure() for failure-only work
Some steps make sense only when something went wrong, such as printing extra debug output. Use failure() for those.
- name: Show server log
if: ${{ failure() }}
run: cat logs/server.logA complete example
This job keeps the test step as the one that decides the job result. The reporting steps run afterward and do not change that result, except that they can fail on their own.
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
- uses: actions/upload-artifact@v4
if: ${{ !cancelled() }}
with:
name: test-reports
path: reports/When a test fails, the job still ends as failed, so the pull request shows a red check. The difference is that the artifact now exists and you can download it.
Common errors
- Condition without an expression. Write
if: ${{ !cancelled() }}. In YAML, a bare value that starts with!is read as a tag, so an unquotedif: !cancelled()breaks the workflow file. The${{ }}wrapper avoids this. - Using
always()everywhere. The step then runs even after cancellation. Prefer!cancelled(). - Report file never created. If the test step crashes before writing the file, the upload finds nothing. The
upload-artifactaction warns by default. Addif-no-files-found: errorif you want the step to fail instead. - Expecting the job to turn green. A status condition changes whether a step runs. It never changes the result of a step that already failed.
Exercise
A job runs pytest, then an upload step with no if: condition. Tests fail. Does the upload run? Which condition would you add so it runs after both passing and failing tests, but not after a manual cancel?
Show answer
No. The default condition is success(), so the upload is skipped after a failed step. Add if: ${{ !cancelled() }} to the upload step.