单元测试报告示例
- Tier: 基础版,专业版,旗舰版
- Offering: JihuLab.com, 私有化部署
单元测试报告 可以为多种语言和软件包生成。使用这些示例作为配置你的流水线以生成列出的语言和软件包的单元测试报告的指南。你可能需要编辑示例以匹配你正在使用的语言或软件包版本。
Ruby
在 .gitlab-ci.yml 中使用以下任务。这包括 artifacts:paths 关键词,以提供单元测试报告输出文件的链接。
yaml1## Use https://github.com/sj26/rspec_junit_formatter to generate a JUnit report format XML file with rspec 2ruby: 3 image: ruby:3.0.4 4 stage: test 5 before_script: 6 - apt-get update -y && apt-get install -y bundler 7 script: 8 - bundle install 9 - bundle exec rspec --format progress --format RspecJunitFormatter --out rspec.xml 10 artifacts: 11 when: always 12 paths: 13 - rspec.xml 14 reports: 15 junit: rspec.xml
Go
在 .gitlab-ci.yml 中使用以下任务:
yaml1## Use https://github.com/gotestyourself/gotestsum to generate a JUnit report format XML file with go 2golang: 3 stage: test 4 script: 5 - go install gotest.tools/gotestsum@latest 6 - gotestsum --junitfile report.xml --format testname 7 artifacts: 8 when: always 9 reports: 10 junit: report.xml
Java
有一些工具可以在 Java 中生成 JUnit 报告格式的 XML 文件。
Gradle
在以下示例中,gradle 用于生成测试报告。如果定义了多个测试任务,gradle 会在 build/test-results/ 下生成多个目录。在这种情况下,你可以通过定义以下路径来利用 glob 匹配:build/test-results/test/**/TEST-*.xml:
yaml1java: 2 stage: test 3 script: 4 - gradle test 5 artifacts: 6 when: always 7 reports: 8 junit: build/test-results/test/**/TEST-*.xml
Maven
为了解析 Surefire 和 Failsafe 测试报告,使用以下任务在 .gitlab-ci.yml 中:
yaml1java: 2 stage: test 3 script: 4 - mvn verify 5 artifacts: 6 when: always 7 reports: 8 junit: 9 - target/surefire-reports/TEST-*.xml 10 - target/failsafe-reports/TEST-*.xml
Python 示例
此示例使用 pytest 的 --junitxml=report.xml 标志将输出格式化为 JUnit 报告 XML 格式:
yaml1pytest: 2 stage: test 3 script: 4 - pytest --junitxml=report.xml 5 artifacts: 6 when: always 7 reports: 8 junit: report.xml
C/C++
有一些工具可以在 C/C++ 中生成 JUnit 报告格式的 XML 文件。
GoogleTest
在以下示例中,使用 gtest 来生成测试报告。如果为不同架构(x86,x64 或 arm)创建了多个 gtest 可执行文件,则需要为每个测试提供唯一的文件名。然后将结果聚合在一起。
yaml1cpp: 2 stage: test 3 script: 4 - gtest.exe --gtest_output="xml:report.xml" 5 artifacts: 6 when: always 7 reports: 8 junit: report.xml
CUnit
CUnit 可以在使用其 CUnitCI.h 宏运行时自动生成 JUnit 报告格式 XML 文件:
yaml1cunit: 2 stage: test 3 script: 4 - ./my-cunit-test 5 artifacts: 6 when: always 7 reports: 8 junit: ./my-cunit-test.xml
.NET
JUnitXML.TestLogger NuGet 软件包可以为 .Net Framework 和 .Net Core 应用程序生成测试报告。以下示例期望在存储库的根文件夹中有一个解决方案,并且在子文件夹中有一个或多个项目文件。每个测试项目生成一个结果文件,并将每个文件放置在产物文件夹中。此示例包括可选的格式化参数,这些参数可以提高测试数据在测试小部件中的可读性。
yaml1## Source code and documentation are here: https://github.com/spekt/junit.testlogger/ 2 3Test: 4 stage: test 5 script: 6 - 'dotnet test --test-adapter-path:. --logger:"junit;LogFilePath=..\artifacts\{assembly}-test-result.xml;MethodFormat=Class;FailureBodyFormat=Verbose"' 7 artifacts: 8 when: always 9 paths: 10 - ./**/*test-result.xml 11 reports: 12 junit: 13 - ./**/*test-result.xml
JavaScript
有一些工具可以在 JavaScript 中生成 JUnit 报告格式的 XML 文件。
Jest
jest-junit npm 软件包可以为 JavaScript 应用程序生成测试报告。在以下 .gitlab-ci.yml 示例中,javascript 任务使用 Jest 生成测试报告:
yaml1javascript: 2 image: node:latest 3 stage: test 4 before_script: 5 - 'yarn global add jest' 6 - 'yarn add --dev jest-junit' 7 script: 8 - 'jest --ci --reporters=default --reporters=jest-junit' 9 artifacts: 10 when: always 11 reports: 12 junit: 13 - junit.xml
为了使任务在没有带有单元测试的 .test.js 文件时通过,请在 script: 部分的 jest 命令末尾添加 --passWithNoTests 标志。
Karma
Karma-junit-reporter npm 软件包可以为 JavaScript 应用程序生成测试报告。在以下 .gitlab-ci.yml 示例中,javascript 任务使用 Karma 生成测试报告:
yaml1javascript: 2 stage: test 3 script: 4 - karma start --reporters junit 5 artifacts: 6 when: always 7 reports: 8 junit: 9 - junit.xml
Mocha
JUnit Reporter for Mocha NPM 软件包可以为 JavaScript 应用程序生成测试报告。在以下 .gitlab-ci.yml 示例中,javascript 任务使用 Mocha 生成测试报告:
yaml1javascript: 2 stage: test 3 script: 4 - mocha --reporter mocha-junit-reporter --reporter-options mochaFile=junit.xml 5 artifacts: 6 when: always 7 reports: 8 junit: 9 - junit.xml
Flutter 或 Dart
此示例 .gitlab-ci.yml 文件使用 JUnit Report 软件包将 flutter test 输出转换为 JUnit 报告 XML 格式:
yaml1test: 2 stage: test 3 script: 4 - flutter test --machine | tojunit -o report.xml 5 artifacts: 6 when: always 7 reports: 8 junit: 9 - report.xml
PHP
此示例使用 PHPUnit 的 --log-junit 标志。你还可以使用 XML 在 phpunit.xml 配置文件中添加此选项。
yaml1phpunit: 2 stage: test 3 script: 4 - composer install 5 - vendor/bin/phpunit --log-junit report.xml 6 artifacts: 7 when: always 8 reports: 9 junit: report.xml
Rust
此示例使用 cargo2junit,它安装在当前目录中。要从 cargo test 检索 JSON 输出,必须启用夜间编译器。
yaml1run unittests: 2 image: rust:latest 3 stage: test 4 before_script: 5 - cargo install --root . cargo2junit 6 script: 7 - cargo test -- -Z unstable-options --format json --report-time | bin/cargo2junit > report.xml 8 artifacts: 9 when: always 10 reports: 11 junit: 12 - report.xml
Helm
此示例使用 Helm Unittest 插件,使用 -t junit 标志将输出格式化为 JUnit 报告 XML 格式。
yaml1helm: 2 image: helmunittest/helm-unittest:latest 3 stage: test 4 script: 5 - '-t JUnit -o report.xml -f tests/*[._]test.yaml .' 6 artifacts: 7 reports: 8 junit: report.xml
-f tests/*[._]test.yaml 标志配置 helm-unittest 在 tests/ 目录中查找以以下任一结尾的文件:
- .test.yaml
- _test.yaml