答案 0 :(得分:0)
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
和tearDown
等),请运行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