
本文将介绍如何配置 GitHub Actions,以便在每次推送代码时自动生成并展示 Python 项目的代码覆盖率报告。我们将使用 pytest-cov 工具来生成覆盖率数据,并通过简单的配置修改,使其在 GitHub 上可见。
要在 GitHub 中展示 Python 项目的代码覆盖率,首先需要一个能够生成覆盖率报告的工具。pytest-cov 是一个流行的选择,它与 pytest 测试框架集成良好,可以方便地生成代码覆盖率数据。
要使用 pytest-cov,需要先安装它:
pip install pytest pytest-cov
安装完成后,就可以在运行 pytest 时添加 --cov 选项来生成覆盖率报告。例如,如果你的测试文件位于 tests/ 目录下,可以这样运行测试:
立即学习“Python免费学习笔记(深入)”;
pytest --cov tests/
这将会运行你的测试,并生成覆盖率数据,但这些数据默认只会在命令行中显示。为了在 GitHub 中展示这些数据,我们需要进一步的配置。
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 都会自动运行测试并生成代码覆盖率数据。
虽然我们已经生成了覆盖率数据,但它仍然只存在于 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 中显示覆盖率变化。
通过使用 pytest-cov 和 GitHub Actions,我们可以方便地在 GitHub 中展示 Python 项目的代码覆盖率报告。这可以帮助我们更好地了解代码的测试覆盖情况,并及时发现潜在的问题。 结合 Codecov 等覆盖率服务,可以更有效地管理和利用这些数据,提高代码质量。
以上就是在 GitHub 中展示 Python 项目代码覆盖率的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号