注意
GitHub Code Quality 当前正处于 公开预览 阶段,并可能会发生变化。 在 公开预览期间,Code Quality 不会计费,尽管 Code Quality 扫描将使用 GitHub Actions 分钟。
在以下过程中,你将从测试套件生成 Cobertura XML 覆盖率报告,将其 GitHub上传到并查看拉取请求的覆盖率结果。
先决条件
- 已经为您的存储库启用Code Quality。
- 您的代码库有一个在 GitHub Actions 中运行的测试套件。
- 测试框架可以生成 Cobertura XML 格式的覆盖率报告。
步骤 1:生成 Cobertura XML 覆盖率报告
将测试框架配置为以 Cobertura XML 格式输出覆盖率报告。 代码覆盖率适用于可生成此格式的任何编程语言。
- 从下表中找出你的语言对应的覆盖率工具。
- 将适当的命令或配置添加到 CI 工作流,以便在每次测试运行时生成 Cobertura XML 文件。
| 语言 | 框架/工具 | 如何生成 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)。
-
提交并推送该工作流更改。
完整工作流示例
此示例使用 pytest-cov 运行Python测试并上传覆盖率报告:
# 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:查看拉取请求的覆盖率结果
- 创建一个会触发你已配置工作流的拉取请求(或推送到一个现有的拉取请求)。
- 工作流完成后,请在拉取请求中查找来自
github-code-quality[bot]的评论。 注释包括:- 与默认分支相比,拉取请求分支的聚合覆盖率百分比。
- 按文件细分,显示哪些文件的覆盖率有所增加或下降。
后续步骤
- 解读结果: 了解拉取请求中的覆盖率指标和按文件划分的明细。 请参阅“解读存储库的代码质量评估结果”。