鼻子测试错误

时间:2013-12-03 09:37:18

标签: python unit-testing python-2.7 nosetests

我正在使用命令

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但它仍然显示相同的错误。任何想法..?

2 个答案:

答案 0 :(得分:0)

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

这样你的args可以灵活。

答案 1 :(得分:0)

尝试的一件事是运行nose而不记录日志--nologcapture。很可能你在导入级别的某个地方有一个流氓日志记录,并且在测试可以运行之前它会扼杀鼻子。 如果只是在测试文件上运行python task.py,通常可以轻松暴露这些错误 - 错误将立即被抛出。

如果这仍然无法解决您的问题,请尝试在测试nose.run()函数中使用__main__在python中运行nose,然后使用python -m pdb task.py将其激活,它会让您调试此类错误,即使nose中的--pdb选项不起作用。