File size: 908 Bytes
065fee7 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
import types
import functools
from more_itertools import always_iterable
def parameterize(names, value_groups):
"""
Decorate a test method to run it as a set of subtests.
Modeled after pytest.parametrize.
"""
def decorator(func):
@functools.wraps(func)
def wrapped(self):
for values in value_groups:
resolved = map(Invoked.eval, always_iterable(values))
params = dict(zip(always_iterable(names), resolved))
with self.subTest(**params):
func(self, **params)
return wrapped
return decorator
class Invoked(types.SimpleNamespace):
"""
Wrap a function to be invoked for each usage.
"""
@classmethod
def wrap(cls, func):
return cls(func=func)
@classmethod
def eval(cls, cand):
return cand.func() if isinstance(cand, cls) else cand
|