Skip to main content

为存储库设置代码覆盖率

上传测试覆盖率报告,直接查看拉取请求的覆盖率结果,帮助审阅者在合并之前识别未经测试的代码。

谁可以使用此功能?

具有 管理员 角色的存储库所有者、组织所有者和用户

GitHub Team 或 GitHub Enterprise Cloud

注意

GitHub Code Quality 当前正处于 公开预览 阶段,并可能会发生变化。 在 公开预览期间,Code Quality 不会计费,尽管 Code Quality 扫描将使用 GitHub Actions 分钟。

在以下过程中,你将从测试套件生成 Cobertura XML 覆盖率报告,将其 GitHub上传到并查看拉取请求的覆盖率结果。

先决条件

  • 已经为您的存储库启用Code Quality。
  • 您的代码库有一个在 GitHub Actions 中运行的测试套件。
  • 测试框架可以生成 Cobertura XML 格式的覆盖率报告。

步骤 1:生成 Cobertura XML 覆盖率报告

将测试框架配置为以 Cobertura XML 格式输出覆盖率报告。 代码覆盖率适用于可生成此格式的任何编程语言。

  1. 从下表中找出你的语言对应的覆盖率工具。
  2. 将适当的命令或配置添加到 CI 工作流,以便在每次测试运行时生成 Cobertura XML 文件。
语言框架/工具如何生成 Cobertura XML
Pythonpytest + pytest-covpytest --cov=. --cov-report=xml
JavaJaCoCo使用 cover2cover.py 脚本或 JaCoCo-to-Cobertura Gradle/Maven 插件
JavaScript/TypeScript伊斯坦布尔/ nycnyc report --reporter=cobertura
RubySimpleCov添加 SimpleCov::Formatter::CoberturaFormatter
Gogo test + gocover-coberturago test -coverprofile=cover.out && gocover-cobertura < cover.out > coverage.xml

提示

如果上面未列出框架,请查看其文档以获取 Cobertura 输出支持。 许多工具可以直接支持它,也可以从其他格式转换为 Cobertura XML。

步骤 2:上传覆盖范围报告

测试生成 Cobertura XML 报告后,将其上传到 GitHub,以便在拉取请求中显示覆盖率结果。

  1. 打开存储库的 CI 工作流文件(例如 .github/workflows/ci.yml)。

  2. 在运行测试并生成覆盖报告的步骤后添加以下步骤:

    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
    
  3. 请替换以下值:

    • COVERAGE-FILE-PATH.xml:Cobertura XML 报表的路径(例如, coverage.xmltarget/site/jacoco/cobertura.xml)。
    • LANGUAGE:所涵盖代码的主要语言(例如,PythonJavaJavaScript)。
    • LABEL:用于标识此覆盖报表的可选标签(例如 code-coverage/pytest)。
  4. 提交并推送该工作流更改。

完整工作流示例

此示例使用 pytest-cov 运行Python测试并上传覆盖率报告:

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

步骤 3:查看拉取请求的覆盖率结果

  1. 创建一个会触发你已配置工作流的拉取请求(或推送到一个现有的拉取请求)。
  2. 工作流完成后,请在拉取请求中查找来自 github-code-quality[bot] 的评论。 注释包括:
    • 与默认分支相比,拉取请求分支的聚合覆盖率百分比。
    • 按文件细分,显示哪些文件的覆盖率有所增加或下降。

后续步骤