pytest-coverage
它能做什么
这个工具可以帮你检查代码是否都经过了测试。它会告诉你代码中哪些部分还没有被测试,方便你补充更多测试。
适用场景
- ✓当你想确保所有代码都有测试覆盖时。
- ✓当你需要找出没有测试的代码部分时。
- ✓当目标是让 100% 的代码都经过测试时。
技能链接
https://github.com/github/awesome-copilot/blob/main/skills/pytest-coverage/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“检查我项目的测试覆盖率,并告诉我哪里缺少测试。”
每周 5 个新技能,1 分钟读完。
工作原理
这个工具会运行你的测试,并检查哪些代码行被执行过。它会生成一份报告,把没有被测试到的代码行用“!”标记出来。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
The goal is for the tests to cover all lines of code.
Generate a coverage report with:
pytest --cov --cov-report=annotate:cov_annotate
If you are checking for coverage of a specific module, you can specify it like this:
pytest --cov=your_module_name --cov-report=annotate:cov_annotate
You can also specify specific tests to run, for example:
pytest tests/test_your_module.py --cov=your_module_name --cov-report=annotate:cov_annotate
Open the cov_annotate directory to view the annotated source code. There will be one file per source file. If a file has 100% source coverage, it means all lines are covered by tests, so you do not need to open the file.
For each file that has less than 100% test coverage, find the matching file in cov_annotate and review the file.
If a line starts with a ! (exclamation mark), it means that the line is not covered by tests. Add tests to cover the missing lines.
Keep running the tests and improving coverage until all lines are covered.
常见问题
什么是“代码覆盖率”?
代码覆盖率是一项指标,用来表示你的测试执行了代码中的哪些部分。目标是让代码覆盖率达到 100%。
“cov_annotate”目录是什么?
它是工具用来保存文件的文件夹,这些文件会逐行显示代码中哪些部分被测试过,哪些没有。
我怎么知道是否需要补充测试?
如果报告中的某个代码文件里有带“!”标记的行,就说明这些行没有被测试,你需要为它们补充测试。
详情
作者的原始描述
Run pytest tests with coverage, discover lines missing coverage, and increase coverage to 100%.
repo: github/awesome-copilot
热度
- GitHub 星标
- 40k
- 排名
- #7349
- 许可证
- MIT
- 更新时间
- 2026/10/15
评论
- …
同一仓库的更多技能
acquire-codebase-knowledge
40k帮你的智能体理解并编写现有代码项目的文档。它有助于新成员快速上手,或者快速了解项目架构。
acreadiness-assess
40k让你的智能体评估一个项目对 AI 的准备程度,并生成报告。它有助于了解项目在使用 AI 工具方面的准备情况。
acreadiness-generate-instructions
40k帮你的智能体根据评估结果编写 AI 工具的指令文件。它有助于改善 AI 与代码的交互方式,并弥补已发现的不足。
acreadiness-policy
40k让你的智能体协助制定或应用 AI 项目评估的政策。它有助于规范项目的评估方式,并确保合规。