我们说我有一个名为program1test.py
import math
def test_1case1():
assert math.sqrt(4) == 4
我使用PyDev中的Nose Test Runner运行,而Window中没有参数 - >偏好 - > PyDev - >的PyUnit
结果如预期:
F
======================================================================
FAIL: program1test.test_1case1
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\Python34\lib\site-packages\nose\case.py", line 198, in runTest
self.test(*self.arg)
File "D:\Users\Samuel Louie\Documents\workspace\testpy\program1test.py", line 5, in test_1case1
assert math.sqrt(4) == 4
AssertionError
----------------------------------------------------------------------
Ran 1 test in 0.000s
FAILED (failures=1)
但是,我想检查math.sqrt(4)实际给出的内容。所以我添加了--detailed-errors
作为Nose Test Runer的参数。
如果我现在运行测试,结果将是:
E
======================================================================
ERROR: program1test.test_1case1
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\Python34\lib\site-packages\nose\case.py", line 134, in run
self.runTest(result)
File "C:\Python34\lib\site-packages\nose\case.py", line 152, in runTest
test(result)
File "C:\Python34\lib\unittest\case.py", line 622, in __call__
return self.run(*args, **kwds)
File "C:\Python34\lib\unittest\case.py", line 582, in run
self._feedErrorsToResult(result, outcome.errors)
File "C:\Python34\lib\unittest\case.py", line 513, in _feedErrorsToResult
result.addFailure(test, exc_info)
File "C:\Python34\lib\site-packages\nose\proxy.py", line 146, in addFailure
plugins.addFailure(self.test, err)
File "C:\Python34\lib\site-packages\nose\plugins\manager.py", line 99, in __call__
return self.call(*arg, **kw)
File "C:\Python34\lib\site-packages\nose\plugins\manager.py", line 167, in simple
result = meth(*arg, **kw)
File "C:\Program Files\Eclipse\plugins\org.python.pydev_3.4.1.201403181715\pysrc\pydev_runfiles_nose.py", line 126, in addFailure
err,
File "C:\Program Files\Eclipse\plugins\org.python.pydev_3.4.1.201403181715\pysrc\pydev_runfiles_nose.py", line 70, in reportCond
error_contents = self.getIoFromError(error)
File "C:\Program Files\Eclipse\plugins\org.python.pydev_3.4.1.201403181715\pysrc\pydev_runfiles_nose.py", line 101, in getIoFromError
import traceback;traceback.print_exception(etype, value, tb, file=s)
File "C:\Python34\lib\traceback.py", line 169, in print_exception
for line in _format_exception_iter(etype, value, tb, limit, chain):
File "C:\Python34\lib\traceback.py", line 146, in _format_exception_iter
for value, tb in values:
File "C:\Python34\lib\traceback.py", line 125, in _iter_chain
context = exc.__context__
AttributeError: 'str' object has no attribute '__context__'
----------------------------------------------------------------------
Ran 1 test in 0.030s
FAILED (errors=1)
如何解决?
答案 0 :(得分:0)
查看nose.tools.eq_
:
from nose.tools import eq_
.....
eq_(math.sqrt(4), 4)