商城首页欢迎来到中国正版软件门户

您的位置:首页 >GitHub 展示 Python 代码覆盖率方法

GitHub 展示 Python 代码覆盖率方法

  发布于2025-11-12 阅读(0)

扫一扫,手机访问

在 GitHub 中展示 Python 项目代码覆盖率

本文将介绍如何配置 GitHub Actions,以便在每次推送代码时自动生成并展示 Python 项目的代码覆盖率报告。我们将使用 pytest-cov 工具来生成覆盖率数据,并通过简单的配置修改,使其在 GitHub 上可见。

使用 pytest-cov 生成代码覆盖率报告

要在 GitHub 中展示 Python 项目的代码覆盖率,首先需要一个能够生成覆盖率报告的工具。pytest-cov 是一个流行的选择,它与 pytest 测试框架集成良好,可以方便地生成代码覆盖率数据。

要使用 pytest-cov,需要先安装它:

pip install pytest pytest-cov

安装完成后,就可以在运行 pytest 时添加 --cov 选项来生成覆盖率报告。例如,如果你的测试文件位于 tests/ 目录下,可以这样运行测试:

pytest --cov tests/

这将会运行你的测试,并生成覆盖率数据,但这些数据默认只会在命令行中显示。为了在 GitHub 中展示这些数据,我们需要进一步的配置。

配置 GitHub Actions

GitHub Actions 允许你在 GitHub 仓库中自动化工作流程,包括运行测试、构建项目和部署代码等。我们可以利用 GitHub Actions 在每次推送代码时自动运行测试并生成覆盖率报告。

首先,确保你的项目根目录下有一个 .github/workflows/ 目录,并在其中创建一个 YAML 文件(例如 github-actions.yaml)来定义你的工作流程。

一个基本的 Python 项目 CI 工作流程可能如下所示:

name: Python CI

on: [push]

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v4
    - name: Set up Python
      uses: actions/setup-python@v4
      with:
        python-version: '3.11'
    - name: Install dependencies
      run: |
        python -m pip install --upgrade pip
        pip install -r requirements.txt
    - name: Test with pytest
      run: |
        pip install pytest pytest-cov
        pytest tests/

要在这个工作流程中添加代码覆盖率报告功能,我们需要修改 Test with pytest 步骤,将 pytest tests/ 命令替换为 pytest --cov tests/。

修改后的 YAML 文件如下所示:

name: Python CI

on: [push]

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v4
    - name: Set up Python
      uses: actions/setup-python@v4
      with:
        python-version: '3.11'
    - name: Install dependencies
      run: |
        python -m pip install --upgrade pip
        pip install -r requirements.txt
    - name: Test with pytest
      run: |
        pip install pytest pytest-cov
        pytest --cov tests/

现在,每次你推送代码到 GitHub 仓库时,GitHub Actions 都会自动运行测试并生成代码覆盖率数据。

将覆盖率报告上传到 Codecov 或类似服务

虽然我们已经生成了覆盖率数据,但它仍然只存在于 GitHub Actions 的运行环境中。为了在 GitHub 中更方便地查看和管理这些数据,我们可以将覆盖率报告上传到 Codecov 或类似的覆盖率服务。

Codecov 是一个流行的代码覆盖率服务,它可以将覆盖率数据可视化,并提供各种分析和报告功能。

要将覆盖率报告上传到 Codecov,首先需要在 Codecov 上注册一个账号,并为你的项目创建一个仓库。然后,需要在 GitHub Actions 工作流程中添加一个步骤,将覆盖率报告上传到 Codecov。

首先安装 codecov:

pip install codecov

接下来,在 GitHub Actions 中添加以下步骤:

    - name: Upload coverage to Codecov
      uses: codecov/codecov-action@v3
      with:
        token: ${{ secrets.CODECOV_TOKEN }} # Optional: Secrets required for private repos
        fail_ci_if_error: true # Optional: Make the job fail when Codecov detects errors

需要注意的是,你需要将 CODECOV_TOKEN 替换为你在 Codecov 上生成的访问令牌。你可以在 GitHub 仓库的 Settings -> Secrets -> Actions 中添加一个名为 CODECOV_TOKEN 的 Secret,并将你的访问令牌设置为其值。

完整的 YAML 文件如下所示:

name: Python CI

on: [push]

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v4
    - name: Set up Python
      uses: actions/setup-python@v4
      with:
        python-version: '3.11'
    - name: Install dependencies
      run: |
        python -m pip install --upgrade pip
        pip install -r requirements.txt
    - name: Test with pytest
      run: |
        pip install pytest pytest-cov
        pytest --cov tests/
    - name: Upload coverage to Codecov
      uses: codecov/codecov-action@v3
      with:
        token: ${{ secrets.CODECOV_TOKEN }} # Optional: Secrets required for private repos
        fail_ci_if_error: true # Optional: Make the job fail when Codecov detects errors

现在,每次你推送代码到 GitHub 仓库时,GitHub Actions 都会自动运行测试,生成代码覆盖率数据,并将覆盖率报告上传到 Codecov。你可以在 Codecov 上查看你的项目的覆盖率报告,并将其集成到你的 GitHub 仓库中,例如在 Pull Request 中显示覆盖率变化。

注意事项

  • 确保你的测试覆盖了尽可能多的代码,以获得更准确的覆盖率报告。
  • 定期检查你的覆盖率报告,并根据需要调整你的测试策略。
  • Codecov 提供免费和付费计划,你可以根据你的需求选择合适的计划。
  • 除了 Codecov,还有其他的代码覆盖率服务可供选择,例如 Coveralls 和 SonarQube。

总结

通过使用 pytest-cov 和 GitHub Actions,我们可以方便地在 GitHub 中展示 Python 项目的代码覆盖率报告。这可以帮助我们更好地了解代码的测试覆盖情况,并及时发现潜在的问题。 结合 Codecov 等覆盖率服务,可以更有效地管理和利用这些数据,提高代码质量。

本文转载于:互联网 如有侵犯,请联系zhengruancom@outlook.com删除。
免责声明:正软商城发布此文仅为传递信息,不代表正软商城认同其观点或证实其描述。

热门关注