用途:测量代码测试覆盖率,帮助识别未测试代码并提升测试质量。适用于“measure coverage”“track coverage”“identify untested code”“set coverage thresholds”等场景;不用于编写测试、pytest 配置或修复低覆盖率。
主要能力:生成 HTML 与终端覆盖率报告;按分层设定阈值(领域层 95-100%、应用层 85-95%、适配器层 75-85%、基础设施层 60-75%);区分行覆盖率与分支覆盖率;通过 term-missing 定位缺失行;用 pragma: no cover 排除无需覆盖的行;输出 JSON/XML 供趋势跟踪与 CI 使用。
必要用法: 生成报告:pytest --cov=app --cov-report=html --cov-report=term-missing 查看报告:open htmlcov/index.html 低于阈值失败:pytest --cov=app --cov-fail-under=80 在 pyproject.toml 中配置 [tool.pytest.ini_options]、[tool.coverage.run](branch = true)、[tool.coverage.report] 与 [tool.coverage.html]。
限制:需 Python 3.11+、pytest >= 7.0、pytest-cov >= 4.0;覆盖率数值本身不代表测试质量,需结合分支覆盖与人工判断;具体平台与收费信息暂未提供。