メモ
GitHub Code Quality は現在 パブリック プレビュー にあり、変更される可能性があります。 パブリック プレビュー の間、Code Quality は課金されませんが、Code Quality スキャンでは GitHub Actions 分が消費されます。
次の手順では、テスト スイートから Cobertura XML カバレッジ レポートを生成し、それを GitHubにアップロードして、プル要求のカバレッジ結果を表示します。
前提条件
- Code Quality はリポジトリに対して有効になっています。
- リポジトリには、 GitHub Actionsで実行されるテスト スイートがあります。
- テスト フレームワークでは、 Cobertura XML 形式でカバレッジ レポートを生成できます。
手順 1: Cobertura XML カバレッジ レポートを生成する
Cobertura XML 形式でカバレッジ レポートを出力するようにテスト フレームワークを構成します。 コード カバレッジは、この形式を生成できる任意のプログラミング言語で動作します。
- 以下の表から、ご使用の言語に対応するカバレッジ ツールを特定してください。
- テストを実行するたびに Cobertura XML ファイルが生成されるように、CI ワークフローに適切なコマンドまたは構成を追加します。
| Language | フレームワーク/ツール | Cobertura XML を生成する方法 |
|---|---|---|
| Python | pytest + pytest-cov | pytest --cov=. --cov-report=xml |
| Java | JaCoCo | |
cover2cover.py スクリプトまたは JaCoCo-to-Cobertura Gradle/Maven プラグインを使用する | ||
| JavaScript/TypeScript | イスタンブール/ nyc | nyc report --reporter=cobertura |
| Ruby | SimpleCov | |
SimpleCov::Formatter::CoberturaFormatterを追加する | ||
| Go | go test + gocover-cobertura | go test -coverprofile=cover.out && gocover-cobertura < cover.out > coverage.xml |
ヒント
フレームワークが上記の一覧にない場合は、Cobertura 出力のサポートに関するドキュメントを確認してください。 多くのツールで直接サポートされているか、他の形式から Cobertura XML に変換できます。
手順 2: カバレッジ レポートをアップロードする
テストで Cobertura XML レポートが生成されたら、それを GitHub にアップロードして、プル要求にカバレッジの結果が表示されるようにします。
-
リポジトリの CI ワークフロー ファイル (たとえば、
.github/workflows/ci.yml) を開きます。 -
テストを実行し、カバレッジ レポートを生成する手順の後に、次の手順を追加します。
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- 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 -
次の値を置き換えます。
COVERAGE-FILE-PATH.xml: Cobertura XML レポートへのパス (たとえば、coverage.xmlまたはtarget/site/jacoco/cobertura.xml)。LANGUAGE: 対象となるコードの主な言語 (例:Python、Java、JavaScript)。LABEL: このカバレッジレポートを識別するためのオプションのラベル (たとえばcode-coverage/pytest)。
-
ワークフローの変更をコミットしてプッシュします。
完全なワークフローの例
この例では、Python pytest-cov を使用してテストを実行し、カバレッジ レポートをアップロードします。
# 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
name: Code CoverageThis 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-covReplace 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=xmlAdapt 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/pytestThis 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: プル リクエストのカバレッジ結果を表示する
- 設定したワークフローがトリガーされるプル リクエストを作成するか、既存のプル リクエストにプッシュします。
- ワークフローが完了したら、pull request の
github-code-quality[bot]からコメントを探します。 コメントには次のものが含まれます。- 既定のブランチと比較した場合の、プル リクエスト ブランチの集計されたカバレッジ率。
- どのファイルでカバレッジが増減したかを示すファイルごとの内訳。
次のステップ
- 結果を解釈する: プル要求のカバレッジ メトリックとファイルごとの内訳について理解します。 「リポジトリのコード品質結果の解釈」を参照してください。