Skip to main content

Setting up code coverage for your repository

Upload test coverage reports to see coverage results directly on pull requests, helping reviewers identify untested code before merging.

Qui peut utiliser cette fonctionnalité ?

Propriétaires de référentiels, propriétaires d’organisations et utilisateurs avec le rôle d’administrateur

GitHub Team ou GitHub Enterprise Cloud

Remarque

GitHub Code Quality est actuellement dans préversion publique et peut être modifié. Pendant préversion publique, Code Quality ne sera pas facturé, bien que les analyses Code Quality consomment GitHub Actions minutes.

In the following procedures, you will generate a Cobertura XML coverage report from your test suite, upload it to GitHub, and view the coverage results on your pull requests.

Prerequisites

  • Code Quality is enabled for your repository.
  • Your repository has a test suite that runs in GitHub Actions.
  • Your test framework can produce a coverage report in Cobertura XML format.

Step 1: Generate a Cobertura XML coverage report

Configure your test framework to output a coverage report in the Cobertura XML format. Code coverage works with any programming language that can produce this format.

  1. Identify the coverage tool for your language from the table below.
  2. Add the appropriate command or configuration to your CI workflow so that a Cobertura XML file is generated each time your tests run.
LanguageFramework / ToolHow to generate Cobertura XML
Pythonpytest + pytest-covpytest --cov=. --cov-report=xml
JavaJaCoCoUse the cover2cover.py script or the JaCoCo-to-Cobertura Gradle/Maven plugin
JavaScript/TypeScriptIstanbul / nycnyc report --reporter=cobertura
RubySimpleCovAdd SimpleCov::Formatter::CoberturaFormatter
Gogo test + gocover-coberturago test -coverprofile=cover.out && gocover-cobertura < cover.out > coverage.xml

Conseil

If your framework isn't listed above, check its documentation for Cobertura output support. Many tools either support it directly or can convert to Cobertura XML from other formats.

Step 2: Upload the coverage report

After your tests generate a Cobertura XML report, upload it to GitHub so coverage results appear on pull requests.

  1. Open your repository's CI workflow file (for example, .github/workflows/ci.yml).

  2. Add the following step after the step that runs your tests and generates the coverage report:

    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:
        report: COVERAGE-FILE-PATH.xml
        language: LANGUAGE
        label: LABEL
    
  3. Replace the following values:

    • COVERAGE-FILE-PATH.xml: The path to your Cobertura XML report (for example, coverage.xml or target/site/jacoco/cobertura.xml).
    • LANGUAGE: The primary language of the code being covered (for example, Python, Java, JavaScript).
    • LABEL: An optional label to identify this coverage report (for example, code-coverage/pytest).
  4. Commit and push the workflow change.

Full workflow example

This example runs Python tests with pytest-cov and uploads the coverage report:

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:
          report: 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:
          report: coverage.xml
          language: Python
          label: code-coverage/pytest

Step 3: View coverage results on pull requests

  1. Open a pull request (or push to an existing one) that triggers the workflow you configured.
  2. After the workflow completes, look for a comment from github-code-quality[bot] on the pull request. The comment includes:
    • The aggregate coverage percentage for the pull request branch compared to the default branch.
    • A per-file breakdown showing which files gained or lost coverage.

Next steps