def __new__(mcs, name, bases, classdict, **kwargs):
"""__new__ method of metaclass."""
# TODO: maybe this could be just put in the base class instead of in
# the meta class.
def assert_X_from_iterables(self, x=lambda: True, *args, **kwargs):
func = getattr(self, x.__name__, x)
for i, zipargs in enumerate(zip(*args)):
with self.subTest(iteration=i, arguments=zipargs):
func(*zipargs)
return None
classdict['assert_X_from_iterables'] = assert_X_from_iterables
@property
def verbose(cls):
"""Return the verbosity setting of the currently running unittest.
This function 'scans' the frame looking for a 'cls' variable.
Returns:
int: the verbosity level.
0 if this is the __main__ file
1 if run with unittests module without verbosity (default in
TestProgram)
2 if run with unittests module with verbosity
"""
frame = inspect.currentframe()
# Scans frames from innermost to outermost for a TestProgram
# instance. This python object has a verbosity defined in it.
while frame:
cls = frame.f_locals.get('cls')
if isinstance(cls, unittest.TestProgram):
return cls.verbose
# Proceed to one outer frame.
frame = frame.f_back
return 0
# METACLASS: set verbosity.
classdict['verbose'] = verbose
return type.__new__(mcs, name, bases, classdict)
评论列表
文章目录