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

2024-09-25 00:29:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一套测试服被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)

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

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


Tags: ofnamereportifmainunittestsuiterunner
1条回答
网友
1楼 · 发布于 2024-09-25 00:29:10

始终可以尝试将测试另存为函数,并使用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

相关问题 更多 >