$30 off During Our Annual Pro Sale. View Details »

CircleCIでFlakyなテストを再実行する_potatotips#83

 CircleCIでFlakyなテストを再実行する_potatotips#83

kako351

July 21, 2023
Tweet

More Decks by kako351

Other Decks in Technology

Transcript

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  9. 設定①: テスト結果のアップロード
    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

    View Slide

  10. 設定①: テスト結果のアップロード
    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にコピーする

    View Slide

  11. 設定①: テスト結果のアップロード
    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

    View Slide

  12. 設定①: テスト結果のアップロード
    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

    View Slide

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

    View Slide

  14. 設定②: 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

    View Slide

  15. 設定②: 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 コマンドでテストファイルのグロ
    ブします

    View Slide

  16. 設定②: 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オプションでデバッグメッセージが出
    るようにします。

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  20. ご静聴
    ありがとう
    ございました
    20
    potatotips #83 2023/07/21

    kako351@おいしい健康


    View Slide