python - 如何从Python覆盖率单元测试中省略(删除)虚拟环境(venv)?

标签 python python-3.x unit-testing coverage.py python-venv

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

我的报道还包括“venv”文件夹,我想排除它 无论我做什么,即使使用 --include 或省略,也不起作用

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

这会运行测试,但仍然添加“venv”文件夹和依赖项及其覆盖率%

当我这样做的时候

coverage run --include tests.py

仅运行测试 - 它说

Nothing to do.

这很烦人......有人可以帮忙吗?

Python Coverage Report

最佳答案

--omit 选项的帮助文本显示 ( documentation )

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

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

要运行我的测试而不覆盖 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%

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

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

关于python - 如何从Python覆盖率单元测试中省略(删除)虚拟环境(venv)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52133482/

相关文章:

python - 字符串列表列表到字典列表

python - peewee 外键错误

Python 3 : Sympy: Include list information to optimize lambdify

python - 单线程脚本在多核处理器上消耗过多的 CPU

python - 基数排序, "Queue"对象不可迭代

python - 在 webhook 中解析 JSON

python - 如何同时运行两个线程?

在同一类的另一个方法中调用 C++ GoogleMock 模拟方法

unit-testing - 如何在一个go包中运行多个测试文件

ios - 如何使用返回数据的完成处理程序为方法编写单元测试?