unittest Integration with Mergify
Report your test results from Python's unittest, and from Django's test runner, to Mergify
This guide shows how to generate a JUnit report from tests written with
Python’s unittest, run
with unittest itself or with Django’s manage.py test, and upload it to
Test Engine from your CI workflow.
Generate a JUnit report with unittest
Section titled Generate a JUnit report with unittestunittest has no JUnit output of its own. The unittest-xml-reporting
package provides a test runner, xmlrunner, that runs your tests as unittest
does and writes the report.
Install it:
pip install unittest-xml-reportingThen run your tests through it, with the arguments you give unittest today:
python -m xmlrunner --output-file junit.xml discover -s tests -t .--output-file writes every test to one file. Without it, the runner writes a
file per test class, with a timestamp in its name, in the directory -o names.
With Django
Section titled With DjangoDjango’s manage.py test takes its runner from the TEST_RUNNER setting.
Install unittest-xml-reporting and point that setting at its Django runner:
TEST_RUNNER = "xmlrunner.extra.djangotestrunner.XMLTestRunner"TEST_OUTPUT_DIR = "test-reports"TEST_OUTPUT_FILE_NAME = "junit.xml"python manage.py test then writes test-reports/junit.xml after the tests,
with Django’s usual test database setup around them.
Update your CI workflow
Section titled Update your CI workflowThe examples below use junit.xml, written by the xmlrunner command above.
With the Django settings above, use test-reports/junit.xml and
python manage.py test instead.
GitHub Actions
Section titled GitHub ActionsRun the tests in a step with an id, and let the upload step decide the
job’s result:
- name: Run unittest Tests id: tests continue-on-error: true run: python -m xmlrunner --output-file junit.xml discover -s tests -t .- name: Mergify CI Upload if: success() || failure() uses: mergifyio/gha-mergify-ci@v27 with: token: ${{ secrets.MERGIFY_TOKEN }} report_path: junit.xml test_step_outcome: ${{ steps.tests.outcome }}Key Points:
-
if: success() || failure(): Runs the upload step even if tests fail, ensuring Test Engine has the full report. -
report_path: junit.xml: Points to where your JUnit file is located. Make sure it matches the path you set in your CI job. -
test_step_outcome: ${{ steps.tests.outcome }}: Passes the test runner step's outcome so Mergify can detect silent failures where the runner crashed but the JUnit report appears clean. Add anid(such astests) to your test runner step and update thesteps.<id>.outcomereference to match. -
On pull requests from forks and from Dependabot,
MERGIFY_TOKENis empty and this step fails: skip it as shown in Pull Requests Without the Secret.
If you use a job matrix in your workflow (e.g., to test across multiple versions), every leg
reports the same job name, because GitHub Actions gives them all one GITHUB_JOB. Set
the job_name input so Test Engine can tell the reports apart:
jobs: example_matrix: strategy: matrix: version: [10, 12, 14]Your upload step should look like:
- name: Mergify CI Upload if: success() || failure() uses: mergifyio/gha-mergify-ci@v27 with: job_name: example_matrix (${{ matrix.version }}) token: ${{ secrets.MERGIFY_TOKEN }} report_path: junit.xml test_step_outcome: ${{ steps.tests.outcome }}To benefit from Test Engine quarantine, you need to add continue-on-error: true
in your GitHub Actions step that executes your tests and generates the JUnit file.
The step running the gha-mergify-ci action then decides whether the job passes or fails,
taking quarantined tests into account.
You should also pass test_step_outcome: ${{ steps.tests.outcome }} to the step that runs
mergifyio/gha-mergify-ci (where tests is the id of your test runner step) to detect
silent failures where the test runner crashed but the JUnit report appears clean. Without
this input, a crash that produces a partial or empty report could be mistakenly treated as a
success.
With continue-on-error: true, the job’s result comes from the upload step. On
pull requests from forks and from Dependabot, MERGIFY_TOKEN is empty and the
upload step has to be skipped: add the step that fails the job when the tests
failed and nothing was uploaded, as shown in
pull requests without the secret.
Buildkite
Section titled Buildkitesteps: - label: "Run tests" command: <your test command> plugins: - mergifyio/mergify-ci#v8: action: junit-process report_path: junit.xmlKey Points:
-
The plugin runs in the
post-commandhook, so it uploads results after your tests finish, even if they fail. -
report_path: junit.xml: Points to where your JUnit file is located. Make sure it matches the path you set in your test configuration. - Silent failure detection is automatic: the plugin reads the step's exit code to detect cases where the test runner crashed but the JUnit report appears clean.
If you use a build matrix in your pipeline, each variation reports its own step label as its job
name, so the variations are already distinct. Set the job_name plugin property when the
label is not the name you want Test Engine to file the reports under:
steps: - label: "Flaky detection ({{matrix}})" matrix: - "3.10" - "3.11" - "3.12" command: <your test command> plugins: - mergifyio/mergify-ci#v8: action: junit-process job_name: "Tests ({{matrix}})" report_path: junit.xmlWhen using the Buildkite plugin for Test Engine quarantine, the plugin automatically detects test failures using the step’s exit code. No extra configuration is needed.
The mergifyio/mergify-ci plugin runs in the post-command hook and reads
BUILDKITE_COMMAND_EXIT_STATUS to detect silent failures where the test runner
crashed but the JUnit report appears clean.
Any CI (Mergify CLI)
Section titled Any CI (Mergify CLI)
Install the Mergify CLI in your pipeline and export
MERGIFY_TOKEN. Run
mergify ci junit-process after your tests:
set -o pipefailexit_code=0python -m xmlrunner --output-file junit.xml discover -s tests -t . || exit_code=$?mergify ci junit-process \ --test-exit-code "$exit_code" \ junit.xmlKey Points:
-
MERGIFY_TOKEN: Export this environment variable with your Mergify application key sojunit-processcan authenticate to the API. -
|| exit_code=$?: CI shells run with-e, so a failing test command on its own line would end the script before the upload. This form keeps going and records the code. The script needs bash forset -o pipefail: on Jenkins, whoseshsteps often rundash, start it with#!/bin/bash. -
--test-exit-code "$exit_code": Passes the test runner's exit code so Mergify can detect silent failures where the runner crashed but the JUnit report appears clean. -
junit-processexits last, so its status becomes the step's. Quarantine depends on that: it exits0when every failure it found is quarantined, and1when one is not. Re-exiting with the test runner's own code would fail the step whatever quarantine decided. -
The branch quarantine is evaluated against is detected from your CI provider, falling back to
the checked-out branch. Pass
--tests-target-branch <branch>only to override it.
Test names
Section titled Test namesTest Engine names each test by its module, its class and its method, joined
with dots: test_adds_an_item of the class CartTest in tests/test_cart.py
is reported as tests.test_cart.CartTest.test_adds_an_item, and the same test
in a Django app shop as shop.tests.CartTest.test_adds_an_item. Moving or
renaming the module, the class or the method starts a new history for the
test.
A failing subTest is reported as a test of its own, named after its
parameters: tests.test_cart.CartTest.test_totals (i=1). Subtests that pass
are not reported one by one.
Tests marked with unittest.skip are reported as skipped.
Verify and review in Test Engine
Section titled Verify and review in Test EngineAfter pushing these changes:
- Your CI workflow runs your tests through
xmlrunner, ormanage.py test. - The runner writes the JUnit report.
- Mergify uploads the report to Test Engine.
You can then review your test results, including any failures or flaky tests, directly in the Test Engine dashboard.
Troubleshooting tips
Section titled Troubleshooting tipsNo report with Django:
manage.py test --testrunneroverridesTEST_RUNNER. Check that your CI command does not pass another runner.- The CLI provides information about the upload. Check the logs in GitHub Actions.
- File paths: Double-check that the output file matches the path used in
report_path. - Permissions: Make sure the
MERGIFY_TOKENis valid and setup in your GitHub Actions secrets as explained in the docs. - Workflow conditions: If your step is not running, confirm the if condition is actually triggered in your job.
Was this page helpful?
Thanks for your feedback!