可以通过两种方式为存储库设置代码覆盖率:
- 自动设置: 使用 AI 驱动的代理自动生成工作流。 如果出现以下情况,请选择该选项:
- 你希望在不编写 YAML 配置的情况下快速开始。
- 项目使用常见的测试框架和生成模式。
- 你可以轻松地循环访问 AI 生成的工作流。
- 手动设置: 自行配置 CI 工作流。 如果出现以下情况,请选择该选项:
- 你需要对覆盖过程进行精确控制。
- 你有复杂的 CI 要求(例如专用注册表或自定义生成步骤)。
- 你想要确切地了解覆盖范围的配置方式。
自动配置
可以使用自动设置选项生成工作代码覆盖率工作流,而无需手动创作 CI 配置。 代理分析存储库、标识测试框架,并打开一个拉取请求,其中包含可供评审的覆盖工作流。
注意
自动设置使用 AI 生成工作流文件。 使用此功能无需额外付费。
自动设置的先决条件
- 已经为您的存储库启用Code Quality。 请参阅“启用 GitHub Code Quality”。
- 存储库具有现有的测试套件。
自动生成覆盖工作流
-
在 GitHub 上,导航到存储库的主页面。
-
在仓库名称下,单击 “Settings”****。 如果看不到“设置”选项卡,请选择“”下拉菜单,然后单击“设置”。

-
在边栏中的“安全性”下,单击 代码质量 以显示“代码质量”页。
-
在“代码覆盖率分析”部分中,单击 “设置 ”下拉列表框。
-
在列表中,选择 “使用 AI 生成工作流”。 等待代理分析存储库。 代理将打开草稿拉取请求,并发布其正在执行的步骤的清单。
-
若要查看拉取请求,请单击“ 查看拉取请求”。 在代理完成其工作后查看拉取请求。 拉取请求说明汇总了这些更改,包括任何项目配置更新、工作流文件更改和覆盖率输出设置。
-
如果工作流在 CI 中成功运行并正确上传覆盖率,请合并拉取请求。
如果工作流需要调整,请参阅 自动代码覆盖率设置 以获取有关不同结果以及如何循环访问的指导。
有关代理工作原理和预期内容的详细信息,请参阅 自动代码覆盖率设置。
手动安装
借助内置的代码覆盖率功能,你可以了解测试对代码的覆盖程度,而无需在工具链中引入第三方服务,也无需为此增加预算。 在以下过程中,你将从测试套件生成 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 | 添加 Simple |
| 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]的评论。 注释包括:- 拉取请求分支与默认分支相比的聚合覆盖率百分比。
- 按文件细分,显示哪些文件的覆盖率有所增加或下降。
后续步骤
- 解读结果: 了解拉取请求的覆盖率指标和每个文件的细分。 请参阅“解读存储库的代码质量评估结果”。
- 强制实施覆盖阈值: 阻止不符合最低覆盖率百分比或导致覆盖率下降的拉取请求。 请参阅“为拉取请求设置代码覆盖率阈值”。