pip install coverage
最简单的方式:
# 运行测试并生成所有报告
python run_coverage.py
# 运行并自动在浏览器中打开 HTML 报告
python run_coverage.py --open
# 1. 运行测试并收集覆盖率数据
python -m coverage run -m unittest discover -s tests -p "test_*.py"
# 2. 显示控制台报告
python -m coverage report
# 3. 生成 HTML 报告
python -m coverage html
# 4. 生成 XML 报告(用于 CI/CD)
python -m coverage xml
# 显示缺失的行号
python -m coverage report -m
# 只显示特定文件的覆盖率
python -m coverage report services/gateway_session_store.py
# 设置覆盖率阈值(低于 80% 会失败)
python -m coverage report --fail-under=80
# 显示分支覆盖率详情
python -m coverage report --show-missing
运行 python -m coverage report 后直接在终端显示:
Name Stmts Miss Branch BrPart Cover
----------------------------------------------------------------------
services/token_store.py 158 63 50 10 52.40%
app.py 129 25 48 16 74.58%
tools/bind_auth_code.py 18 3 6 3 75.00%
mcp_protocol.py 147 20 66 18 80.28%
...
----------------------------------------------------------------------
TOTAL 909 132 294 63 80.80%
指标说明:
生成后打开 htmlcov/index.html:
# 自动打开
python run_coverage.py --open
# 或手动打开
start htmlcov/index.html
HTML 报告特性:
用于 CI/CD 集成(如 GitHub Actions、Jenkins):
python -m coverage xml
# 生成 coverage.xml
项目使用 .coveragerc 配置覆盖率工具:
[run]
source = .
omit = tests/*, */__pycache__/*, run_coverage.py
branch = True
[report]
precision = 2
exclude_lines = pragma: no cover
[html]
directory = htmlcov
关键配置:
source: 要测量的源代码目录omit: 排除的文件/目录branch: 启用分支覆盖率exclude_lines: 排除带有特定注释的行查看 HTML 报告中的红色行:
python run_coverage.py --open
对于未覆盖的代码,编写测试:
def test_error_handling(self):
"""测试错误处理分支"""
with self.assertRaises(ValueError):
function_that_raises_error()
def test_empty_input(self):
result = process([])
self.assertEqual(result, [])
def test_none_input(self):
with self.assertRaises(ValueError):
process(None)
使用 # pragma: no cover 注释:
if __name__ == '__main__': # pragma: no cover
main()
name: Tests with Coverage
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.11'
- name: Install dependencies
run: |
pip install -r requirements.txt
pip install coverage
- name: Run tests with coverage
run: |
python -m coverage run -m unittest discover -s tests -p "test_*.py"
python -m coverage report --fail-under=80
python -m coverage xml
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3
with:
file: ./coverage.xml
test:
script:
- pip install coverage
- python -m coverage run -m unittest discover -s tests -p "test_*.py"
- python -m coverage report --fail-under=80
- python -m coverage xml
coverage: '/TOTAL.*\s+(\d+%)$/'
artifacts:
reports:
coverage_report:
coverage_format: cobertura
path: coverage.xml
A: 可能原因:
解决:运行 python -m coverage report -m 查看缺失的行号。
A: 使用 --source 参数:
python -m coverage run --source=services/gateway_session_store.py -m unittest tests.test_gateway_session_store_unit
python -m coverage report
A: 不一定。覆盖率只表示代码被执行了,不表示逻辑正确。还需要:
A: 建议:
截至 2026-07-08:
| 类别 | 覆盖率 | 状态 |
|---|---|---|
| 新增核心模块 | 95-100% | ✅ 优秀 |
| 整体项目 | 80.80% | ✅ 良好 |
| 目标 | 80%+ | ✅ 已达标 |
8 个新增核心模块达到 100% 覆盖率: