Help us learn about your current experience with the documentation. Take the survey.

单元测试报告示例

  • Tier: Free, Premium, Ultimate
  • Offering: GitLab.com, GitLab Self-Managed, GitLab Dedicated

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

Ruby

.gitlab-ci.yml 中使用以下作业。这其中包含了 artifacts:paths 关键字,用于提供到单元测试报告输出文件的链接。

## 使用 https://github.com/sj26/rspec_junit_formatter 通过 rspec 生成 JUnit 报告格式的 XML 文件
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 中使用以下作业:

## 使用 https://github.com/gotestyourself/gotestsum 通过 go 生成 JUnit 报告格式的 XML 文件
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

要解析 SurefireFailsafe 测试 报告,请在 .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

当使用其 CUnitCI.h 宏运行时,CUnit 可以自动生成 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 应用程序生成测试报告。以下 示例假定在仓库的根文件夹中有一个解决方案,子文件夹中有一个或多个 项目文件。每个测试项目会生成一个结果文件,每个文件 都放在产物文件夹中。此示例包括可选的格式化参数,这些 参数可以提高测试小组件中测试数据的可读性。一个完整的 .Net Core 示例可供参考

## 源代码和文档请见: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

Mocha 的 JUnit Reporter 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 标志。 您也可以在 phpunit.xml 配置文件中使用 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 输出,您必须启用 nightly 编译器。

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 标志,以将输出格式化为 XML 格式的 JUnit 报告。

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