|
| 1 | +--- |
| 2 | +title: Setting up code coverage for your repository |
| 3 | +shortTitle: Set up code coverage |
| 4 | +intro: 'Upload test coverage reports to see coverage results directly on pull requests, helping reviewers identify untested code before merging.' |
| 5 | +versions: |
| 6 | + feature: code-quality |
| 7 | +product: '{% data reusables.gated-features.code-quality-availability %}' |
| 8 | +permissions: '{% data reusables.permissions.code-quality-repo-enable %}' |
| 9 | +contentType: how-tos |
| 10 | +layout: inline |
| 11 | +category: |
| 12 | + - Improve code quality |
| 13 | +--- |
| 14 | + |
| 15 | +{% data reusables.code-quality.code-quality-preview-note %} |
| 16 | + |
| 17 | +In the following procedures, you will generate a Cobertura XML coverage report from your test suite, upload it to {% data variables.product.github %}, and view the coverage results on your pull requests. |
| 18 | + |
| 19 | +## Prerequisites |
| 20 | + |
| 21 | +* {% data variables.product.prodname_code_quality_short %} is enabled for your repository. |
| 22 | +* Your repository has a test suite that runs in {% data variables.product.prodname_actions %}. |
| 23 | +* Your test framework can produce a coverage report in **Cobertura XML** format. |
| 24 | + |
| 25 | +## Step 1: Generate a Cobertura XML coverage report |
| 26 | + |
| 27 | +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. |
| 28 | + |
| 29 | +1. Identify the coverage tool for your language from the table below. |
| 30 | +1. Add the appropriate command or configuration to your CI workflow so that a Cobertura XML file is generated each time your tests run. |
| 31 | + |
| 32 | +| Language | Framework / Tool | How to generate Cobertura XML | |
| 33 | +|----------|-----------------|-------------------------------| |
| 34 | +| Python | `pytest` + `pytest-cov` | `pytest --cov=. --cov-report=xml` | |
| 35 | +| Java | JaCoCo | Use the `cover2cover.py` script or the JaCoCo-to-Cobertura Gradle/Maven plugin | |
| 36 | +| JavaScript/TypeScript | Istanbul / `nyc` | `nyc report --reporter=cobertura` | |
| 37 | +| Ruby | SimpleCov | Add `SimpleCov::Formatter::CoberturaFormatter` | |
| 38 | +| Go | `go test` + `gocover-cobertura` | `go test -coverprofile=cover.out && gocover-cobertura < cover.out > coverage.xml` | |
| 39 | + |
| 40 | +> [!TIP] |
| 41 | +> 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. |
| 42 | +
|
| 43 | +## Step 2: Upload the coverage report |
| 44 | + |
| 45 | +After your tests generate a Cobertura XML report, upload it to {% data variables.product.github %} so coverage results appear on pull requests. |
| 46 | + |
| 47 | +1. Open your repository's CI workflow file (for example, `.github/workflows/ci.yml`). |
| 48 | +1. Add the following step after the step that runs your tests and generates the coverage report: |
| 49 | + |
| 50 | + ```yaml copy |
| 51 | + - name: Upload coverage report |
| 52 | + if: {% raw %}github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.repository{% endraw %} |
| 53 | + uses: actions/upload-code-coverage@v1 |
| 54 | + with: |
| 55 | + report: COVERAGE-FILE-PATH.xml |
| 56 | + language: LANGUAGE |
| 57 | + label: LABEL |
| 58 | + ``` |
| 59 | +
|
| 60 | +1. Replace the following values: |
| 61 | + * **`COVERAGE-FILE-PATH.xml`**: The path to your Cobertura XML report (for example, `coverage.xml` or `target/site/jacoco/cobertura.xml`). |
| 62 | + * **`LANGUAGE`**: The primary language of the code being covered (for example, `Python`, `Java`, `JavaScript`). |
| 63 | + * **`LABEL`**: An optional label to identify this coverage report (for example, `code-coverage/pytest`). |
| 64 | +1. Commit and push the workflow change. |
| 65 | + |
| 66 | +### Full workflow example |
| 67 | + |
| 68 | +This example runs Python tests with `pytest-cov` and uploads the coverage report: |
| 69 | + |
| 70 | +```yaml annotate copy |
| 71 | +# This workflow runs your test suite, generates a Cobertura XML coverage report, and uploads it to {% data variables.product.github %}. Once this workflow is committed, coverage results appear automatically on every pull request. |
| 72 | +name: Code Coverage |
| 73 | +
|
| 74 | +# Run on pushes to the default branch (to establish the baseline) and on pull requests (to compare against it). {% data variables.product.prodname_code_quality_short %} compares PR branch coverage to the default branch, so both triggers are needed. |
| 75 | +on: |
| 76 | + push: |
| 77 | + branches: [main] |
| 78 | + pull_request: |
| 79 | + branches: [main] |
| 80 | +
|
| 81 | +# The `code-quality: write` permission is required to upload coverage data. No other elevated permissions are needed. |
| 82 | +permissions: |
| 83 | + contents: read |
| 84 | + code-quality: write |
| 85 | + |
| 86 | +jobs: |
| 87 | + test: |
| 88 | + runs-on: ubuntu-latest |
| 89 | + steps: |
| 90 | + # Check out the PR head commit (not the merge commit) so coverage line numbers map correctly to the diff. |
| 91 | + - uses: {% data reusables.actions.action-checkout %} |
| 92 | + with: |
| 93 | + ref: {% raw %}${{ github.event.pull_request.head.sha || github.sha }}{% endraw %} |
| 94 | + |
| 95 | + # 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. |
| 96 | + - uses: {% data reusables.actions.action-setup-python %} |
| 97 | + with: |
| 98 | + python-version: "3.x" |
| 99 | + |
| 100 | + - name: Install dependencies |
| 101 | + run: | |
| 102 | + python -m pip install --upgrade pip |
| 103 | + pip install -r requirements.txt |
| 104 | + pip install pytest pytest-cov |
| 105 | +
|
| 106 | + # 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. |
| 107 | + - name: Run tests with coverage |
| 108 | + run: pytest --cov=. --cov-report=xml |
| 109 | + |
| 110 | + # This step replaces any third-party coverage upload (Codecov, Coveralls, etc.). After this runs, the `{% data variables.code-quality.pr_commenter %}` bot posts a coverage summary directly on the pull request. |
| 111 | + - name: Upload coverage report |
| 112 | + if: {% raw %}github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.repository{% endraw %} |
| 113 | + uses: actions/upload-code-coverage@v1 |
| 114 | + with: |
| 115 | + report: coverage.xml |
| 116 | + language: Python |
| 117 | + label: code-coverage/pytest |
| 118 | +``` |
| 119 | +
|
| 120 | +## Step 3: View coverage results on pull requests |
| 121 | +
|
| 122 | +1. Open a pull request (or push to an existing one) that triggers the workflow you configured. |
| 123 | +1. After the workflow completes, look for a comment from `{% data variables.code-quality.pr_commenter %}` on the pull request. The comment includes: |
| 124 | + * The aggregate coverage percentage for the pull request branch compared to the default branch. |
| 125 | + * A per-file breakdown showing which files gained or lost coverage. |
| 126 | + |
| 127 | +## Next steps |
| 128 | + |
| 129 | +* **Interpret results:** Understand coverage metrics and per-file breakdowns on your pull requests. See [AUTOTITLE](/code-security/how-tos/maintain-quality-code/interpret-results). |
0 commit comments