2015-05-31 97 views
0

我写了一个“包” - 我指的是在它的__init__.py目录 - 所谓的ardid什么是鼻子测试套件?

(master)[email protected][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的模块(具体地说ardidgoals,和utils):

(master)[email protected][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)[email protected][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 
+0

是否有覆盖排除文件的测试? '__init __。py'中是什么? – jonrsharpe

+0

如果你在*目录结构中“上”*一级,并在那里运行相同的命令,会发生什么? – jonrsharpe

+0

考虑转型使得'tests'和'ardid'目录是在*同级别*,另一个字典中(你也可以拨打'ardid')。然后从* that *目录运行测试。 – jonrsharpe

回答

0

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

因此,一种简单的解决方案是在测试模块中的进口变成

from ardid import X 

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

另一种解决办法,如果此包是适合使用的除我之外的人(和它包含了我的解决方案),这将是更合适的,由@jonrsharpe在给予。