是什么构成了鼻试?

时间:2015-05-31 17:33:13

标签: python package code-coverage nose nosetests

我写了一个“包” - 我指的是一个名为__init__.py的目录 - 名为ardid

(master)dbliss@nx3[ardid]> ls
analysis.py   ardid.sh     __init__.pyc    plot_support.py   utils.py
analysis.pyc  goals.py     k_12.png        plot_support.pyc  utils.pyc
ardid.py      goals.pyc    plot_ardid.py   results/
ardid.pyc     __init__.py  plot_ardid.pyc  tests/

如您所见,该软件包包含以下模块

  • analysis
  • ardid
  • goals
  • plot_ardid
  • plot_support
  • utils

以及目录tests中的测试模块。

__init__.py为空。)

但是,当我使用nosetests运行--cover-package=ardid时,输出仅显示一些ardid模块的覆盖范围(具体为ardidgoalsutils):

(master)dbliss@nx3[ardid]> nosetests tests -s --cover-package=ardid --with-coverage
...............
Name             Stmts   Miss  Cover   Missing
----------------------------------------------
ardid.py             0      0   100%   
ardid/ardid.py     108     54    50%   105-254
ardid/goals.py      42     38    10%   52-87, 102-161
ardid/utils.py     437    366    16%   23-26, 30-71, 78, 83-89, 108-110, 113-140, 142-146, 165-166, 168-183, 186-218, 223-285, 288-324, 344, 357-427, 438-441, 443-444, 446-468, 475-480, 483-496, 499, 508-509, 512-513, 516-524, 532, 545-548, 559, 571, 575-576, 581-582, 594-605, 614-615, 618-1018
----------------------------------------------
TOTAL              587    458    22%   
----------------------------------------------------------------------
Ran 15 tests in 11.454s

OK

为什么会这样?

请注意,并非这些是我测试的唯一模块:

(master)dbliss@nx3[ardid]> ls tests/
test_plot_support.py  test_plot_support.pyc  test_utils.py  test_utils.pyc

真实的是ardid模块导入goalsutils,但不导入其他模块。这是导致nosetests仅检测goalsutils(除了ardid)的原因吗?为什么会这样?

我认为答案可能与我在测试模块中导入软件包模块的方式有关。因为ardid模块与包的名称相同,所以我必须使用

导入它
from ardid import ardid

(如果我只是打电话

import ardid

将导入包。)

我导入的其他模块,例如

import utils

1 个答案:

答案 0 :(得分:0)

nosetests会考虑从ardid导入的ardid个模块的一部分(来自ardid包,而不是模块)。

因此,一个简单的解决方案是将测试模块中的导入更改为

from ardid import X

其中X是要测试的模块之一。

另一个解决方案,如果这个软件包供我以外的任何人使用(并且包含我的解决方案),那将更合适,@ jonrsharpe在评论中提供。