import unittest from functools import wraps class DependencyError(Exception): def __init__(self, _type=0): self._type = _type def __str__(self): if self._type == 0: return f'Dependency name of test is required!' if self._type == 1: return f'Dependency name of test can not the case self!' def depend(case=''): if not case: raise DependencyError _mark = [] # 执行过程中搜集的测试结果 def wrap_func(func): @wraps(func) def inner_func(self): if case == func.__name__: raise DependencyError(1) _r = self._outcome.result # _r.skipped获取跳过case,case前需要加上@skip(reason),reason必填 _f, _e, _s = _r.failures, _r.errors, _r.skipped # 三个值都是list,list中包含的是元组 if not (_f or _e or _s): # 都是空,则可以执行测试用例 return func(self) if _f: _mark.extend([fail[0] for fail in _f]) if _e: _mark.extend([error[0] for error in _e]) # 错误的 用例放在mark中 if _s: _mark.extend([skip[0] for skip in _s]) is_skip = case in str(_mark) unittest.skipIf( is_skip, f'The pre-depend case : {case} has failid! Skip the specified case!' )(func)(self) return inner_func return wrap_func