python - 如何从 python 覆盖单元测试中省略(删除)虚拟环境(venv)?
问题描述
https://coverage.readthedocs.io/en/coverage-4.5.1a/source.html#source
我的覆盖范围还包括“venv”文件夹,无论我做什么,我都想排除它,即使使用 --include 或 omit nothing works
coverage run --omit /venv/* tests.py
这会运行测试,但仍会添加“venv”文件夹和依赖项及其覆盖率
当我做
coverage run --include tests.py
只运行测试 - 它说
Nothing to do.
这很烦人......有人可以帮忙吗?
解决方案
该--omit
选项的帮助文本说(文档)
--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%
如果你通常使用 plainpython -m unittest
来运行你的测试,你当然也可以省略 test target 参数。
$ coverage run --omit 'venv/*' -m unittest
$ coverage report -m
推荐阅读
- java - 如何在 Android 上使用 ARCore 获取 PointCloud 和 Pose?
- ruby-on-rails - Rails 5:来自枚举(散列)的 collection_check_boxes
- angular - 如何在 Angular + Laravel 中使用 Echo?
- php - 每页的 Woocommerce 产品无法正确显示
- php - get_meta 关于可变结帐流程(购物车、结帐、电子邮件)
- spring-boot - 在类路径资源 [org/springframework/boot/autoconfigure/gson/GsonAutoConfiguration.class] 中定义名称为“gsonBuilder”的 bean 创建错误
- python - 如何迭代表的列并使用列表的值更新它们?
- pycharm - 在 pycharm 中运行 python 解释器时运行源脚本以加载环境
- unicode - 为什么我不能将这些十六进制转换为 utf-8?
- c++ - 即使可执行文件的输入是从文件重定向的,如何强制从终端输入?