pythonunit-testingnosetest-coveragepython-coverage

command line option to display coverage in python


When I use nose from terminal to display test result with coverage I get error and missing lines that are actually covered

$ python -m nose.core test_utils.py -s --with-coverage

saying

======================================================================
ERROR: Failure: AttributeError ('module' object has no attribute 'test_utils')
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Library/Python/2.7/site-packages/nose/loader.py", line 413, in loadTestsFromName
    addr.filename, addr.module)
  File "/Library/Python/2.7/site-packages/nose/importer.py", line 47, in importFromPath
    return self.importFromDir(dir_path, fqname)
  File "/Library/Python/2.7/site-packages/nose/importer.py", line 94, in importFromDir
    mod = load_module(part_fqname, fh, filename, desc)
  File "/Development/python/slideShow/tests/test_utils.py", line 50, in <module>
    unittest.main()
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/main.py", line 94, in __init__
    self.parseArgs(argv)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/main.py", line 149, in parseArgs
    self.createTests()
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/main.py", line 158, in createTests
    self.module)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/loader.py", line 128, in loadTestsFromNames
    suites = [self.loadTestsFromName(name, module) for name in names]
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/loader.py", line 100, in loadTestsFromName
    parent, obj = obj, getattr(obj, part)
AttributeError: 'module' object has no attribute 'test_utils'

Name      Stmts   Miss  Cover   Missing
---------------------------------------
PyQt4         0      0   100%   
getopt      103    103     0%   34-210
mox         597    597     0%   17-1898
stubout      50     50     0%   17-142
utils        23     16    30%   8-10, 13-20, 23, 26-30

how is the test_utils not found? I am standing in the same directory!


Solution

  • alright while troubleshooting I got my answer,

    the test module have to have

    if __name__ == '__main__': unittest.main()

    in order for python -m nose.core test_utils.TestUtils --with-coverage to work

    earlier i just had unittest.main()