Frage

I get an import error when I use coverage.py to run a suite of tests where nose is the underlying test runner. The tests run fine if I just run under Python instead.

coverage run run_tests.py
ERROR

======================================================================
ERROR: <nose.suite.ContextSuite context=nova.tests>
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/lorin/nova/hpc-trunk/.nova-venv/lib/python2.6/site-packages/nose/suite.py", line 208, in run
    self.setUp()
  File "/Users/lorin/nova/hpc-trunk/.nova-venv/lib/python2.6/site-packages/nose/suite.py", line 291, in setUp
    self.setupContext(ancestor)
  File "/Users/lorin/nova/hpc-trunk/.nova-venv/lib/python2.6/site-packages/nose/suite.py", line 314, in setupContext
    try_run(context, names)
  File "/Users/lorin/nova/hpc-trunk/.nova-venv/lib/python2.6/site-packages/nose/util.py", line 478, in try_run
    return func()
  File "/Users/lorin/nova/trunk/nova/tests/__init__.py", line 43, in setup
    from nova import context
ImportError: cannot import name context

This does not seem to be a path error, instead it seems like the "nova" module's submodules are not visible when I run the test by doing "coverage run". I found this out by adding the following code to the method that was failing:

import nova; print nova.__path__
print dir(nova)

When I run this as python run_tests.py, I see the following output:

['/Users/lorin/nova/hpc-trunk/nova']
['__builtins__', '__doc__', '__file__', '__name__', '__package__', '__path__', 
 'api', 'auth', 'compute', 'console', 'context', 'crypto', 'db', 'exception', 
 'fakerabbit', 'flags', 'image', 'ipv6', 'log', 'manager', 'network', 'objectstore', 
 'quota', 'rpc', 'scheduler', 'service', 'test', 'tests', 'twistd', 'utils', 'version', 
 'virt', 'vnc', 'volume', 'wsgi']

However, when I run this as coverage run run_tests.py, I see the following output:

['/Users/lorin/nova/hpc-trunk/nova']
['__builtins__', '__doc__', '__file__', '__name__', '__package__', '__path__', 
 'api', 'tests']

It seems like it's finding the nova module in the same place in each case, but in the case where I run it with coverage, Python does not see most of the submodules: (auth, compute, ...)console.

Note that this problem doesn't happen when I use the coverage plugin for nose, but in this particular case I don't want to use the plugin.

War es hilfreich?

Lösung

This was a problem with coverage.py. When it inserted the current directory into the sys.path list, it didn't use an absolute path, which somehow caused this problem. Coverage.py is now fixed: https://bitbucket.org/ned/coveragepy/changeset/811ed58de8a3

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top