【问题标题】:Python: AssertionError when running nose tests with coveragePython:运行覆盖范围的鼻子测试时出现 AssertionError
【发布时间】:2012-02-19 19:51:29
【问题描述】:

我对 python 测试相当熟悉,所以这可能是我做错了..

当我运行我的测试时,测试运行器工作正常并且覆盖范围也很好。但是在两者之间我得到一个断言错误:

Traceback (most recent call last):
  File "/usr/local/bin/coverage", line 9, in <module>
    load_entry_point('coverage==3.5.1', 'console_scripts', 'coverage')()
  File "/usr/local/lib/python2.7/dist-packages/coverage/cmdline.py", line 657, in main
    status = CoverageScript().command_line(argv)
  File "/usr/local/lib/python2.7/dist-packages/coverage/cmdline.py", line 526, in command_line
    self.coverage.stop()
  File "/usr/local/lib/python2.7/dist-packages/coverage/control.py", line 389, in stop
    self.collector.stop()
  File "/usr/local/lib/python2.7/dist-packages/coverage/collector.py", line 262, in stop
    assert self._collectors[-1] is self
AssertionError

为了让事情变得更加困难,我正在尝试测试一个命令行实用程序。这意味着我必须告诉 coverage 以涵盖子流程调用。

我认为我已经完成了这部分工作,因为覆盖率现在报告正在运行的脚本的覆盖率百分比。但由于我得到了覆盖工作,我无法摆脱 AssertionError。

非常感谢您对了解问题所在的帮助。我所有的代码都可以在 github 上找到:

快跑:

cd /tmp/ && git clone git://github.com/h3/django-duke-client.git 
cd django-duke-client && chmod a+x run_tests && ./run_tests

谢谢

更新

我在另一台计算机上运行了测试,得到了相同的 AssertionError .. 加上一个新的 TypeError。测试再次正常运行,即使出现这些错误,覆盖率似乎也能正常工作..

...
Ran 9 tests in 1.324s

OK
Traceback (most recent call last):
  File "/usr/local/bin/coverage", line 9, in <module>
    load_entry_point('coverage==3.5.1', 'console_scripts', 'coverage')()
  File "/usr/local/lib/python2.7/dist-packages/coverage/cmdline.py", line 657, in main
    status = CoverageScript().command_line(argv)
  File "/usr/local/lib/python2.7/dist-packages/coverage/cmdline.py", line 526, in command_line
    self.coverage.stop()
  File "/usr/local/lib/python2.7/dist-packages/coverage/control.py", line 389, in stop
    self.collector.stop()
  File "/usr/local/lib/python2.7/dist-packages/coverage/collector.py", line 262, in stop
    assert self._collectors[-1] is self
AssertionError
Error in atexit._run_exitfuncs:
Traceback (most recent call last):
  File "/usr/lib/python2.7/atexit.py", line 24, in _run_exitfuncs
    func(*targs, **kargs)
  File "/usr/lib/python2.7/multiprocessing/util.py", line 284, in _exit_function
    info('process shutting down')
TypeError: 'NoneType' object is not callable
Error in sys.exitfunc:
Traceback (most recent call last):
  File "/usr/lib/python2.7/atexit.py", line 24, in _run_exitfuncs
    func(*targs, **kargs)
  File "/usr/lib/python2.7/multiprocessing/util.py", line 284, in _exit_function
    info('process shutting down')
TypeError: 'NoneType' object is not callable

Name                               Stmts   Miss Branch BrPart  Cover   Missing
------------------------------------------------------------------------------
dukeclient/__init__                   53     53      2      0     4%   1-93
dukeclient/commands/__init__          41     33      6      2    26%   1-9, 12, 14-15, 17, 24-28, 34-43, 46-63
...

【问题讨论】:

    标签: python unit-testing nose coverage.py python-coverage


    【解决方案1】:

    关于NoneType is not callable error,请在下面找到一些可能对您有所帮助的元素。

    在您的模块plugintest.py 中来自nose-1.1.2-py2.7.egg/nose/plugins/,第174 行,可以阅读以下行:

    from multiprocessing import Manager
    

    这会导致 multiprocessing.util 包被导入,并随之注册一个退出函数:

    atexit.register(_exit_function)
    

    问题似乎是在plugintest 中加载的multiprocessing.util 然后在_exit_function 被调用之前被卸载,顺便说一下它的函数定义。

    因此,如果您将其导入 setup.py 文件:

    from multiprocessing import util
    

    错误消失。

    顺便说一句,我不得不对测试中失败的部分进行注释或更改某些代码行:

    • -m 命令似乎无效;
    • 我不得不将duke_conf.yml 重命名为duke_conf.yml;
    • 检查README.rst 和LICENSE 文件是否存在的测试失败(没有时间检查原因);

    希望对你有帮助,

    【讨论】:

      猜你喜欢
      • 2011-07-26
      • 1970-01-01
      • 1970-01-01
      • 2023-04-07
      • 2019-09-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多