{{< details >}}

  • Tier: 基础版,专业版,旗舰版
  • Offering: JihuLab.com, 私有化部署

{{< /details >}}

单元测试报告 可以为多种语言和软件包生成。使用这些示例作为配置你的流水线以生成列出的语言和软件包的单元测试报告的指南。你可能需要编辑示例以匹配你正在使用的语言或软件包版本。

Ruby

.gitlab-ci.yml 中使用以下任务。这包括 artifacts:paths 关键词,以提供单元测试报告输出文件的链接。

## Use https://github.com/sj26/rspec_junit_formatter to generate a JUnit report format XML file with rspec
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

Go

.gitlab-ci.yml 中使用以下任务:

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

Java

有一些工具可以在 Java 中生成 JUnit 报告格式的 XML 文件。

Gradle

在以下示例中,gradle 用于生成测试报告。如果定义了多个测试任务,gradle 会在 build/test-results/ 下生成多个目录。在这种情况下,你可以通过定义以下路径来利用 glob 匹配:build/test-results/test/**/TEST-*.xml

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

Maven

为了解析 Surefire 和 Failsafe 测试报告,使用以下任务在 .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 示例

此示例使用 pytest 的 --junitxml=report.xml 标志将输出格式化为 JUnit 报告 XML 格式:

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

C/C++

有一些工具可以在 C/C++ 中生成 JUnit 报告格式的 XML 文件。

GoogleTest

在以下示例中,使用 gtest 来生成测试报告。如果为不同架构(x86x64arm)创建了多个 gtest 可执行文件,则需要为每个测试提供唯一的文件名。然后将结果聚合在一起。

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

CUnit

CUnit 可以在使用其 CUnitCI.h 宏运行时自动生成 JUnit 报告格式 XML 文件:

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

.NET

JUnitXML.TestLogger NuGet 软件包可以为 .Net Framework 和 .Net Core 应用程序生成测试报告。以下示例期望在存储库的根文件夹中有一个解决方案,并且在子文件夹中有一个或多个项目文件。每个测试项目生成一个结果文件,并将每个文件放置在产物文件夹中。此示例包括可选的格式化参数,这些参数可以提高测试数据在测试小部件中的可读性。

## 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

有一些工具可以在 JavaScript 中生成 JUnit 报告格式的 XML 文件。

Jest

jest-junit npm 软件包可以为 JavaScript 应用程序生成测试报告。在以下 .gitlab-ci.yml 示例中,javascript 任务使用 Jest 生成测试报告:

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

为了使任务在没有带有单元测试的 .test.js 文件时通过,请在 script: 部分的 jest 命令末尾添加 --passWithNoTests 标志。

Karma

Karma-junit-reporter npm 软件包可以为 JavaScript 应用程序生成测试报告。在以下 .gitlab-ci.yml 示例中,javascript 任务使用 Karma 生成测试报告:

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

Mocha

JUnit Reporter for Mocha NPM 软件包可以为 JavaScript 应用程序生成测试报告。在以下 .gitlab-ci.yml 示例中,javascript 任务使用 Mocha 生成测试报告:

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

Flutter 或 Dart

此示例 .gitlab-ci.yml 文件使用 JUnit Report 软件包将 flutter test 输出转换为 JUnit 报告 XML 格式:

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

PHP

此示例使用 PHPUnit 的 --log-junit 标志。你还可以使用 XML 在 phpunit.xml 配置文件中添加此选项。

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

Rust

此示例使用 cargo2junit,它安装在当前目录中。要从 cargo test 检索 JSON 输出,必须启用夜间编译器。

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

Helm

此示例使用 Helm Unittest 插件,使用 -t junit 标志将输出格式化为 JUnit 报告 XML 格式。

helm:
  image: helmunittest/helm-unittest:latest
  stage: test
  script:
    - '-t JUnit -o report.xml -f tests/*[._]test.yaml .'
  artifacts:
    reports:
      junit: report.xml

-f tests/*[._]test.yaml 标志配置 helm-unittesttests/ 目录中查找以以下任一结尾的文件:

  • .test.yaml
  • _test.yaml