注意
GitHub Code Quality 当前正处于 公开预览 阶段,并可能会发生变化。 在 公开预览期间,Code Quality 不会计费,尽管 Code Quality 扫描将使用 GitHub Actions 分钟。
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.
- Identify the coverage tool for your language from the table below.
- Add the appropriate command or configuration to your CI workflow so that a Cobertura XML file is generated each time your tests run.
| Language | Framework / Tool | How to generate Cobertura XML |
|---|---|---|
| Python | pytest + pytest-cov | pytest --cov=. --cov-report=xml |
| Java | JaCoCo | Use the cover2cover.py script or the JaCoCo-to-Cobertura Gradle/Maven plugin |
| JavaScript/TypeScript | Istanbul / nyc | nyc report --reporter=cobertura |
| Ruby | SimpleCov | Add SimpleCov::Formatter::CoberturaFormatter |
| Go | go test + gocover-cobertura | go test -coverprofile=cover.out && gocover-cobertura < cover.out > coverage.xml |
提示
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.
-
Open your repository's CI workflow file (for example,
.github/workflows/ci.yml). -
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- 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 -
Replace the following values:
COVERAGE-FILE-PATH.xml: The path to your Cobertura XML report (for example,coverage.xmlortarget/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).
-
Commit and push the workflow change.
Full workflow example
This example runs Python tests with pytest-cov and uploads the coverage report:
# 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
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:
report: 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:
report: coverage.xml
language: Python
label: code-coverage/pytest
Step 3: View coverage results on pull requests
- Open a pull request (or push to an existing one) that triggers the workflow you configured.
- 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
- Interpret results: Understand coverage metrics and per-file breakdowns on your pull requests. See 解读存储库的代码质量评估结果.