単体テストレポートの例
- プラン: Free、Premium、Ultimate
- 提供形態: GitLab.com、GitLab Self-Managed、GitLab Dedicated
単体テストレポートは、多くの言語とパッケージに対して生成できます。これらの例をガイドラインとして、リストされた言語とパッケージの単体テストレポートを生成するようにパイプラインを設定します。使用している言語またはパッケージのバージョンに合わせて、例を編集する必要がある場合があります。
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.xmlGo
.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.xmlJava
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-*.xmlMaven
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-*.xmlPythonの例
この例では、--junitxml=report.xmlフラグを指定したpytestを使用して、出力をJUnitレポートのXML形式に整形します。
pytest:
stage: test
script:
- pytest --junitxml=report.xml
artifacts:
when: always
reports:
junit: report.xmlC/C++
C/C++には、JUnitレポート形式のXMLファイルを生成できるツールがいくつかあります。
GoogleTest
次の例では、gtestを使用してテストレポートを生成します。異なるアーキテクチャ(x86、x64、またはarm)用に複数のgtest実行可能ファイルが作成されている場合は、一意のファイル名を指定して各テストを実行する必要があります。結果はその後、集約されます。
cpp:
stage: test
script:
- gtest.exe --gtest_output="xml:report.xml"
artifacts:
when: always
reports:
junit: report.xmlCUnit
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アプリケーション用のテストレポートを生成できます。次の例では、リポジトリのルートフォルダーにソリューションがあり、サブフォルダーに1つ以上のプロジェクトファイルがあることを想定しています。テストプロジェクトごとに1つの結果ファイルが生成され、各ファイルはアーティファクトフォルダーに配置されます。この例には、テストウィジェットでのテストデータの可読性を向上させるオプションの書式設定引数が含まれています。完全な.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.xmlJavaScript
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.xmlMocha
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.xmlFlutterまたは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.xmlPHP
この例では、--log-junitフラグを指定したPHPUnitを使用します。phpunit.xml設定ファイルでXMLを使用して、このオプションを追加することもできます。
phpunit:
stage: test
script:
- composer install
- vendor/bin/phpunit --log-junit report.xml
artifacts:
when: always
reports:
junit: report.xmlRust
この例では、現在のディレクトリにインストールされている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.xmlHelm
この例では、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フラグは、tests/ディレクトリ内の次のいずれかで終わるファイルを検索するようにhelm-unittestを設定します。
.test.yaml_test.yaml