1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

CodecovでREADMEにテストカバレッジを表示する with Java(Jacoco)

Last updated at Posted at 2025-09-19

これは何?

GitHubで公開しているリポジトリのREADMEにテストカバレッジを表示したくなったのですが、結構ハマったのでメモ代わりにうまくいった手順を記載します。

image.png


環境


うまくいった方法

  1. Codecovのアカウントを作成する

  2. GitHubにCodecovのアプリをインストールする。https://github.com/apps/codecov/installations/select_target

  3. Codecovを使用したいリポジトリを選択してConfigureを押下する。
    Screenshot from 2025-09-19 22-22-43.png

  4. GitHub Actionsを使い、カバレッジを含むファイルをアップロードする。今回の場合はbuild/reports/jacoco/test/jacocoTestReport.xmlが対象になる。
    設定例をもとにした自分のリポジトリでの設定例

    name: Test
    
    on:
      push:
        paths:
          - "src/**/*.java"
          - "build.gradle.kts"
          - "gradle/**"
          - ".github/workflows/*.yaml"
      pull_request:
        paths:
          - "src/**/*.java"
          - "build.gradle.kts"
          - "gradle/**"
    
    jobs:
      test:
        runs-on: ubuntu-latest
        permissions:
          contents: read
          pull-requests: write # Codecovがプルリクエストにコメントするために必要
    
        steps:
        - name: Checkout code
          uses: actions/checkout@v4
          with:
            fetch-depth: 0 # Codecovが正しく動作するために履歴全体を取得
    
        - name: Set up Java 21
          uses: actions/setup-java@v4
          with:
            java-version: '21'
            distribution: 'temurin'
    
        - name: Setup Gradle
          uses: gradle/actions/setup-gradle@v3
    
        - name: Grant execute permission for gradlew
          run: chmod +x gradlew
    
        - name: Run tests with coverage
          run: ./gradlew test jacocoTestReport && ls build/reports/jacoco/test/jacocoTestReport.xml
    
        - name: Upload coverage to Codecov
          uses: codecov/codecov-action@v5
    
        - name: Archive test results
          uses: actions/upload-artifact@v4
          if: always()
          with:
            name: test-results
            path: |
              build/reports/tests/
              build/reports/jacoco/
    
  5. README.mdにCodecovのBadges & Graphsにあるurlをコピペする。
    image.png

ちょっと待つとREADME.mdに付けたバッチのステータスがunknownからカバレッジになる。

image.png


バッチがunknownになる時の対処方

READMEに配置したバッチをクリックした時に以下のような画面に遷移した場合、カバレッジのデータの連携がうまくいっていない

image.png

設定が上手くいっている場合↓

Screenshot from 2025-09-19 22-43-38.png

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?