【问题标题】:Nose Test Error鼻子测试错误
【发布时间】:2013-12-03 09:37:18
【问题描述】:

我正在使用命令

nosetests -v --with-coverage --cover-package=task --cover-erase --cover-html-dir=cover --cover-html --with-xunit task

运行测试用例 但最后在运行所有测试用例后,我得到了nosetests.xml 空白和以下错误。

Traceback (most recent call last):
  File "/home/nishant-un/env/bin/nosetests", line 9, in <module>
    load_entry_point('nose==1.0.0', 'console_scripts', 'nosetests')()
  File "/home/nishant-un/env/local/lib/python2.7/site-packages/nose/core.py", line 118, in __init__
    **extra_args)
  File "/usr/lib/python2.7/unittest/main.py", line 95, in __init__
    self.runTests()
  File "/home/nishant-un/env/local/lib/python2.7/site-packages/nose/core.py", line 197, in runTests
    result = self.testRunner.run(self.test)
  File "/home/nishant-un/env/local/lib/python2.7/site-packages/nose/core.py", line 61, in run
    test(result)
  File "/home/nishant-un/env/local/lib/python2.7/site-packages/nose/suite.py", line 176, in __call__
    return self.run(*arg, **kw)
  File "/home/nishant-un/env/local/lib/python2.7/site-packages/nose/suite.py", line 223, in run
    test(orig)
  File "/home/nishant-un/env/local/lib/python2.7/site-packages/nose/suite.py", line 176, in __call__
    return self.run(*arg, **kw)
  File "/home/nishant-un/env/local/lib/python2.7/site-packages/nose/suite.py", line 223, in run
    test(orig)
  File "/home/nishant-un/env/local/lib/python2.7/site-packages/nose/suite.py", line 176, in __call__
    return self.run(*arg, **kw)
  File "/home/nishant-un/env/local/lib/python2.7/site-packages/nose/suite.py", line 223, in run
    test(orig)
  File "/home/nishant-un/env/local/lib/python2.7/site-packages/nose/suite.py", line 176, in __call__
    return self.run(*arg, **kw)
  File "/home/nishant-un/env/local/lib/python2.7/site-packages/nose/suite.py", line 223, in run
    test(orig)
  File "/home/nishant-un/env/local/lib/python2.7/site-packages/nose/suite.py", line 176, in __call__
    return self.run(*arg, **kw)
  File "/home/nishant-un/env/local/lib/python2.7/site-packages/nose/suite.py", line 223, in run
    test(orig)
  File "/home/nishant-un/env/local/lib/python2.7/site-packages/nose/suite.py", line 176, in __call__
    return self.run(*arg, **kw)
  File "/home/nishant-un/env/local/lib/python2.7/site-packages/nose/suite.py", line 223, in run
    test(orig)
  File "/home/nishant-un/env/local/lib/python2.7/site-packages/nose/case.py", line 45, in __call__
    return self.run(*arg, **kwarg)
  File "/home/nishant-un/env/local/lib/python2.7/site-packages/nose/case.py", line 138, in run
    result.addError(self, err)
  File "/home/nishant-un/env/local/lib/python2.7/site-packages/nose/proxy.py", line 118, in addError
    formatted = plugins.formatError(self.test, err)
  File "/home/nishant-un/env/local/lib/python2.7/site-packages/nose/plugins/manager.py", line 94, in __call__
    return self.call(*arg, **kw)
  File "/home/nishant-un/env/local/lib/python2.7/site-packages/nose/plugins/manager.py", line 136, in chain
    result = meth(*arg, **kw)
  File "/home/nishant-un/env/local/lib/python2.7/site-packages/nose/plugins/logcapture.py", line 223, in formatError
    test.capturedLogging = records = self.formatLogRecords()
  File "/home/nishant-un/env/local/lib/python2.7/site-packages/nose/plugins/logcapture.py", line 231, in formatLogRecords
    return [safe_str(format(r)) for r in self.handler.buffer]
  File "/usr/lib/python2.7/logging/__init__.py", line 723, in format
    return fmt.format(record)
  File "/usr/lib/python2.7/logging/__init__.py", line 464, in format
    record.message = record.getMessage()
  File "/usr/lib/python2.7/logging/__init__.py", line 328, in getMessage
    msg = msg % self.args
TypeError: not all arguments converted during string formatting

我已经尝试了几乎所有在谷歌中找到的东西。即使删除了 .coverage 文件和所有 .pyc 文件,但它仍然显示相同的错误。任何想法..?

【问题讨论】:

    标签: python unit-testing python-2.7 nosetests


    【解决方案1】:

    这个TypeError是因为混合了不同的格式。

    你的错误在formatLogRecords()里面

    代替:

    msg = msg % self.args

    你应该使用format():

    myMsg = "{} {} {} {} {}".format(param1, param2, param3, param4)
    

    甚至更好的方法是:

    args = ['1', '2', '3', '4']
    myMsg = (' '.join('{}'.format(k) for k in args))
    

    结果:

    >>> 1 2 3 4
    

    这样你的参数就可以灵活了。

    【讨论】:

    • formatLogRecords() 在 /local/lib/python2.7/site-packages/nose/plugins/logcapture.py 中
    • 我明白了,但仍然是同样的错误,其中一个 args 无法转换,因为它具有不同的数据类型,尝试使用调试器,您会发现问题。
    【解决方案2】:

    要尝试的一件事是在没有日志捕获--nologcapture 的情况下运行。很可能您在导入级别的某个地方有一个流氓日志记录,并且在测试可以运行之前它会窒息。 如果你只是在你的测试文件上运行python task.py,你通常可以很容易地暴露这些错误——错误会立即被抛出。

    如果这仍然不能解决您的问题,请尝试在您的测试__main__ 函数中使用nose.run() 在python 中运行nose,然后使用python -m pdb task.py 将其关闭,即使--pdb 也可以让您调试此类错误鼻子中的选项不起作用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-09
      • 2018-09-02
      • 2015-08-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多