Nexgen Software Solution Company

株式会社トレンドソリューションズ
TEL:050-3524-3317

GitLabでJavaのテストレポートとカバレッジの可視化

詳細

Java開発において、eclipseでJunitを実行しカバレッジを確認することは一般的だと思います。GitLab CICDを利用した場合は、テスト結果及びカバレッジの確認はどうすればいいでしょうか?この記事では、.gitlab-ci.ymlの書き方及び結果の確認方法をご紹介したいと思います。

目次

  • pom.xmlにレポート作成に必要なpluginを追記
  • .gitlab-ci.ymlのサンプル
  • テスト結果の確認
  • カバレッジの確認

pom.xmlにレポート作成に必要なpluginを追記

今回のサンプルでは、カバレッジ計算するためのjacoco.xml及び最終的なカバレッジレポートはcobertura.xml形式にするので、それぞれのpluginを追加する必要です。

<build>.<plugins>に以下を追加
<!-- ここから --> 
<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>cobertura-maven-plugin</artifactId>
    <version>2.7</version>
</plugin>
<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>0.8.7</version>
    <executions>
    <execution>
        <goals>
        <goal>prepare-agent</goal>
        </goals>
    </execution>
    <execution>
        <id>report</id>
        <phase>test</phase>
        <goals>
        <goal>report</goal>
        </goals>
    </execution>
    </executions>
</plugin>
<!-- ここまで -->
<project>に以下を追加
<!-- ここから -->
<reporting>
    <plugins>
    <plugin>
        <groupId>org.jacoco</groupId>
        <artifactId>jacoco-maven-plugin</artifactId>
        <reportSets>
        <reportSet>
            <reports>
            <report>report</report>
            </reports>
        </reportSet>
        </reportSets>
    </plugin>
    </plugins>
</reporting>
<!-- ここまで -->

.gitlab-ci.yml

stages:
  - test
  - visualize

test-job:
  stage: test
  image: maven:3.3-jdk-8
  script:
    - mvn verify
  artifacts:
    when: always
    reports:
      junit:
        # テスト結果をGitLab画面に表示させるための元ファイル
        - target/surefire-reports/TEST-*.xml
        - target/failsafe-reports/TEST-*.xml
    paths:

      # カバレッジ計算するためのjacoco.xmlファイルをartifactsに登録し、後続jobに渡す
      - target/site/jacoco/jacoco.xml
  tags:
    - docker-runner

coverage-job:
  # Must be in a stage later than test-job's stage.
  # The `visualize` stage does not exist by default.
  # Please define it first, or choose an existing stage like `deploy`.
  stage: visualize
  image: registry.gitlab.com/haynes/jacoco2cobertura:1.0.7
  variables:
    JACOCO_XML: target/site/jacoco/jacoco.xml
  before_script:
    # for xmllint
    - apk --no-cache add libxml2-utils
  script:
    # convert report from jacoco to cobertura, using relative project path
    - python /opt/cover2cover.py $JACOCO_XML $CI_PROJECT_DIR/src/main/java/ > target/site/cobertura.xml

    # カバレッジ計算用値の取得し、カバレッジを算出(ここではC1分岐網羅)
    - covered=$(xmllint --xpath 'string(/report/counter[@type="BRANCH"]/@covered)' $JACOCO_XML)
    - missed=$(xmllint --xpath 'string(/report/counter[@type="BRANCH"]/@missed)' $JACOCO_XML)
    - coverage=$(awk -vmissed=$missed -vcovered=$covered 'BEGIN{ printf("%.1f\n", covered/(covered+missed)*100 ) }')

    # カバレッジをechoして、GitLabのjob画面とMR画面に表示
    - echo "Test Coverage=${coverage}%"

  # テスト結果を画面に表示するために必要
  coverage: '/Test Coverage=\d+\.\d+/'
  needs: ["test-job"]
  artifacts:
    reports:
      coverage_report:
        coverage_format: cobertura
        path: target/site/cobertura.xml
  tags:
    - docker-runner

テスト結果の確認

pipelin画面にTestsタブにテスト件数が表示され、クリックして次へ。

テスト実行のジョブ一覧が表示され、クリックするとテストケースの一覧が表示されます。

View detailsをクリックすると、詳細情報がポップアップで表示されます。


カバレッジの確認

カバレッジの確認は、以下の画面で確認できます

  • pipelineのカバレッジ出力job画面
  • MR画面
  • AnalyzeのRepository画面(グラフで履歴を表示)

pipelineのカバレッジ出力job画面

MR画面

AnalyzeのRepository画面(グラフで履歴を表示)

以上です。