2016-12-07 116 views
0

目的:我需要調用了一堆的步驟(因此函數)只有在測試失敗。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

+0

它不會因爲沒有理由1等於1而導致第一個參數是消息,第一個參數是第二個消息。還有更多的:在第一個你不用 - >() – metmirr

回答

0

的 「消息」各種斷言的參數方法是用來描述測試失敗的字符串。在你的第一個例子中,你傳遞一個函數。如果斷言成功則不使用,如果測試失敗則打印(因爲這是消息發生的情況)。

在你的第二個例子中,你已經做了一個函數調用,以便準備參數 - 這發生在調用assertEquals之前之前。 'message'被打印出來,因爲你的電話打印在func1中。

+0

調用函數我同意你的觀點。這解釋了觀察到的行爲。我雖然找到了解決我的問題(張貼在這裏)。告訴我,如果你同意。 –

0

在MSG =無希望的東西來顯示。所以如果我們想使用一個函數,我們需要返回一些東西。現在我已經嘗試過只使用字符串,它的工作原理。

class TestAuto(unittest.TestCase): 
    def test_fail(self): 
     self.assertEqual(1, 1, msg=self.func1('2 msg')) 
    def func1(self, mesg=None): 
     if mesg: 
      return mesg 
     else: 
      return 'Just an error' 

1)案例1:當測試失敗

============================================================== 
FAIL: test_fail (test_fail.TestAuto) 
-------------------------------------------------------------- 
Traceback (most recent call last): 
    File "test_fail.py", line 5, in test_fail 
    self.assertEqual(1, 2, msg=self.func1('2 msg')) 
AssertionError: 2 msg 

-------------------------------------------------------------- 
Ran 1 test in 0.002s 

FAILED (failures=1) 

2)案例2:如果測試通過

self.assertEqual(1, 1, msg=self.func1('2 msg')) 

test_fail (test_fail.TestAuto) ... ok 

-------------------------------------------------------------- 
Ran 1 test in 0.001s 

OK 
+0

'self.func1('2 msg')'將被調用,無論測試是否失敗 –

+0

@ GennadyKandaurov ..你說得對。我剛剛發現了。我試圖捕捉測試的狀態並在所謂的函數中使用它。 –

2

您可以使用順序try/except聲明:

from exceptions import AssertionError as AE 

class TestAuto(unittest.TestCase): 
    def test_not_fail(self): 
     # won't call func1 
     try: 
      self.assertEqual(1, 1) 
     except AE: 
      self.func1() 
      raise 

    def test_fail(self): 
     # will call func1 
     try: 
      self.assertEqual(1, 9) 
     except AE: 
      self.func1() 
      raise 

    def func1(self): 
     print 'We are inside' 

它可以作爲dec執行orator方便使用:

from exceptions import AssertionError as AE 

def run_if_test_fails(call_on_fail): 
    def deco(f): 
     def inner(*args, **kwargs): 
      try: 
       f(*args, **kwargs) 
      except AE: 
       # test failed - run callback 
       call_on_fail() 
       # reraise Error to mark it in result 
       raise 
     return inner 
    return deco 

def func1(): 
    print('We are inside') 

class TestAuto(unittest.TestCase): 

    @run_if_test_fails(func1) 
    def test_not_fail(self): 
     # won't call func1 
     self.assertEqual(1, 1) 

    @run_if_test_fails(func1) 
    def test_fail(self): 
     # will call func1 
     self.assertEqual(1, 9)