Slide 1

Slide 1 text

CircleCIでFlakyなテスト を再実行する potatotips #83 kako351@おいしい健康

Slide 2

Slide 2 text

自己紹介 kako351 / @kako_351 おいしい健康 Androidエンジニア Androidエンジニア募集中! ● バイク(ハンターカブ) ● ギター ● コーヒー自宅焙煎 趣味

Slide 3

Slide 3 text

本日話す内容 話すこと ● Circle CIで失敗したテストを再実行する方法 (前提として僕がAndroidエンジニアなのでJUnitでのテストをベースに話します。) 話さないこと ● 他のCI/CDサービスとの比較 ● Flakyなテストを改善していく系の話 Circle CIで失敗したテストのみ再実行できるようになりました

Slide 4

Slide 4 text

実行結果が不安定なテスト。 コードに変更がないのにテストに成功したり失敗したりするテストのこと。 Flaky(フレーキー)なテストとは

Slide 5

Slide 5 text

Circle CIでテストを実行している例 (AndroidのUnitTestの実行例) CircleCIのテストの実行例 - run: name: Run UnitTest command: | ./gradlew testDebugUnitTest

Slide 6

Slide 6 text

テストが失敗した場合の表示例 CircleCIのテストの実行例

Slide 7

Slide 7 text

CircleCIのテストの実行例 この状態で右上のRerunのメニューを開くとい くつか選択肢があります。 デフォルトでは「Rerun failed tests」が選べな いようになっています。

Slide 8

Slide 8 text

設定①: テスト結果のアップロード 失敗したテストを再実行するためにはテスト結果をアップロードする必要が あります。 ● store_artifactsを使用する方法 ● store_test_resultsを使用する方法 の2つがあります

Slide 9

Slide 9 text

設定①: テスト結果のアップロード store_artifactsを使用する方法 - run: name: Save test results command: | mkdir -p ~/test-results/junit/ find . -type f -regex ".*/build/test-results/.*xml" -exec cp {} ~/test-results/junit/ \; when: always - store_artifacts: path: ~/test-results destination: circleci-docs

Slide 10

Slide 10 text

設定①: テスト結果のアップロード store_artifactsを使用する方法 - run: name: Save test results command: | mkdir -p ~/test-results/junit/ find . -type f -regex ".*/build/test-results/.*xml" -exec cp {} ~/test-results/junit/ \; when: always - store_artifacts: path: ~/test-results destination: circleci-docs JUnitのテスト結果はそれぞれの モジュール内にxmlファイルが作られるのでま とめて ~/test-resultsにコピーする

Slide 11

Slide 11 text

設定①: テスト結果のアップロード store_artifactsを使用する方法 - run: name: Save test results command: | mkdir -p ~/test-results/junit/ find . -type f -regex ".*/build/test-results/.*xml" -exec cp {} ~/test-results/junit/ \; when: always - store_artifacts: path: ~/test-results destination: circleci-docs

Slide 12

Slide 12 text

設定①: テスト結果のアップロード store_test_resultsを使用する方法 - run: name: Save test results 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

Slide 13

Slide 13 text

設定①: テスト結果のアップロード store_artifacts store_test_results

Slide 14

Slide 14 text

設定②: circleci tests runを使ってテスト実行 次にcircleci tests runを使ってテストを実行する必要があります。 - run: name: Run unit test command: | set -e TEST_FILES=$(circleci tests glob " **/src/test/java/*.kt") echo "$TEST_FILES" | circleci tests run \ --command="./gradlew testDebugUnitTest" \ --verbose --split-by=timings --timings-type=classname

Slide 15

Slide 15 text

設定②: circleci tests runを使ってテスト実行 次にcircleci tests runを使ってテストを実行する必要があります。 - run: name: Run unit test command: | set -e TEST_FILES=$(circleci tests glob " **/src/test/java/*.kt") echo "$TEST_FILES" | circleci tests run \ --command="./gradlew testDebugUnitTest" \ --verbose --split-by=timings --timings-type=classname circleci tests コマンドでテストファイルのグロ ブします

Slide 16

Slide 16 text

設定②: circleci tests runを使ってテスト実行 次にcircleci tests runを使ってテストを実行する必要があります。 - run: name: Run unit test command: | set -e TEST_FILES=$(circleci tests glob " **/src/test/java/*.kt") echo "$TEST_FILES" | circleci tests run \ --command="./gradlew testDebugUnitTest" \ --verbose circleci tests run のcommandオプションに テストの実行コマンドを指定します。 verboseオプションでデバッグメッセージが出 るようにします。

Slide 17

Slide 17 text

失敗したテストの再実行 設定が完了したのでこの状態でFlakyなテストを 実行してみます。 テストが失敗すると「Rerun failed tests」が選 べるようになっています。 失敗したテストのみを再実行できるので時間の 短縮や消費クレジットの削減になります。

Slide 18

Slide 18 text

おまけ CircleCIでは store_test_resultsにテスト結果をアップしているとFlakyなテス トがある場合にラベルが付きます。 またテストInsightがありFlakyなテストを一括でチェックすることができます。

Slide 19

Slide 19 text

参考資料 ● https://circleci.com/ja/blog/rerun-failed-flaky-tests/ ● https://circleci.com/docs/ja/insights-tests/

Slide 20

Slide 20 text

ご静聴 ありがとう ございました 20 potatotips #83 2023/07/21 
 kako351@おいしい健康