如何从python覆盖单元测试中省略

如何从python覆盖单元测试中省略

本文介绍了如何从python覆盖单元测试中省略(删除)虚拟环境(venv)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

解决方案

--omit 选项的帮助文本说 (文档)

--omit=PAT1,PAT2,... 省略路径匹配这些模式之一的文件.接受必须引用的 shell 样式通配符.

如果不引用通配符,它​​将无法工作,因为 bash 会在将参数列表传递给覆盖二进制文件之前扩展通配符.使用单引号避免 bash 通配符扩展.

要运行我的测试而不从 venv/* 中的任何文件获得覆盖:

$ coverage run --omit 'venv/*' -m unittest tests/*.py &&覆盖率报告 -m…………----------------------------------------------------------------------在 0.023 秒内运行 8 个测试好的Name Stmts Miss Cover Missing-------------------------------------------------------ruterstop.py 84 8 90% 177, 188, 191-197, 207测试/test_ruterstop.py 108 0 100%-------------------------------------------------------总计 192 8 96%

如果你通常使用普通的 python -m unittest 来运行你的测试,你当然也可以省略测试目标参数.

$ coverage run --omit 'venv/*' -m unittest$ 覆盖率报告 -m

https://coverage.readthedocs.io/en/coverage-4.5.1a/source.html#source

My coverage is also including "venv" folder and I would like to exclude itno matter what I do even with --include or omit nothing works

coverage run --omit /venv/* tests.py

This runs the test but still adds "venv" folder and dependencies and their % coverage

When I do

coverage run --include tests.py

To run only tests - it says

Nothing to do.

It is pretty annoying... can someone please help?

解决方案

The help text for the --omit option says (documentation)

--omit=PAT1,PAT2,...  Omit files whose paths match one of these patterns.
                      Accepts shell-style wildcards, which must be quoted.

It will not work without quoting the wildcard, as bash will expand the wildcards before handing the argument list to the coverage binary. Use single-quotes to avoid bash wildcard expansion.

To run my tests without getting coverage from any files within venv/*:

$ coverage run --omit 'venv/*' -m unittest tests/*.py && coverage report -m
........
----------------------------------------------------------------------
Ran 8 tests in 0.023s

OK
Name                      Stmts   Miss  Cover   Missing
-------------------------------------------------------
ruterstop.py                 84      8    90%   177, 188, 191-197, 207
tests/test_ruterstop.py     108      0   100%
-------------------------------------------------------
TOTAL                       192      8    96%

If you usually use plain python -m unittest to run your tests you can of course omit the test target argument as well.

$ coverage run --omit 'venv/*' -m unittest
$ coverage report -m

这篇关于如何从python覆盖单元测试中省略(删除)虚拟环境(venv)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 22:45