【发布时间】:2016-12-07 19:47:15
【问题描述】:
目标:只有在测试失败时,我才需要调用一堆步骤(因此是一个函数)。
我尝试了什么:
1) 尝试传递不带参数的函数。
观察:如果测试通过,函数不会被调用。但是如果测试失败,我会得到一个错误。 (AssertionError: <bound method TestAuto.func1 of <test_fail.TestAuto testMethod=test_fail>>)
class TestAuto(unittest.TestCase):
def test_fail(self):
self.assertEqual(1, 1, self.func1)
def func1(self):
print 'We are inside'
if __name__ == '__main__':
unittest.main()
:
test_fail (test_fail.TestAuto) ... ok
----------------------------------------
Ran 1 test in 0.001s
OK
2) 尝试使用参数调用函数。
class TestAuto(unittest.TestCase):
def test_fail(self):
self.assertEqual(1, 1, self.func1('message'))
def func1(self, msg):
print msg
观察:无论测试通过还是失败都会调用函数。
结果:
test_fail (test_fail.TestAuto) ... 消息 好的
在 0.001 秒内运行 1 次测试
好的
【问题讨论】:
-
它不会因为没有理由1等于1
assertEqual(first, second, msg="message!")第三个参数是消息。还有更多:在第一个中,您不使用 ->() 调用该函数
标签: python unit-testing