doc/ci/testing/unit_test_report_examples.md
{{< details >}}
{{< /details >}}
Use these examples as guidelines for configuring unit test reports in different languages and testing frameworks. Unit test reports require your test framework to generate JUnit XML format output and your CI/CD job to upload the results as artifacts.
The following examples show individual job configurations to add to your .gitlab-ci.yml file.
All examples use:
artifacts:when: always to upload reports even when tests fail.artifacts:reports:junit to specify the JUnit XML file location.before_script when required.Each example is a functional job that you can copy and adapt for your project. You might need to:
image: specification for your environment.For setup instructions and troubleshooting, see unit test reports.
| Language | Tool | JUnit output flag |
|---|---|---|
| .NET | JunitXML.TestLogger | --logger:"junit;LogFilePath=report.xml" |
| C/C++ | GoogleTest | --gtest_output="xml:report.xml" |
| C/C++ | CUnit | Automatic with CUnitCI.h macros |
| Flutter/Dart | junitreport | | tojunit -o report.xml |
| Go | gotestsum | --junitfile report.xml |
| Helm | helm-unittest | -t JUnit -o report.xml |
| Java | Gradle | Automatic in build/test-results/test/ |
| Java | Maven | Automatic in target/surefire-reports/ and target/failsafe-reports/ |
| JavaScript | jest-junit | --reporters=jest-junit |
| JavaScript | karma-junit-reporter | --reporters junit |
| JavaScript | mocha-gitlab-reporter | --reporter mocha-gitlab-reporter |
| PHP | PHPUnit | --log-junit report.xml |
| Python | pytest | --junitxml=report.xml |
| Ruby | rspec_junit_formatter | --format RspecJunitFormatter --out report.xml |
| Rust | cargo2junit | | cargo2junit > report.xml |
Generate JUnit XML reports with .NET using the JunitXML.TestLogger NuGet package:
Test:
stage: test
script:
- 'dotnet test --test-adapter-path:. --logger:"junit;LogFilePath=..\artifacts\{assembly}-test-result.xml;MethodFormat=Class;FailureBodyFormat=Verbose"'
artifacts:
when: always
paths:
- ./**/*test-result.xml
reports:
junit:
- ./**/*test-result.xml
This example expects a solution in the root folder of the repository, with one or more project files in sub-folders. One result file is produced per test project, and each file is placed in the artifacts folder. The formatting arguments improve the readability of test data in the test widget.
Generate JUnit XML reports with GoogleTest using built-in XML output:
cpp:
stage: test
script:
- gtest.exe --gtest_output="xml:report.xml"
artifacts:
when: always
reports:
junit: report.xml
If there are multiple gtest executables created for different architectures (x86, x64 or arm),
make sure each test has a unique filename. The results are then aggregated together.
Generate JUnit XML reports with CUnit
using CUnitCI.h macros:
cunit:
stage: test
script:
- ./my-cunit-test
artifacts:
when: always
reports:
junit: ./my-cunit-test.xml
Generate JUnit XML reports with Flutter or Dart using the junitreport package:
test:
stage: test
script:
- flutter test --machine | tojunit -o report.xml
artifacts:
when: always
reports:
junit:
- report.xml
This example uses the junitreport package to convert flutter test output into JUnit report XML format.
Generate JUnit XML reports with Go using gotestsum:
golang:
stage: test
script:
- go install gotest.tools/gotestsum@latest
- gotestsum --junitfile report.xml --format testname
artifacts:
when: always
reports:
junit: report.xml
Generate JUnit XML reports with Helm using the Helm Unittest plugin:
helm:
image: helmunittest/helm-unittest:latest
stage: test
script:
- '-t JUnit -o report.xml -f tests/*[._]test.yaml .'
artifacts:
when: always
reports:
junit: report.xml
The -f tests/*[._]test.yaml flag configures helm-unittest to look for files in the tests/ directory that end in either .test.yaml or _test.yaml.
Generate JUnit XML reports with Gradle using built-in test reporting:
java:
stage: test
script:
- gradle test
artifacts:
when: always
reports:
junit: build/test-results/test/**/TEST-*.xml
If there are multiple test tasks defined, gradle generates multiple directories under build/test-results/.
In that case, you can leverage glob matching by defining the following path: build/test-results/test/**/TEST-*.xml.
Generate JUnit XML reports with Maven using Surefire and Failsafe test reports:
java:
stage: test
script:
- mvn verify
artifacts:
when: always
reports:
junit:
- target/surefire-reports/TEST-*.xml
- target/failsafe-reports/TEST-*.xml
Generate JUnit XML reports with Jest using the jest-junit npm package:
javascript:
image: node:latest
stage: test
before_script:
- 'yarn global add jest'
- 'yarn add --dev jest-junit'
script:
- 'jest --ci --reporters=default --reporters=jest-junit'
artifacts:
when: always
reports:
junit:
- junit.xml
To make the job pass when there are no .test.js files with unit tests,
add the --passWithNoTests flag to the end of the jest command in the script: section.
Generate JUnit XML reports with Karma using the karma-junit-reporter npm package:
javascript:
stage: test
script:
- karma start --reporters junit
artifacts:
when: always
reports:
junit:
- junit.xml
For a Mocha configuration example, see mocha-gitlab-reporter.
Generate JUnit XML reports with PHP using PHPUnit:
phpunit:
stage: test
script:
- composer install
- vendor/bin/phpunit --log-junit report.xml
artifacts:
when: always
reports:
junit: report.xml
You can also configure this option using XML in the phpunit.xml configuration file.
Generate JUnit XML reports with Python using pytest:
pytest:
stage: test
script:
- pytest --junitxml=report.xml
artifacts:
when: always
reports:
junit: report.xml
Generate JUnit XML reports with RSpec using the rspec_junit_formatter gem:
ruby:
image: ruby:3.0.4
stage: test
before_script:
- apt-get update -y && apt-get install -y bundler
script:
- bundle install
- bundle exec rspec --format progress --format RspecJunitFormatter --out rspec.xml
artifacts:
when: always
paths:
- rspec.xml
reports:
junit: rspec.xml
Generate JUnit XML reports with Rust using cargo2junit:
run unittests:
image: rust:latest
stage: test
before_script:
- cargo install --root . cargo2junit
script:
- cargo test -- -Z unstable-options --format json --report-time | bin/cargo2junit > report.xml
artifacts:
when: always
reports:
junit:
- report.xml
To retrieve JSON output from cargo test, you must enable the nightly compiler.
The tool is installed in the current directory.