my_test_case.py
from unittest import TestCase, case
import time
class TestCase_(TestCase):
# 是否开启失败重跑
FAILURE_REPEAT_RUN_FLAG = True
# 失败重跑尝试次数
FAILURE_REPEAT_RUN_NUM = 5
# 设置重跑时间间隔
REPEAT_TIME_INTERVAL = 3
def run(self, result=None):
run_count = 1
orig_result = result
if result is None:
result = self.defaultTestResult()
startTestRun = getattr(result, 'startTestRun', None)
if startTestRun is not None:
startTestRun()
result.startTest(self)
testMethod = getattr(self, self._testMethodName)
if (getattr(self.__class__, "__unittest_skip__", False) or
getattr(testMethod, "__unittest_skip__", False)):
# If the class or method was skipped.
try:
skip_why = (getattr(self.__class__, '__unittest_skip_why__', '')
or getattr(testMethod, '__unittest_skip_why__', ''))
self._addSkip(result, self, skip_why)
finally:
result.stopTest(self)
return
expecting_failure_method = getattr(testMethod,
"__unittest_expecting_failure__", False)
expecting_failure_class = getattr(self,
"__unittest_expecting_failure__", False)
expecting_failure = expecting_failure_class or expecting_failure_method
outcome = case._Outcome(result)
try:
self._outcome = outcome
while True:
with outcome.testPartExecutor(self):
self.setUp()
if outcome.success:
outcome.expecting_failure = expecting_failure
with outcome.testPartExecutor(self, isTest=True):
testMethod()
outcome.expecting_failure = False
with outcome.testPartExecutor(self):
self.tearDown()
self.doCleanups()
for test, reason in outcome.skipped:
self._addSkip(result, test, reason)
self._feedErrorsToResult(result, outcome.errors)
if outcome.success:
if expecting_failure:
if outcome.expectedFailure:
self._addExpectedFailure(result, outcome.expectedFailure)
else:
self._addUnexpectedSuccess(result)
else:
result.addSuccess(self)
# =======================重跑===================
if not self.FAILURE_REPEAT_RUN_FLAG:
return result
if run_count < self.FAILURE_REPEAT_RUN_NUM:
if not outcome.success:
try:
result.result.pop()
except Exception as e:
pass
run_count += 1
time.sleep(self.REPEAT_TIME_INTERVAL)
continue
# ==============================================
return result
finally:
result.stopTest(self)
if orig_result is None:
stopTestRun = getattr(result, 'stopTestRun', None)
if stopTestRun is not None:
stopTestRun()
# explicitly break reference cycles:
# outcome.errors -> frame -> outcome -> outcome.errors
# outcome.expectedFailure -> frame -> outcome -> outcome.expectedFailure
outcome.errors.clear()
outcome.expectedFailure = None
# clear the outcome, no more needed
self._outcome = None
测试代码
from BeautifulReport import BeautifulReport
import unittest
import time
from unittest.case import skip
# coding=utf-8
import sys
import functools
import traceback
from unittest_learn.my_test_case import TestCase_
class MyTest2(TestCase_):
def test001(self):
'''test001'''
print('步骤1')
print(1)
print('步骤2')
def test002(self):
print(1)
def test003(self):
time.sleep(1)
assert 1==2
if __name__=='__main__':
dis = unittest.defaultTestLoader.discover(r'D:\eclipse\python_test\src\unittest_learn',
'mytest*.py')
print(dis)
result = BeautifulReport(dis)
result.report(filename='myreport.html', description='myClass')