目的:我需要調用了一堆的步驟(因此函數)只有在測試失敗。Python的單元測試呼叫功能時斷言失敗
我試了一下:
1)試圖傳遞函數W/O參數。
觀察:如果測試通過,功能不會被調用。但是如果測試失敗,我會收到一個錯誤。 (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)...消息 確定
冉1次測試中0.001S
OK
它不會因爲沒有理由1等於1而導致第一個參數是消息,第一個參數是第二個消息。還有更多的:在第一個你不用 - >() – metmirr