我使用的是鼻子1.3.0和3.7。
在命令行上运行
coverage run test_myfile.py
coverage report
生成一个报告,将其自身限制为myfile.py中的函数:
Name Stmts Miss Branch BrMiss Cover
----------------------------------------------------
myfile 157 27 38 12 80%
test_myfile 81 16 16 13 70%
----------------------------------------------------
TOTAL 238 43 54 25 77%
但是,如果我尝试使用nose的coverage插件,则覆盖范围扩展到已安装的python库,速度很慢并且会导致结果混乱:
nosetests --with-coverage myfile.py
此处覆盖范围扩展到已安装库中所有已使用的软件包的所有详细信息(报告手动缩短):
Name Stmts Miss Branch BrMiss Cover Missing
---------------------------------------------------------------------------------------------------
Cookie 201 201 64 64 0% 42-756
_LWPCookieJar 93 93 42 42 0% 14-169
_MozillaCookieJar 68 68 33 33 0% 3-149
bisect 46 46 32 32 0% 3-92
.
.
.
myfile 157 27 38 12 80% 57-58, 61, 64-67, 83, 114-145
.
.
.
requests.packages.urllib3 27 27 0 0 0% 7-58
requests.packages.urllib3._collections 49 49 10 10 0% 7-94
requests.packages.urllib3.connectionpool 209 209 50 48 1% 7-597
requests.packages.urllib3.contrib 0 0 0 0 100%
requests.packages.urllib3.exceptions 42 42 2 2 0% 10-95
requests.packages.urllib3.filepost 40 40 18 18 0% 7-98
requests.packages.urllib3.packages 2 2 0 0 0% 1-3
.
.
.
sqlalchemy.engine.reflection 156 156 79 79 0% 7-506
sqlalchemy.engine.result 434 434 173 169 1% 7-997
sqlalchemy.engine.strategies 124 124 40 40 0% 7-260
sqlalchemy.engine.threadlocal 80 80 28 28 0% 7-134
.
.
.
urllib2 820 820 323 323 0% 92-1470
uuid 293 293 119 119 0% 47-560
---------------------------------------------------------------------------------------------------
TOTAL 35091 34961 14918 14828 1%
----------------------------------------------------------------------
这似乎等同于在命令行上指定:
coverage run --pylib test_myfile.py
如何让鼻子覆盖插件无法深入研究已安装的python库?
答案 0 :(得分:1)
你应该configure it。这是一个例子:
# .coveragerc
[report]
include = *.py
omit =
tests.py
*_test.py
*_tests.py
*/site-packages/*
*/migrations/*
答案 1 :(得分:0)
我想你可能想尝试this:
之类的东西nosetests --with-coverage --cover-package=myfile --cover-tests