如何在python测试覆盖中错过哪些语句

Pla*_*oom 11 python code-coverage pytest

我是python的新手,我已经为我的类编写了测试用例,我正在使用它 python -m pytest --cov=azuread_api来获取代码覆盖率.

我在控制台上得到了报道 在此输入图像描述

如何在aadadapter.py文件中通过测试获取哪些行

谢谢,

Ign*_*sel 23

如果您在pytest-cov中查看报告文档,您可以看到如何操作报告并生成额外版本.

例如,添加选项,--cov-report term-missing您将获得在终端中打印的缺失行.

更加用户友好的选项是通过使用--cov-report html选项生成html报告.然后,您可以导航到生成的文件夹(htmlcov默认情况下),然后index.html使用浏览器打开并导航突出显示缺失行的源代码.


pep*_*uan 6

除了来自 Ignacio答案之外,还可以设置show_missing = truein .coveragerc,因为 pytest-cov 也会读取该配置文件。


Ken*_*iii 5

要修改@pepoluan的答案pytest-cov也可以在pyproject.toml.

#pyproject.toml
[tool.pytest.ini_options]
addopts = "--cov --cov-report term-missing"
Run Code Online (Sandbox Code Playgroud)

或两者兼而有之,例如

#.coveragerc
[report]
show_missing=true
Run Code Online (Sandbox Code Playgroud)

#pyproject.toml
[tool.pytest.ini_options]
addopts = "--cov"
Run Code Online (Sandbox Code Playgroud)