Python 在覆盖率和鼻子下运行时看不到子模块

发布于 2024-11-08 22:03:30 字数 2070 浏览 1 评论 0原文

当我使用coverage.py 运行一组测试时出现导入错误,其中nose 是底层测试运行程序。如果我只是在 Python 下运行,测试就可以正常运行。

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

这似乎不是路径错误,相反,当我通过“覆盖运行”运行测试时,“nova”模块的子模块似乎不可见。我通过将以下代码添加到失败的方法中发现了这一点:

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

当我将其作为 python run_tests.py 运行时,我看到以下输出:

['/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']

但是,当我将其作为 coverage 运行时运行 run_tests.py,我看到以下输出:

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

似乎在每种情况下都在同一位置找到 nova 模块,但在我以覆盖率运行它的情况下,Python 看不到大多数子模块:(验证、计算、 ...)安慰。

请注意,当我使用鼻子的覆盖插件时,不会发生此问题,但在这种特殊情况下,我不想使用该插件。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓝眼泪 2024-11-15 22:03:30

这是coverage.py 的问题。当它将当前目录插入到 sys.path 列表中时,它没有使用绝对路径,这在某种程度上导致了这个问题。 Coverage.py 现已修复: https://bitbucket.org/ned/coveragepy/changeset/811ed58de8a3< /a>

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文