Skip to main content

リポジトリのコード カバレッジの設定

テスト カバレッジ レポートをアップロードすると、プル要求でカバレッジの結果が直接表示され、レビュー担当者はマージ前にテストされていないコードを特定できます。

この機能を使用できるユーザーについて

リポジトリの所有者、組織の所有者、および 管理者 ロールを持つユーザー

GitHub Team または GitHub Enterprise Cloud

メモ

GitHub Code Quality は現在 パブリック プレビュー にあり、変更される可能性があります。 パブリック プレビュー の間、Code Quality は課金されませんが、Code Quality スキャンでは GitHub Actions 分が消費されます。

次の手順では、テスト スイートから Cobertura XML カバレッジ レポートを生成し、それを GitHubにアップロードして、プル要求のカバレッジ結果を表示します。

前提条件

  • Code Quality はリポジトリに対して有効になっています。
  • リポジトリには、 GitHub Actionsで実行されるテスト スイートがあります。
  • テスト フレームワークでは、 Cobertura XML 形式でカバレッジ レポートを生成できます。

手順 1: Cobertura XML カバレッジ レポートを生成する

Cobertura XML 形式でカバレッジ レポートを出力するようにテスト フレームワークを構成します。 コード カバレッジは、この形式を生成できる任意のプログラミング言語で動作します。

  1. 以下の表から、ご使用の言語に対応するカバレッジ ツールを特定してください。
  2. テストを実行するたびに Cobertura XML ファイルが生成されるように、CI ワークフローに適切なコマンドまたは構成を追加します。
Languageフレームワーク/ツールCobertura XML を生成する方法
Pythonpytest + pytest-covpytest --cov=. --cov-report=xml
JavaJaCoCo
cover2cover.py スクリプトまたは JaCoCo-to-Cobertura Gradle/Maven プラグインを使用する
JavaScript/TypeScriptイスタンブール/ nycnyc report --reporter=cobertura
RubySimpleCov
SimpleCov::Formatter::CoberturaFormatterを追加する
Gogo test + gocover-coberturago test -coverprofile=cover.out && gocover-cobertura < cover.out > coverage.xml

ヒント

フレームワークが上記の一覧にない場合は、Cobertura 出力のサポートに関するドキュメントを確認してください。 多くのツールで直接サポートされているか、他の形式から Cobertura XML に変換できます。

手順 2: カバレッジ レポートをアップロードする

テストで Cobertura XML レポートが生成されたら、それを GitHub にアップロードして、プル要求にカバレッジの結果が表示されるようにします。

  1. リポジトリの CI ワークフロー ファイル (たとえば、 .github/workflows/ci.yml) を開きます。

  2. テストを実行し、カバレッジ レポートを生成する手順の後に、次の手順を追加します。

    YAML
    - name: Upload coverage report
      if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.repository
      uses: actions/upload-code-coverage@v1
      with:
        file: COVERAGE-FILE-PATH.xml
        language: LANGUAGE
        label: LABEL
    
  3. 次の値を置き換えます。

    • COVERAGE-FILE-PATH.xml: Cobertura XML レポートへのパス (たとえば、 coverage.xml または target/site/jacoco/cobertura.xml)。
    • LANGUAGE: 対象となるコードの主な言語 (例: PythonJavaJavaScript)。
    • LABEL: このカバレッジレポートを識別するためのオプションのラベル (たとえば code-coverage/pytest)。
  4. ワークフローの変更をコミットしてプッシュします。

完全なワークフローの例

この例では、Python pytest-cov を使用してテストを実行し、カバレッジ レポートをアップロードします。

YAML
name: Code Coverage

This workflow runs your test suite, generates a Cobertura XML coverage report, and uploads it to GitHub. Once this workflow is committed, coverage results appear automatically on every pull request.

on:
  push:
    branches: [main]
  pull_request:
    branches: [main]

Run on pushes to the default branch (to establish the baseline) and on pull requests (to compare against it). Code Quality compares PR branch coverage to the default branch, so both triggers are needed.

permissions:
  contents: read
  code-quality: write
jobs:
  test:
    runs-on: ubuntu-latest
    steps:

The code-quality: write permission is required to upload coverage data. No other elevated permissions are needed.

      - uses: actions/checkout@v6
        with:
          ref: ${{ github.event.pull_request.head.sha || github.sha }}

Check out the PR head commit (not the merge commit) so coverage line numbers map correctly to the diff.

      - uses: actions/setup-python@v5
        with:
          python-version: "3.x"
      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          pip install -r requirements.txt
          pip install pytest pytest-cov

Replace this step with whatever language setup your project uses (Node.js, Java, Go, etc.). The upload action works with any language that produces a Cobertura XML report.

      - name: Run tests with coverage
        run: pytest --cov=. --cov-report=xml

Adapt this step for your test framework. The key requirement is producing a Cobertura XML file. For other languages, see the framework table earlier in this article.

      - name: Upload coverage report
        if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.repository
        uses: actions/upload-code-coverage@v1
        with:
          file: coverage.xml
          language: Python
          label: code-coverage/pytest

This step replaces any third-party coverage upload (Codecov, Coveralls, etc.). After this runs, the github-code-quality[bot] bot posts a coverage summary directly on the pull request.

# This workflow runs your test suite, generates a Cobertura XML coverage report, and uploads it to GitHub. Once this workflow is committed, coverage results appear automatically on every pull request.
name: Code Coverage

# Run on pushes to the default branch (to establish the baseline) and on pull requests (to compare against it). Code Quality compares PR branch coverage to the default branch, so both triggers are needed.
on:
  push:
    branches: [main]
  pull_request:
    branches: [main]

# The `code-quality: write` permission is required to upload coverage data. No other elevated permissions are needed.
permissions:
  contents: read
  code-quality: write

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      # Check out the PR head commit (not the merge commit) so coverage line numbers map correctly to the diff.
      - uses: actions/checkout@v6
        with:
          ref: ${{ github.event.pull_request.head.sha || github.sha }}

      # Replace this step with whatever language setup your project uses (Node.js, Java, Go, etc.). The upload action works with any language that produces a Cobertura XML report.
      - uses: actions/setup-python@v5
        with:
          python-version: "3.x"

      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          pip install -r requirements.txt
          pip install pytest pytest-cov

      # Adapt this step for your test framework. The key requirement is producing a Cobertura XML file. For other languages, see the framework table earlier in this article.
      - name: Run tests with coverage
        run: pytest --cov=. --cov-report=xml

      # This step replaces any third-party coverage upload (Codecov, Coveralls, etc.). After this runs, the `github-code-quality[bot]` bot posts a coverage summary directly on the pull request.
      - name: Upload coverage report
        if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.repository
        uses: actions/upload-code-coverage@v1
        with:
          file: coverage.xml
          language: Python
          label: code-coverage/pytest

手順 3: プル リクエストのカバレッジ結果を表示する

  1. 設定したワークフローがトリガーされるプル リクエストを作成するか、既存のプル リクエストにプッシュします。
  2. ワークフローが完了したら、pull request の github-code-quality[bot] からコメントを探します。 コメントには次のものが含まれます。
    • 既定のブランチと比較した場合の、プル リクエスト ブランチの集計されたカバレッジ率。
    • どのファイルでカバレッジが増減したかを示すファイルごとの内訳。

次のステップ