テストデータの収集
On This Page
- 概要
- ストレージ使用量の表示
- テストインサイト
- Server v2.x 用のテストインサイト
- フォーマッタの有効化
- カスタムテストランナーの例
- JavaScript
- Jest
- Node.js 用の Mocha
- Mocha と nyc の組み合わせ
- Karma
- Node.js 用の AVA
- ESLint
- Ruby
- RSpec
- Minitest
- Cucumber
- Python
- pytest
- unittest
- Java
- Java JUnit の結果に使用する Maven Surefire プラグイン
- Gradle JUnit のテスト結果
- PHP
- PHPUnit
- .NET
- Clojure
- Kaocha
- Clojure テスト用の test2junit
- API
CircleCI でテストを実行する場合、テスト結果を保存する方法が 2 つあります。 アーティファクトを使用する方法と store_test_results
ステップを使用する方法です。 それぞれの方法にメリットがあるので、プロジェクト毎に決定する必要があります。
store_test_results
ステップを使ってデータを保存する場合、CircleCI はデータを XML ファイルから収集し、そのデータを使ってジョブのインサイトを提供します。 ここでは、よく使用されるテストランナー用にテストデータを XML として出力し、store_test_results
ステップでレポートを保存するように CircleCI を設定する方法について説明します。
store_test_results
ステップ を使うと以下が可能です。
- CircleCI Web アプリの テスト
- テストインサイトと結果が不安定なテストの検出
- テストの分割
一方で、テスト結果をアーティファクトとして保存すると、生の XML を見ることができます。 これは、プロジェクトにおけるテスト結果の処理の設定に関する問題をデバッグする際に便利です。たとえば、誤ってアップロードしたファイルを探す場合に効果的です。
テスト結果をビルドアーティファクトとして表示するには、 store_artifacts
ステップを使ってテスト結果をアップロードします。 アーティファクトはストレージを使用します。そのため、アーティファクトの保存によって料金が発生します。 アーティファクトなどのオブジェクトをストレージに保存する期間をカスタマイズする方法については、 データの永続化のページを参照してください。
注: store_test_results
と store_artifacts
の両方を使ってテスト結果をアップロードすることも可能です。
概要
store_test_results
ステップを使用すると、テスト結果をアップロードして保存することができ、また CircleCI のWeb アプリで成功したテストおよび失敗したテストを表示することができます。
このテスト結果の表示は、ジョブを表示する際に以下に示すように Tests タブから利用できます。
.circleci/config.yml
では、 store_test_results
キーは以下のように使用します。
steps:
- run:
#...
# run tests and store XML files to a subdirectory, for example, test-results
#...
- store_test_results:
path: test-results
ここで、path
キーは、JUnit XML または Cucumber JSON テストのメタデータファイルのサブディレクトリが含まれる working_directory
への絶対パスまたは相対パス、またはすべてのテスト結果が含まれる一つのファイルのパスです。
**注: **path
の値が非表示のフォルダーではないことを確認してください。 たとえば、.my_hidden_directory
は無効な形式です。
ストレージ使用量の表示
ストレージの使用状況の表示、および毎月のストレージの超過料金の計算については、 データの永続化ガイドを参照してください。
テストインサイト
インサイト機能を使ったテストに関する情報については、 テストインサイトをご確認下さい。 このページでは、結果の不安定なテストの検知、失敗の多いテスト、実行速度の遅いテスト、およびパフォーマンスの概要について説明しています。
また、テストの失敗に関する情報については、 API v2 のインサイトのエンドポイントをご覧ください。
Server v2.x 用のテストインサイト
CircleCI Server v2.x をご使用の場合、テストメタデータを収集するように設定すると、頻繁に失敗するテストがインサイトのページのリストに表示されます。それにより、不安定なテストを特定し、繰り返し発生する問題を隔離することができます。
フォーマッタの有効化
JUnit フォーマッタを有効化するまで、テストメタデータは CircleCI で自動的には収集されません。 RSpec、Minitest、および Django に、以下の設定を追加してフォーマッタを有効化します。
- RSpec では、gemfile に以下を追加する必要があります。
gem 'rspec_junit_formatter'
- Minitest では、gemfile に以下を追加する必要があります。
gem 'minitest-ci'
- Django は、 django-nose テストランナーを使用して設定する必要があります。
注: iOS アプリケーションをテストする方法は、 macOS での iOS アプリケーションのテストをご覧ください。
カスタムテストランナーの例
このセクションでは、以下のテスト ランナーの例を示します。
言語 | テストランナー | フォーマッタ | 例 | ||
---|---|---|---|---|---|
JavaScript | Jest | jest-junit | 例 | ||
JavaScript | Mocha | mocha-junit-reporter | 例、 NYC での例 | ||
JavaScript | Karma | karma-junit-reporter | 例 | ||
JavaScript | AVA | tap-xunit | 例 | ||
JavaScript | ESLint | JUnit formatter | 例 | ||
Ruby | RSpec | rspec_junit_formatter | 例 | ||
Ruby | Minitest | minitest-ci | 例 | ||
Cucumber | ビルトイン | 例 | |||
Python | pytest | ビルトイン | 例 | ||
Python | unittest | テストの実行には pytest を使用 | 例 | ||
Java | Maven | Maven Surefire プラグイン | 例 | ||
Java | Gradle | ビルトイン | 例 | ||
PHP | PHPUnit | ビルトイン | 例 | ||
.NET | trx2junit | 例 | |||
Clojure | Kaocha | kaocha-junit-xml | 例 | ||
Clojure | clojure.test | test2junit | 例 |
JavaScript
Jest
Jest で JUnit の互換テストデータを出力するには、 jest-junitを使用します。
.circleci/config.yml
の作業セクションは、以下のようになります。
steps:
- run:
name: Install JUnit coverage reporter
command: yarn add --dev jest-junit
- run:
name: Run tests with JUnit as reporter
command: jest --ci --runInBand --reporters=default --reporters=jest-junit
environment:
JEST_JUNIT_OUTPUT_DIR: ./reports/
- store_test_results:
path: ./reports/
全体の手順については、Viget の記事「 Using JUnit on CircleCI 2.0 with Jest and ESLint (Jest および ESLint と共に CircleCI 2.0 で JUnit を使用する)」を参照してください。 記事の中の jest cli 引数 --testResultsProcessor
の使用は、 --reporters
の構文に置き換えられているのでご注意ください。また、JEST_JUNIT_OUTPUT は JEST_JUNIT_OUTPUT_DIR
および JEST_JUNIT_OUTPUT_NAME
に置き換えられています(上図参照)。
注: Jest テストの実行時には、--runInBand
フラグを使用してください。 このフラグがない場合、Jest は、ジョブを実行している仮想マシン全体に CPU リソースを割り当てようとします。 --runInBand
を使用すると、Jest は、仮想マシン内の仮想化されたビルド環境のみを使用するようになります。
--runInBand
の詳細については、 Jest CLI ドキュメントを参照してください。 この問題の詳細については、公式 Jest リポジトリの Issue 1524 と Issue 5239 を参照してください。
Node.js 用の Mocha
Mocha のテストランナーで JUnit テストを出力するには、 mocha-junit-reporter を使用します。
.circleci/config.yml
のテスト用作業セクションは、以下のようになります。
steps:
- checkout
- run: npm install
- run: mkdir ~/junit
- run:
command: mocha test --reporter mocha-junit-reporter
environment:
MOCHA_FILE: ~/junit/test-results.xml
when: always
- store_test_results:
path: ~/junit
Mocha と nyc の組み合わせ
以下は、 marcospgp から提供された、Mocha と nyc の組み合わせに使用できるサンプルの全文です。
version: 2
jobs:
build:
environment:
CC_TEST_REPORTER_ID: code_climate_id_here
NODE_ENV: development
docker:
- image: cimg/node:16.10
auth:
username: mydockerhub-user
password: $DOCKERHUB_PASSWORD # context / project UI env-var reference
environment:
MONGODB_URI: mongodb://admin:password@localhost:27017/db?authSource=admin
- image: mongo:4.0
auth:
username: mydockerhub-user
password: $DOCKERHUB_PASSWORD # context / project UI env-var reference
environment:
MONGO_INITDB_ROOT_USERNAME: admin
MONGO_INITDB_ROOT_PASSWORD: password
working_directory: ~/repo
steps:
- checkout
# Update npm
- run:
name: update-npm
command: 'sudo npm install -g npm@latest'
# Download and cache dependencies
- restore_cache:
keys:
- v1-dependencies-{{ checksum "package-lock.json" }}
# fallback to using the latest cache if no exact match is found
- v1-dependencies-
- run: npm install
- run: npm install mocha-junit-reporter # just for CircleCI
- save_cache:
paths:
- node_modules
key: v1-dependencies-{{ checksum "package-lock.json" }}
- run: mkdir reports
# Run mocha
- run:
name: npm test
command: ./node_modules/.bin/nyc ./node_modules/.bin/mocha --recursive --timeout=10000 --exit --reporter mocha-junit-reporter --reporter-options mochaFile=reports/mocha/test-results.xml
when: always
# Run eslint
- run:
name: eslint
command: |
./node_modules/.bin/eslint ./ --format junit --output-file ./reports/eslint/eslint.xml
when: always
# Run coverage report for Code Climate
- run:
name: Setup Code Climate test-reporter
command: |
# download test reporter as a static binary
curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
chmod +x ./cc-test-reporter
./cc-test-reporter before-build
when: always
- run:
name: code-coverage
command: |
mkdir coverage
# nyc report requires that nyc has already been run,
# which creates the .nyc_output folder containing necessary data
./node_modules/.bin/nyc report --reporter=text-lcov > coverage/lcov.info
./cc-test-reporter after-build -t lcov
when: always
# Upload results
- store_test_results:
path: reports
- store_artifacts: # upload test coverage as artifact
path: ./coverage/lcov.info
prefix: tests
Karma
Karma テストランナーで JUnit テストを出力するには、 karma-junit-reporter を使用します。
.circleci/config.yml
の作業セクションは、以下のようになります。
steps:
- checkout
- run: npm install
- run: mkdir ~/junit
- run:
command: karma start ./karma.conf.js
environment:
JUNIT_REPORT_PATH: ./junit/
JUNIT_REPORT_NAME: test-results.xml
when: always
- store_test_results:
path: ./junit
// karma.conf.js
// additional config...
{
reporters: ['junit'],
junitReporter: {
outputDir: process.env.JUNIT_REPORT_PATH,
outputFile: process.env.JUNIT_REPORT_NAME,
useBrowserName: false
},
}
// additional config...
Node.js 用の AVA
AVA テスト ランナーで JUnit テストを出力するには、 tap-xunit を指定して TAP レポーターを使用します。
.circleci/config.yml
のテスト用作業セクションは、以下の例のようになります。
steps:
- run:
command: |
yarn add ava tap-xunit --dev # or you could use npm
mkdir -p ~/reports
ava --tap | tap-xunit > ~/reports/ava.xml
when: always
- store_test_results:
path: ~/reports
ESLint
ESLint から JUnit 結果を出力するには、 JUnit フォーマッタを使用します。
.circleci/config.yml
のテスト用作業セクションは、以下の例のようになります。
steps:
- run:
command: |
mkdir -p ~/reports
eslint ./src/ --format junit --output-file ~/reports/eslint.xml
when: always
- store_test_results:
path: ~/reports
Ruby
RSpec
カスタム rspec
ビルドステップを使用するプロジェクトにテストメタデータ コレクションを追加するには、Gemfile に以下の gem を追加します。
gem 'rspec_junit_formatter'
さらに、テスト コマンドを以下のように変更します。
steps:
- checkout
- run: bundle check || bundle install
- run:
command: bundle exec rake test
when: always
- store_test_results:
path: test/reports
Minitest
カスタム minitest
ビルドステップを使用するプロジェクトにテストメタデータ コレクションを追加するには、Gemfile に以下の gem を追加します。
gem 'minitest-ci'
さらに、テスト コマンドを以下のように変更します。
steps:
- checkout
- run: bundle check || bundle install
- run:
command: bundle exec rake test
when: always
- store_test_results:
See the [minitest-ci README](https://github.com/circleci/minitest-ci#readme) for more info.
詳細については、 minitest-ci README を参照してください。
Cucumber
カスタム Cucumber ステップの場合は、JUnit フォーマッタを使用してファイルを生成し、それを cucumber
ディレクトリに書き込む必要があります。 .circleci/config.yml
ファイルに追加するコードの例は以下のとおりです。
steps:
- run:
name: Save test results
command: |
mkdir -p ~/cucumber
bundle exec cucumber --format junit --out ~/cucumber/junit.xml
when: always
- store_test_results:
path: ~/cucumber
path:
は、ファイルが格納されるディレクトリをプロジェクトのルート ディレクトリからの相対ディレクトリで指定します。 CircleCI は、アーティファクトを収集して S3 にアップロードし、アプリケーション内の[Job (ジョブ)] ページの [Artifacts (アーティファクト)] タブに表示します。
または、Cucumber の JSON フォーマッタを使用する場合は、出力ファイルに .cucumber
で終わる名前を付け、それを /cucumber
ディレクトリに書き出します。 例えば下記のようにします。
steps:
- run:
name: Save test results
command: |
mkdir -p ~/cucumber
bundle exec cucumber --format pretty --format json --out ~/cucumber/tests.cucumber
when: always
- store_test_results:
path: ~/cucumber
Python
pytest
pytest
を使用するプロジェクトにテストメタデータを追加するには、JUnit XML を出力するように指定したうえで、テストメタデータを保存します。
- run:
name: run tests
command: |
. venv/bin/activate
mkdir test-results
pytest --junitxml=test-results/junit.xml
- store_test_results:
path: test-results
unittest
unittest は JUnit XML をサポートしていませんが、ほぼすべてのケースで pytest を使って unittest テストを実行することができます。
プロジェクトに pytest を追加すると、以下のようにテスト結果を生成したり、アップロードできるようになります。
- run:
name: run tests
command: |
. venv/bin/activate
mkdir test-results
pytest --junitxml=test-results/junit.xml tests
- store_test_results:
path: test-results
Java
Java JUnit の結果に使用する Maven Surefire プラグイン
Maven ベースのプロジェクトをビルドする場合は、 Maven Surefire プラグインを使用して XML 形式のテスト レポートを生成することがほとんどです。 CircleCI では、これらのレポートを簡単に収集できます。 以下のコードをプロジェクトの .circleci/config.yml
ファイルに追加します。
steps:
- run:
name: Save test results
command: |
mkdir -p ~/test-results/junit/
find . -type f -regex ".*/target/surefire-reports/.*xml" -exec cp {} ~/test-results/junit/ \;
when: always
- store_test_results:
path: ~/test-results
Gradle JUnit のテスト結果
Gradle で Java または Groovy ベースのプロジェクトをビルドする場合は、テストレポートが XML 形式で自動的に生成されます。 CircleCI では、これらのレポートを簡単に収集できます。 以下のコードをプロジェクトの .circleci/config.yml
ファイルに追加します。
steps:
- run:
name: テスト結果の保存
command: |
mkdir -p ~/test-results/junit/
find . -type f -regex ".*/build/test-results/.*xml" -exec cp {} ~/test-results/junit/ \;
when: always
- store_test_results:
path: ~/test-results
PHP
PHPUnit
PHPUnit テストの場合は、--log-junit
コマンドラインオプションを使用してファイルを生成し、それを /phpunit
ディレクトリに書き込む必要があります。 .circleci/config.yml
は以下のようになります。
steps:
- run:
command: |
mkdir -p ~/phpunit
phpunit --log-junit ~/phpunit/junit.xml tests
when: always
- store_test_results:
path: ~/phpunit
.NET
Visual Studio/.NET Core テスト用の trx2junit
Visual Studio または .NET Core で出力される trx ファイルを XML 形式に変換するには、 trx2junit を使用します。
.circleci/config.yml
の作業セクションは、以下のようになります。
steps:
- checkout
- run: dotnet build
- run: dotnet test --no-build --logger "trx"
- run:
name: test results
when: always
command: |
dotnet tool install -g trx2junit
export PATH="$PATH:/root/.dotnet/tools"
trx2junit tests/**/TestResults/*.trx
- store_test_results:
path: tests/TestResults
Clojure
Kaocha
kaocha をテストランナーとして既にご利用の場合、以下を実行してテスト結果を生成および保存してください。
依存関係に kaocha-junit-xml
を追加します。
project.clj
を編集して lambdaisland/kaocha-junit-xml プラグインを追加する、または deps.edn を使用している場合は同様なプラグインを追加します。
(defproject ,,,
:profiles {,,,
:dev {:dependencies [,,,
[lambdaisland/kaocha-junit-xml "0.0.76"]]}})
kaocha の設定ファイルの test.edn
をこのテストレポーターを使用するように編集します。
#kaocha/v1
{:plugins [:kaocha.plugin/junit-xml]
:kaocha.plugin.junit-xml/target-file "junit.xml"}
.circleci/config.yml
に store_test_results ステップを追加します。
version: 2.1
jobs:
build:
docker:
- image: circleci/clojure:tools-deps-1.9.0.394
steps:
- checkout
- run: bin/kaocha
- store_test_results:
path: junit.xml
Clojure テスト用の test2junit
Clojure のテスト出力を XML 形式に変換するには、 test2junit を使用します。 詳細については、 サンプルプロジェクトを参照してください。
API
ジョブのテストメタデータに API からアクセスするには、 テストメタデータ API ドキュメントを参照してください。
関連項目
ビデオ: テストランナーのトラブルシューティング
ドキュメントの改善にご協力ください
このガイドは、CircleCI の他のドキュメントと同様にオープンソースであり、 GitHub でご利用いただけます。 ご協力いただき、ありがとうございます。
- このページの編集をご提案ください (最初に「コントリビューションガイド」をご覧ください)。
- ドキュメントの問題点を報告する、またはフィードバックやコメントを送信するには、GitHub で issue を作成してください。
- CircleCI は、ユーザーの皆様の弊社プラットフォームにおけるエクスペリエンスを向上させる方法を常に模索しています。 フィードバックをお寄せいただける場合は、リサーチコミュニティにご参加ください。
サポートが必要ですか
CircleCI のサポートエンジニアによる、サービスに関する問題、請求およびアカウントについての質問への対応、設定の構築に関する問題解決のサポートを行っています。 サポートチケットを送信して、CircleCI のサポートエンジニアにお問い合わせください。日本語でお問い合わせいただけます。
または、 サポートサイト から、サポート記事やコミュニティフォーラム、トレーニングリソースをご覧いただけます。
CircleCI Documentation by CircleCI is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.