debian-mirror-gitlab/doc/ci/testing/unit_test_report_examples.md
2022-08-27 11:52:29 +05:30

6.8 KiB

stage group info
Verify Pipeline Insights To determine the technical writer assigned to the Stage/Group associated with this page, see https://about.gitlab.com/handbook/engineering/ux/technical-writing/#assignments

Unit test report examples (FREE)

Unit test reports can be generated for many languages and packages. Use these examples as guidelines for configuring your pipeline to generate unit test reports for the listed languages and packages. You might need to edit the examples to match the version of the language or package you are using.

Ruby

Use the following job in .gitlab-ci.yml. This includes the artifacts:paths keyword to provide a link to the Unit test report output file.

## Use https://github.com/sj26/rspec_junit_formatter to generate a JUnit report format XML file with rspec
ruby:
  stage: test
  script:
    - bundle install
    - bundle exec rspec --format progress --format RspecJunitFormatter --out rspec.xml
  artifacts:
    when: always
    paths:
      - rspec.xml
    reports:
      junit: rspec.xml

Go

Use the following job in .gitlab-ci.yml:

## Use https://github.com/gotestyourself/gotestsum to generate a JUnit report format XML file with go
golang:
  stage: test
  script:
    - go get gotest.tools/gotestsum
    - gotestsum --junitfile report.xml --format testname
  artifacts:
    when: always
    reports:
      junit: report.xml

Java

There are a few tools that can produce JUnit report format XML file in Java.

Gradle

In the following example, gradle is used to generate the test reports. 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:

java:
  stage: test
  script:
    - gradle test
  artifacts:
    when: always
    reports:
      junit: build/test-results/test/**/TEST-*.xml

In GitLab Runner 13.0 and later, you can use **.

Maven

For parsing Surefire and Failsafe test reports, use the following job in .gitlab-ci.yml:

java:
  stage: test
  script:
    - mvn verify
  artifacts:
    when: always
    reports:
      junit:
        - target/surefire-reports/TEST-*.xml
        - target/failsafe-reports/TEST-*.xml

Python example

This example uses pytest with the --junitxml=report.xml flag to format the output into the JUnit report XML format:

pytest:
  stage: test
  script:
    - pytest --junitxml=report.xml
  artifacts:
    when: always
    reports:
      junit: report.xml

C/C++

There are a few tools that can produce JUnit report format XML files in C/C++.

GoogleTest

In the following example, gtest is used to generate the test reports. If there are multiple gtest executables created for different architectures (x86, x64 or arm), you are required to run each test providing a unique filename. The results are then aggregated together.

cpp:
  stage: test
  script:
    - gtest.exe --gtest_output="xml:report.xml"
  artifacts:
    when: always
    reports:
      junit: report.xml

CUnit

CUnit can be made to produce JUnit report format XML files automatically when run using its CUnitCI.h macros:

cunit:
  stage: test
  script:
    - ./my-cunit-test
  artifacts:
    when: always
    reports:
      junit: ./my-cunit-test.xml

.NET

The JunitXML.TestLogger NuGet package can generate test reports for .Net Framework and .Net Core applications. The following 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. This example includes optional formatting arguments, which improve the readability of test data in the test widget. A full .Net Core example is available.

## Source code and documentation are here: https://github.com/spekt/junit.testlogger/

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

JavaScript

There are a few tools that can produce JUnit report format XML files in JavaScript.

Jest

The jest-junit npm package can generate test reports for JavaScript applications. In the following .gitlab-ci.yml example, the javascript job uses Jest to generate the test reports:

javascript:
  stage: test
  script:
    - 'jest --ci --reporters=default --reporters=jest-junit'
  artifacts:
    when: always
    reports:
      junit:
        - junit.xml

Karma

The Karma-junit-reporter npm package can generate test reports for JavaScript applications. In the following .gitlab-ci.yml example, the javascript job uses Karma to generate the test reports:

javascript:
  stage: test
  script:
    - karma start --reporters junit
  artifacts:
    when: always
    reports:
      junit:
        - junit.xml

Mocha

The JUnit Reporter for Mocha NPM package can generate test reports for JavaScript applications. In the following .gitlab-ci.yml example, the javascript job uses Mocha to generate the test reports:

javascript:
  stage: test
  script:
    - mocha --reporter mocha-junit-reporter --reporter-options mochaFile=junit.xml
  artifacts:
    when: always
    reports:
      junit:
        - junit.xml

Flutter or Dart

This example .gitlab-ci.yml file uses the JUnit Report package to convert the flutter test output into JUnit report XML format:

test:
  stage: test
  script:
    - flutter test --machine | tojunit -o report.xml
  artifacts:
    when: always
    reports:
      junit:
        - report.xml

PHP

This example uses PHPUnit with the --log-junit flag. You can also add this option using XML in the phpunit.xml configuration file.

phpunit:
  stage: test
  script:
    - composer install
    - vendor/bin/phpunit --log-junit report.xml
  artifacts:
    when: always
    reports:
      junit: report.xml