From 8bc5b58a25d42be4ff97b7c76214161c644e4a12 Mon Sep 17 00:00:00 2001 From: David Wolever Date: Sun, 26 Mar 2023 21:39:31 -0400 Subject: [PATCH] Fix #138: support for class-level mock.patch.multiple --- CHANGELOG.txt | 2 ++ parameterized/parameterized.py | 8 ++++---- parameterized/test.py | 20 ++++++++++++++------ 3 files changed, 20 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.txt b/CHANGELOG.txt index 8196da0..0c3894b 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -13,6 +13,8 @@ * Allow str, bytes, and any non-iterable input to be passed to ``@parameterized`` without wrapping in a tuple (https://github.com/wolever/parameterized/pull/157) + * Fix class-level ``mock.patch.multiple`` + (https://github.com/wolever/parameterized/issues/138; thanks @ArthurGW) 0.8.1 (2021-01-09) * Add README and LICENSE to pypi sdist package diff --git a/parameterized/parameterized.py b/parameterized/parameterized.py index d94be9b..56dc535 100644 --- a/parameterized/parameterized.py +++ b/parameterized/parameterized.py @@ -612,12 +612,12 @@ def parameterized_expand_wrapper(f, instance=None): def param_as_standalone_func(cls, p, func, name): if inspect.iscoroutinefunction(func): @wraps(func) - async def standalone_func(*a): - return await func(*(a + p.args), **p.kwargs) + async def standalone_func(*a, **kw): + return await func(*(a + p.args), **p.kwargs, **kw) else: @wraps(func) - def standalone_func(*a): - return func(*(a + p.args), **p.kwargs) + def standalone_func(*a, **kw): + return func(*(a + p.args), **p.kwargs, **kw) standalone_func.__name__ = name diff --git a/parameterized/test.py b/parameterized/test.py index f124182..6c71f79 100644 --- a/parameterized/test.py +++ b/parameterized/test.py @@ -291,21 +291,29 @@ def test_mock_patch_standalone_function(foo, mock_umask): ) ) - +@mock.patch.multiple("os", umask=mock.DEFAULT) class TestParameterizedExpandWithMockPatchMultiple(TestCase): expect([ - "test_mock_patch_multiple_expand(42, 'umask', 'getpid')", + "test_mock_patch_multiple_expand_on_method(42, 'umask', 'getpid')", + "test_mock_patch_multiple_expand_on_class(16, 'umask')", ]) @parameterized.expand([(42, )]) - @mock.patch.multiple("os", umask=mock.DEFAULT, getpid=mock.DEFAULT) - def test_mock_patch_multiple_expand(self, param, umask, getpid): + @mock.patch.multiple("os", getpid=mock.DEFAULT) + def test_mock_patch_multiple_expand_on_method(self, param, umask, getpid): missing_tests.remove( - "test_mock_patch_multiple_expand(%r, %r, %r)" %( + "test_mock_patch_multiple_expand_on_method(%r, %r, %r)" %( param, umask._mock_name, getpid._mock_name ) ) + @parameterized.expand([(16, )]) + def test_mock_patch_multiple_expand_on_class(self, param, umask): + missing_tests.remove( + "test_mock_patch_multiple_expand_on_class(%r, %r)" %( + param, umask._mock_name, + ) + ) expect("standalone", [ "test_mock_patch_multiple_standalone(42, 'umask', 'getpid')", @@ -684,4 +692,4 @@ async def test_one_async_function(self, foo): @mock.patch("os.umask") async def test_one_async_function_patch_decorator(self, foo, mock_umask): missing_tests.remove("test_one_async_function_patch_decorator(%r, %r)" % - (foo, mock_umask._mock_name)) \ No newline at end of file + (foo, mock_umask._mock_name))