def expand(cls, input):
""" A "brute force" method of parameterizing test cases. Creates new
test cases and injects them into the namespace that the wrapped
function is being defined in. Useful for parameterizing tests in
subclasses of 'UnitTest', where Nose test generators don't work.
>>> @parameterized.expand([("foo", 1, 2)])
... def test_add1(name, input, expected):
... actual = add1(input)
... assert_equal(actual, expected)
...
>>> locals()
... 'test_add1_foo_0': <function ...> ...
>>>
"""
def parameterized_expand_wrapper(f):
stack = inspect.stack()
frame = stack[1]
frame_locals = frame[0].f_locals
base_name = f.__name__
get_input = cls.input_as_callable(input)
for num, args in enumerate(get_input()):
p = param.from_decorator(args)
name_suffix = "_%s" %(num, )
if len(p.args) > 0 and isinstance(p.args[0], six.string_types):
name_suffix += "_" + cls.to_safe_name(p.args[0])
name = base_name + name_suffix
frame_locals[name] = cls.param_as_standalone_func(p, f, name)
return nottest(f)
return parameterized_expand_wrapper