Search code examples
pythonpackagecode-coveragenosenosetests

What constitutes a package for nosetests?


I've written a "package" -- by which I mean a directory with an __init__.py in it -- called 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/

As you can see, the package contains the following modules

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

as well as test modules in the directory tests.

(__init__.py is empty.)

However, when I run nosetests with --cover-package=ardid, the output shows coverage only for a few of ardid's modules (specifically ardid, goals, and utils):

(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

Why is this?

Note that it is not the case that these are the only modules for which I have tests:

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

What is true is that the ardid module imports goals and utils, but does not import the other modules. Is this what's causing nosetests only to detect goals and utils (in addition to ardid)? Why would this be?

I think the answer may have to do with the way I'm importing the package modules in my test modules. Because the ardid module has the same name as the package, I have to import it with

from ardid import ardid

(If I were to call simply

import ardid

the package would be imported.)

The other modules I import with, e.g.,

import utils

Solution

  • nosetests considers part of the package ardid modules that are imported from ardid (from ardid the package, not the module).

    Thus, one simple solution is to change the imports in the test modules to

    from ardid import X
    

    where X is one of the modules to be tested.

    Another solution, which would be more appropriate if this package were intended for use by anyone other than me (and which subsumes my solution), is provided by @jonrsharpe in the comments.