After a lot of study and debugging UnitTest code, later found a can also re-run Setup () and Deardown () solution, that is to modify the source, we re-build a module suite class to cover the original Testsuite class
Instance code:
[Python]View PlainCopy
- Class Suit (UnitTest. TestSuite):
- def run (self, result, debug=False):
- Failcount = 1#失败总运行次数
- Class_num = 1
- TopLevel = False
- if GetAttr (result, ' _testrunentered ', false) is False:
- result._testrunentered = TopLevel = True
- For test in self :
- Case_num = 1
- if Result.shouldstop:
- Break
- Success_flag = True
- While Success_flag:
- if _isnotsuite (test):
- self._teardownpreviousclass (test, result)
- self._handlemodulefixture (test, result)
- self._handleclasssetup (test, result)
- Result._previoustestclass = test.__class__
- if (GetAttr (test.__class__, ' _classsetupfailed ', False) or
- GetAttr (result, ' _modulesetupfailed ', False)):
- if Class_num > Failcount:
- Success_flag = False
- Else:
- Time.sleep (5)
- Result._previoustestclass = None
- Print ' class%s '%s re-initialized execution '% (test.__class__,class_num)
- Class_num + = 1
- Continue
- If not debug:
- Test (Result)
- Else:
- Test.debug ()
- if result.result[-1][0]==1 or result.result[-1][0]==2:#结果为fail和err用例判断
- if Case_num > Failcount:
- Success_flag = False
- Else:
- print ' use case%s '%s re-executed '% (test,case_num)
- Case_num + = 1
- Else:
- Success_flag = False
- if TopLevel:
- Self._teardownpreviousclass (None, result)
- Self._handlemoduleteardown (Result)
- result._testrunentered = False
- return result
Then test using
Alltests=suit. Suit ()
Alltests.addtest(Widgettestcase ("Testdefaultsize"))
Runner =htmltestrunner.htmltestrunner (stream=fp,verbosity=2,title= ' Android test Report ', description= ' use case execution: ',)
Runner.run (alltests)
After running, the use cases with failed re-run will be printed in the test report for easy reference.
Python+selenium+unitest use case failed re-run