You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何获取Python单元测试的增量代码覆盖率报告?

How to Track Coverage Delta from Your Last Git Commit

Got it, let's solve this problem of tracking coverage changes specific to your last Git commit—no more tedious manual HTML report comparisons! Here are practical, actionable methods tailored to your workflow with unittest, coverage, and coveralls.io:

If you’re on Coverage.py 6.0+, it has a native compare feature that lets you directly compare coverage data between two commits. Here’s the step-by-step:

  • First, save coverage data from your baseline commit (your last submission):

    # Check out your last commit
    git checkout <your-last-commit-hash>
    # Run tests and generate coverage data
    coverage run -m unittest discover
    # Save this as a baseline dataset
    coverage data save -b baseline
    
  • Switch back to your current branch and generate fresh coverage data:

    git checkout -
    coverage run -m unittest discover
    
  • Now compare the two datasets:

    coverage compare baseline
    

    This outputs a clear diff showing coverage changes—including which lines gained or lost coverage, and the overall delta for each file. You can focus on the modules you’re responsible for to see exactly how your changes impacted coverage.

2. Focus Only on Files You Modified

If you want to cut through project-wide noise and only check coverage for files you edited, combine Git diff with coverage reports:

  • Get a list of Python files you modified in your last commit:

    MODIFIED_FILES=$(git diff --name-only <your-last-commit-hash> HEAD | grep "\.py$")
    
  • Generate a baseline report for these files:

    git checkout <your-last-commit-hash>
    coverage run -m unittest discover
    coverage report -m $MODIFIED_FILES > baseline_coverage.txt
    
  • Generate the current report and compare:

    git checkout -
    coverage run -m unittest discover
    coverage report -m $MODIFIED_FILES > current_coverage.txt
    # Use diff to see the exact changes
    diff baseline_coverage.txt current_coverage.txt
    

    This zeroes in on your edits, so you don’t waste time sifting through coverage data for modules you didn’t touch.

3. Quick Tips to Streamline This Workflow

  • Automate with a script: Turn these commands into a simple shell script (e.g., coverage-delta.sh) that takes a commit hash as an argument—saves you from typing all steps every time.
  • Upgrade Coverage.py: If you don’t have the compare command, run pip install --upgrade coverage to get the latest version (this feature was added in v6.0).
  • Pair with Coveralls: While Coveralls.io shows overall project coverage, use your local delta data to cross-reference with its reports, ensuring your changes align with the project’s coverage goals.

内容的提问来源于stack exchange,提问作者Konstantin Shemyak

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 03:47:21