---
title: NUnit Integration with Mergify
description: Report your test results from NUnit tests to Mergify
---

This guide shows how to generate JUnit reports from your NUnit tests and upload
them to **Test Engine** using your CI workflow.

## Generate a JUnit report with NUnit

The sections below write the report with the `JunitXml.TestLogger` package for
`dotnet test`, or transform the NUnit Console Runner's own output.

### Using dotnet test with the JUnit logger

Install the JUnit test logger:

```bash
dotnet add package JunitXml.TestLogger
```

Run tests with JUnit output:

```bash
dotnet test --logger "junit;LogFilePath=junit.xml"
```

`LogFilePath` is resolved relative to the directory holding your test project's
`.csproj`, not to the repository root. If your test project lives in a
subdirectory, set the upload step's `report_path` to the report's path from the
repository root, such as `tests/MyProject.Tests/junit.xml`.

### Using the NUnit Console Runner

The NUnit Console Runner supports only two result formats, `nunit3` and
`nunit2`; there is no built-in JUnit format. To get JUnit XML out of it, pass an
XSLT that transforms the native `nunit3` output. NUnit publishes one as
[`nunit3-junit.xslt`](https://github.com/nunit/nunit-transforms/tree/master/nunit3-junit):

```bash
nunit3-console.exe your-test.dll "--result=junit.xml;transform=nunit3-junit.xslt"
```

Quote the whole option: the semicolon separating the file name from the
transform is a command separator in most shells.

If you don't want to maintain an XSLT, run your tests through `dotnet test` with
the JUnit logger instead, as shown above.

### Using the NUnit test adapter

Add the NUnit test adapter to your test project:

```xml
<PackageReference Include="NUnit" Version="3.13.3" />
<PackageReference Include="NUnit3TestAdapter" Version="5.0.0" />
<PackageReference Include="JunitXml.TestLogger" Version="6.1.0" />
```

Then run:

```bash
dotnet test --logger junit
```

Without a `LogFilePath`, the logger writes into the `TestResults` directory next
to your test project.

### Using multiple test result formats

You can generate multiple formats simultaneously:

```bash
dotnet test --logger "console;verbosity=detailed" --logger "junit;LogFilePath=junit.xml" --logger "trx;LogFileName=results.trx"
```

### Using NUnit with custom settings

You can also configure test settings in a `.runsettings` file:

```xml
<?xml version="1.0" encoding="utf-8"?>
<RunSettings>
  <LoggerRunSettings>
    <Loggers>
      <Logger friendlyName="junit" enabled="true">
        <Configuration>
          <LogFilePath>junit.xml</LogFilePath>
        </Configuration>
      </Logger>
    </Loggers>
  </LoggerRunSettings>
</RunSettings>
```

Then run:

```bash
dotnet test --settings test.runsettings
```

## Update your CI workflow

### GitHub Actions

Run the tests in a step with an `id`, and let the upload step decide the
job's result:

```yaml
- name: Run NUnit Tests and Generate JUnit Report
  id: tests
  continue-on-error: true
  run: dotnet test --logger "junit;LogFilePath=junit.xml"
```

```yaml
- name: Mergify Test Engine Upload
  if: (success() || failure()) && (!github.event.pull_request || github.event.pull_request.head.repo.full_name == github.repository)
  uses: mergifyio/gha-mergify-ci@v35
  with:
    report_path: junit.xml
    test_step_outcome: ${{ steps.tests.outcome }}
```

Key points:

- `if: (success() || failure()) && (!github.event.pull_request || github.event.pull_request.head.repo.full_name == github.repository)`: runs the upload step even when the tests fail, so **Test Engine** gets the full report, and skips it on pull requests from forks, which get no ID token.
- `report_path: junit.xml`: where your test run writes the JUnit file. It must match the path your test configuration uses.
- `test_step_outcome: ${{ steps.tests.outcome }}`: passes the test step's outcome, so Mergify detects a runner that crashed while the JUnit report looks clean. Give your test step an `id` (here `tests`) and use it in `steps.<id>.outcome`.

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:

```yaml
jobs:
  example_matrix:
    strategy:
      matrix:
        version: [10, 12, 14]
```

Your upload step should look like:

```yaml
- name: Mergify Test Engine Upload
  if: (success() || failure()) && (!github.event.pull_request || github.event.pull_request.head.repo.full_name == github.repository)
  uses: mergifyio/gha-mergify-ci@v35
  with:
    job_name: example_matrix (${{ matrix.version }})
    report_path: junit.xml
    test_step_outcome: ${{ steps.tests.outcome }}
```

> **Note:** the job name is part of a test's identity, so a scheduled workflow has to report the same name as the job running those tests on your pull requests.

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.

With `continue-on-error: true`, the job's result comes from the upload step,
which pull requests from forks skip: add the step that fails the job when the
tests failed and nothing was uploaded, as shown in
[pull requests from forks](/ci-insights/setup/github-actions#pull-requests-from-forks).

#### Let your CI reach Mergify

Grant `id-token: write` to the job, or to the whole workflow. The job then trades its GitHub-signed ID token for a Mergify OIDC CI token, and no `MERGIFY_TOKEN` is stored:

```yaml
permissions:
  id-token: write # lets the job ask GitHub for an ID token
  contents: read # actions/checkout needs it
```

A `permissions` block sets every permission it does not list to `none`: list the ones your other steps need too.

Mergify refuses the ID token until a trust policy of your organization matches the job. A GitHub organization `Owner` or a **CI Admin** writes one under **Settings → Security & Access → CI Authentication → Trust Policies** on the [dashboard](https://dashboard.mergify.com). [OIDC Authentication](/integrations/gha/oidc) covers trust policies, pull requests from forks and refused exchanges. If your workflows store `MERGIFY_TOKEN` today, [move them off it](/integrations/gha/oidc#move-your-workflows-off-mergify_token).

#### Use an application key instead

Other CIs authenticate with a `ci` [application key](/api/application-keys#ci-reporting), and so do the GitHub Actions jobs OIDC leaves out: runs for pull requests from forks when your organization sends them secrets, and jobs that send scopes from the base branch (`pull_request_target`, `workflow_run`, `issue_comment`). Store the key as the `MERGIFY_TOKEN` repository secret and set it on the job:

```yaml
env:
  MERGIFY_TOKEN: ${{ secrets.MERGIFY_TOKEN }}
```

A `MERGIFY_TOKEN` that is set wins over the ID token. For pull requests from forks, set it on those runs only and drop the fork condition from the upload step, as [Jobs that keep a key](/integrations/gha/oidc#jobs-that-keep-a-key) shows. Store the key in your [Dependabot secrets](/integrations/dependabot#reaching-mergify-from-dependabot-pull-requests) too, since runs for Dependabot pull requests read only those.

### Buildkite

```yaml
steps:
  - label: "Run tests"
    command: <your test command>
    plugins:
      - mergifyio/mergify-ci#v30:
          action: junit-process
          report_path: junit.xml
```

Key points:

- The plugin runs in the `post-command` hook, so it uploads results **after** your tests finish, even if they fail.
- `report_path: junit.xml`: where your test run writes the JUnit file. It must match the path your test configuration uses.
- Silent failure detection is automatic: the plugin reads the step's exit code to detect a runner that crashed while the JUnit report looks 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:

```yaml
steps:
  - label: "Flaky detection ({{matrix}})"
    matrix:
      - "3.10"
      - "3.11"
      - "3.12"
    command: <your test command>
    plugins:
      - mergifyio/mergify-ci#v30:
          action: junit-process
          job_name: "Tests ({{matrix}})"
          report_path: junit.xml
```

> **Note:** the job name is part of a test's identity, so a scheduled pipeline has to report the same name as the step running those tests on your pull requests.

> **Caution: keep the token out of the pipeline you upload.** Set `MERGIFY_TOKEN` in the environment of the agents running your steps: [Buildkite secrets](https://buildkite.com/docs/pipelines/security/secrets/buildkite-secrets), a secrets plugin backed by your own secrets store, or an agent `environment` hook that exports it. Do not put the token in the pipeline itself: `buildkite-agent pipeline upload` interpolates `${MERGIFY_TOKEN}` before the pipeline is stored, so a token written into an `env:` block is uploaded in plain text and stays readable in the Buildkite UI and in build history. The plugin's `token:` property carries it the same way.

When 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)

Install the [Mergify CLI](/cli/usage) in your pipeline and export `MERGIFY_TOKEN`. Run `mergify ci junit-process` after your tests:

```bash
set -o pipefail
exit_code=0
<your test command> || exit_code=$?
mergify ci junit-process \
  --test-exit-code "$exit_code" \
  junit.xml
```

Key points:

- `MERGIFY_TOKEN`: export this environment variable with your Mergify application key so `junit-process` can authenticate to the API. On GitHub Actions, a job granted `id-token: write` needs none: it trades its ID token for a Mergify OIDC CI token, as [OIDC Authentication](/integrations/gha/oidc) explains.
- `|| 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 for `set -o pipefail`: on Jenkins, whose `sh` steps often run `dash`, start it with `#!/bin/bash`.
- `--test-exit-code "$exit_code"`: passes the test runner's exit code, so Mergify detects a runner that crashed while the JUnit report looks clean.
- `junit-process` runs last, so its status becomes the step's: `0` when every failure it found is quarantined, `1` when one is not.
- 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.

## Verify and review in Test Engine

After pushing these changes:

1. Your CI workflow runs your NUnit tests.
2. The JUnit logger writes `junit.xml`.
3. 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](https://dashboard.mergify.com/test-engine/detection).

## Troubleshooting tips

<ul>
  <li>Test adapter: Ensure both NUnit and NUnit3TestAdapter packages are properly referenced in your test project.</li>
  <li>Logger package: Make sure the JunitXml.TestLogger package is installed and compatible with your .NET version.</li>

  <li>The CLI provides information about the upload. Check the logs in GitHub Actions.</li>
<li>File paths: Double-check that the output file matches the path used in `report_path`.</li>
<li>
  Authentication: The upload step's log says why Mergify refused the job's ID token, as [OIDC
  troubleshooting](/integrations/gha/oidc#troubleshooting) details. A job that sets
  `MERGIFY_TOKEN` uses that key instead: check that it is valid and granted [CI
  Reporting](/api/application-keys#ci-reporting), or [move the job off
  it](/integrations/gha/oidc#move-your-workflows-off-mergify_token).
</li>
<li>Workflow conditions: If your step is not running, confirm the if condition is actually triggered in your job.</li>
</ul>
