Skip to content

Commit

Permalink
Merge pull request #150 from wolever/pr-148
Browse files Browse the repository at this point in the history
Updates to PR #148, for testing + merge
  • Loading branch information
wolever committed Mar 2, 2023
2 parents 7c792c4 + 18347dc commit b12366f
Showing 1 changed file with 24 additions and 11 deletions.
35 changes: 24 additions & 11 deletions parameterized/parameterized.py
Expand Up @@ -323,9 +323,6 @@ def detect_runner():
return _test_runner_guess


def _get_parent_locals():
return inspect.currentframe().f_back.f_back.f_locals


class parameterized(object):
""" Parameterize a test case::
Expand Down Expand Up @@ -466,13 +463,30 @@ def check_input_values(cls, input_values):
return [ param.from_decorator(p) for p in input_values ]

@classmethod
def expand(cls, input, name_func=None, doc_func=None, skip_on_empty=False, frame_locals=None,
**legacy):
def expand(cls, input, name_func=None, doc_func=None, skip_on_empty=False,
namespace=None, **legacy):
""" 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.
:param input: An iterable of values to pass to the test function.
:param name_func: A function that takes a single argument (the
value from the input iterable) and returns a string to use as
the name of the test case. If not provided, the name of the
test case will be the name of the test function with the
parameter value appended.
:param doc_func: A function that takes a single argument (the
value from the input iterable) and returns a string to use as
the docstring of the test case. If not provided, the docstring
of the test case will be the docstring of the test function.
:param skip_on_empty: If True, the test will be skipped if the
input iterable is empty. If False, a ValueError will be raised
if the input iterable is empty.
:param namespace: The namespace (dict-like) to inject the test cases
into. If not provided, the namespace of the test function will
be used.
>>> @parameterized.expand([("foo", 1, 2)])
... def test_add1(name, input, expected):
... actual = add1(input)
Expand All @@ -499,10 +513,9 @@ def expand(cls, input, name_func=None, doc_func=None, skip_on_empty=False, frame
name_func = name_func or default_name_func

def parameterized_expand_wrapper(f, instance=None):
frame_locals = namespace
if frame_locals is None:
_frame_locals = _get_parent_locals()
else:
_frame_locals = frame_locals
frame_locals = inspect.currentframe().f_back.f_locals

parameters = cls.input_as_callable(input)()

Expand All @@ -523,8 +536,8 @@ def parameterized_expand_wrapper(f, instance=None):
# of param_as_standalone_func so as not to share
# patch objects between new functions
nf = reapply_patches_if_need(f)
_frame_locals[name] = cls.param_as_standalone_func(p, nf, name)
_frame_locals[name].__doc__ = doc_func(f, num, p)
frame_locals[name] = cls.param_as_standalone_func(p, nf, name)
frame_locals[name].__doc__ = doc_func(f, num, p)

# Delete original patches to prevent new function from evaluating
# original patching object as well as re-constructed patches.
Expand Down Expand Up @@ -592,7 +605,7 @@ class TestUserAccessLevel(TestCase):
)

class_name_func = class_name_func or default_class_name_func

if classname_func:
warnings.warn(
"classname_func= is deprecated; use class_name_func= instead. "
Expand Down

0 comments on commit b12366f

Please sign in to comment.