如果单元测试失败,如何再次运行测试

时间:2014-09-27 12:09:41

标签: python selenium python-unittest

我有一个被HTMLRunner破坏的测试套装:

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)

如果代码失败,如何升级我的代码再次运行测试?

注意:我听说两次运行测试是不对的,但我需要重新运行它。

1 个答案:

答案 0 :(得分:0)

您始终可以尝试将测试保存为函数,并使用try / except在循环中运行它。 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
相关问题