美文网首页
python3 测试代码覆盖率- coverage

python3 测试代码覆盖率- coverage

作者: C1R2 | 来源:发表于2023-02-21 09:18 被阅读0次

<meta charset="utf-8">

[Coverage]是一个用来测试代码覆盖率的 Python 第三方库。

安装

pip install coverage

执行

原来的执行UT命令假设是 python run_all_tests.py, 那么需要测试代码覆盖率的命令为coverage run --source . run_all_tests.py,跑完命令后,则会在目录下生成.coverage的文件。
--source .: 指定分析当前路径,不会计算导入库的覆盖率

image.png image.png
更多coverage参数

(1)命令行用法

run – 运行Python程序并且收集执行数据
report – 报告覆盖率结果
html – 生成覆盖率结果的HTML列表且带注释
xml – 生成一份覆盖率结果的XML报告
annotate – 使用覆盖结果注释源文件
erase – 擦除以前收集的覆盖率数据
combine – 将许多数据文件组合在一起
debug – 获取诊断信息

(2)配置参考
--rcfile=FILE: 可以用配置文件代替命令参数

# .coveragerc to control coverage.py
[run]
branch = True

[report]
# Regexes for lines to exclude from consideration
exclude_lines =
    # Have to re-enable the standard pragma
    pragma: no cover

    # Don't complain about missing debug-only code:
    def __repr__
    if self\.debug

    # Don't complain if tests don't hit defensive assertion code:
    raise AssertionError
    raise NotImplementedError

    # Don't complain if non-runnable code isn't run:
    if 0:
    if __name__ == .__main__.:

ignore_errors = True

[html]
directory = coverage_html_report

(3)指定源文件
--source: 指定分析的路径,不会分析引用其他库的覆盖
--include:指定分析文件或者文件夹,可使用或者?匹配。
--omit:指定不分析文件或者文件夹,可使用
或者?匹配。

参考链接:https://www.jianshu.com/p/7eaf04924be8

相关文章

网友评论

      本文标题:python3 测试代码覆盖率- coverage

      本文链接:https://www.haomeiwen.com/subject/raqtertx.html