跳到主要内容
哒哒星球DADA PLANET

Skill · 已公开版本

pytest-coverage · pytest 覆盖率标注与补测

运行 pytest 生成覆盖率标注报告,定位未被测试覆盖的代码行,并补充测试直到全部行被覆盖。

先看看示例

作者提供的示例,实际效果取决于你的输入和使用工具。

输入

对 your_module 运行覆盖率标注,找出未覆盖行并补充测试,直到该模块覆盖率达到 100%。

输出

预期交付:cov_annotate 目录下按源文件生成的标注文件,未覆盖行以 ! 开头标记。(编辑说明,非实际运行结果)

方法说明

---
name: pytest-coverage
description: 'Run pytest tests with coverage, discover lines missing coverage, and increase coverage to 100%.'
---

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.