我看到 TestCase有一个方法Debug(),但我找不到任何关于如何实现它的示例。据我尝试,没有任何效果。
谁能提供一些关于如何使用它的代码?
from unittest import TestCase
class MyTest(TestCase):
def test1(self):
print 'before'
self.assertEquals(2+2, 5)
print 'after'
python -i debugunit.py
要以交互方式运行测试,请创建一个TestCase
实例,并为其提供测试名称作为参数。要运行它,请调用生成的对象。
>>> print MyTest('test1')()
before
None
“2+2!=5”异常被单元测试机器消耗。要使集合运行(使用setUp
andtearDown
等),请运行以下debug()
方法:
>>> MyTest('test1').debug()
before
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/unittest/case.py", line 400, in debug
getattr(self, self._testMethodName)()
File "debugunit.py", line 6, in test1
self.assertEquals(2+2, 5)
File "/usr/lib/python2.7/unittest/case.py", line 515, in assertEqual
assertion_func(first, second, msg=msg)
File "/usr/lib/python2.7/unittest/case.py", line 508, in _baseAssertEqual
raise self.failureException(msg)
AssertionError: 4 != 5