跳到正文
Skillpedia
测试40k · #7349

pytest-coverage

它能做什么

这个工具可以帮你检查代码是否都经过了测试。它会告诉你代码中哪些部分还没有被测试,方便你补充更多测试。

适用场景

  • ✓当你想确保所有代码都有测试覆盖时。
  • ✓当你需要找出没有测试的代码部分时。
  • ✓当目标是让 100% 的代码都经过测试时。

技能链接

https://github.com/github/awesome-copilot/blob/main/skills/pytest-coverage/SKILL.md

复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。

使用方法

最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。

可直接粘贴的消息

请用这个技能帮我:https://github.com/github/awesome-copilot/blob/main/skills/pytest-coverage/SKILL.md 阅读该链接中的 SKILL.md 文件,并在我的任务中按照它的说明来做。
想让你的 AI 自己找到这样的技能吗?安装免费的 Skillpedia MCP:只需一行,每次任务前它都会在 15,000+ 个技能中搜索。1 分钟完成安装 →

可以这样问 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

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能