Search code examples
pythoncode-coveragenosetests

How do you properly restrict xcoverage package coverage in nosetests using setup.cfg?


I've configured it so my simple unit test passes. But nosexunit.xml is reporting on test lib files. Partial output:

nosexcover-1.0.7-py2.6.egg/nosexcover/nosexcover    25     24     4%   5-41, 46-56
test/unit/test_setup                                13      0   100%   

The project is split up into different modules that need to be tested independently. I'm focusing on the backend module at present. I want to restrict coverage to the lib package. A sample of the project tree:

project
\-- backend     # <-- module I'm testing
    \-- lib     # <-- what I want to cover
    \-- test
       \-- unit/test_setup.py       # <-- test I'm running
    \-- setup.py
    \-- setup.cfg
\--reporting
    \-- setup.py
    \-- setup.cfg

I'm running the tests from the dir named backend:

project/backend$ python setup.py nosetests -s --tests=unit/test_setup.py

Nosetests is configured in setup.cfg as follows

[nosetests]
# locating tests
where=./test
include=^unit.*

# coverage
cover-package=lib
cover-html=1
cover-html-dir=htmlcov
with-xcoverage=1
xcoverage-file=coverage.xml
with-xunit=1
xunit-file=nosexunit.xml
cover-erase=1

I've got a feeling one of the path settings is off. I'm assuming the where and cover-package settings are relative to the location of setup.py (also where I'm running the test) and include is relative to where.

Nosetests documentation wasn't much help. I hope someone can set me straight here.


Solution

  • It turns out that I had this configured correctly after all. The problem seems to be that, for my overly simplified smoke test, I wasn't actually importing anything from the module to which I restricted coverage.

    Apparently, in this case, when coverage is nil, rather than report that boring fact, nosexcoverage or nosetests decides to give you a coverage report about a bunch of other stuff instead.

    By adding an import statement for the module I wanted covered, I got the proper coverage report:

    ----------------------------------------------------------------------
    XML: nosexunit.xml
    Name        Stmts   Miss  Cover   Missing
    -----------------------------------------
    lib             0      0   100%   
    lib.blank       1      0   100%   
    -----------------------------------------
    TOTAL           1      0   100%   
    ----------------------------------------------------------------------
    Ran 2 tests in 0.008s