2014-09-27 77 views
2

我有一個由HTMLRunner毀了一個測試套件如何再次運行測試?如果失敗,單元測試

注意:我聽說兩次運行測試是不正確的,但我需要重新運行它。

+0

爲什麼不修複測試呢? – Veedrac 2014-09-27 12:13:16

+0

我不明白爲什麼它有時失敗,有時不會。它總是不同的測試。 – Michael 2014-09-27 12:29:18

+3

@Michael:你仍然必須明白,因爲它可能表明你的代碼中有一個短暫的錯誤。忽略有時失敗的單元測試不是一個好策略。 – 2014-09-27 12:30:21

回答

0

您可以隨時嘗試將測試保存爲函數,並在try/except循環中運行它。嘗試/除了將允許您在失敗後繼續。循環將允許您控制運行測試的次數。

def test(variables go here): 
    if __name__ == '__main__': 
     suite = unittest.TestSuite() 
     suite.addTest(unittest.makeSuite(MyForms)) 
     dateTimeStamp = time.strftime('%Y%m%d_%H_%M_%S') 
     buf = file("../TestReport" + "_" + dateTimeStamp + ".html", 'wb') 
     runner = HTMLTestRunner.HTMLTestRunner(
      stream = buf, 
      title = 'PDFFiller tests', # Title of report 
      description = 'Test results' # Description of report 
      ) 
     runner.run(suite) 

VariableMarkerOfTestStatus = 0 #This variable is just a way of telling if the test failed or not 
#tempCounter = 0 #UNCOMMENT IF YOU WANT TO LIMIT THE NUMBER OF TIMES THIS RUNS 

while VariableMarkerOfTestStatus < 1: #This will loop until it passes. 
    try: test(variables go here) 
    except: VariableMarkerOfTestStatus -=1 

    #UNCOMMENT IF YOU WANT TO LIMIT THE NUMBER OF TIMES THIS RUNS 
    #if VariableMarkerOfTestStatus < 0: 
    # tempCounter+=1 
    # if tempCounter > 2: # 2 represents the number of times the test is allowed to fail 
    #  test(variables go here) # If it has failed twice, run it once more so that the error displays 
    # 

    VariableMarkerOfTestStatus+=1