Skip to content

Commit

Permalink
Merge pull request #151 from wolever/pr-135
Browse files Browse the repository at this point in the history
Support async functions under patch decorator (updates to #135)
  • Loading branch information
wolever committed Mar 2, 2023
2 parents 8e29f4a + d856203 commit f00dbab
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
19 changes: 15 additions & 4 deletions parameterized/parameterized.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,15 @@ def dummy_func(*args, **kwargs):
return dummy_func

if hasattr(func, 'patchings'):
is_original_async = inspect.iscoroutinefunction(func)
func = dummy_wrapper(func)
tmp_patchings = func.patchings
delattr(func, 'patchings')
for patch_obj in tmp_patchings:
func = patch_obj.decorate_callable(func)
if is_original_async:
func = patch_obj.decorate_async_callable(func)
else:
func = patch_obj.decorate_callable(func)
return func


Expand Down Expand Up @@ -547,13 +551,20 @@ def parameterized_expand_wrapper(f, instance=None):
delete_patches_if_need(f)

f.__test__ = False

return parameterized_expand_wrapper

@classmethod
def param_as_standalone_func(cls, p, func, name):
@wraps(func)
def standalone_func(*a):
return func(*(a + p.args), **p.kwargs)
if inspect.iscoroutinefunction(func):
@wraps(func)
async def standalone_func(*a):
return await func(*(a + p.args), **p.kwargs)
else:
@wraps(func)
def standalone_func(*a):
return func(*(a + p.args), **p.kwargs)

standalone_func.__name__ = name

# place_as is used by py.test to determine what source file should be
Expand Down
24 changes: 24 additions & 0 deletions parameterized/test.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# coding=utf-8

import inspect
import sys
import mock
from unittest import TestCase
try:
Expand Down Expand Up @@ -558,3 +559,26 @@ class TestUnicodeDocstring(object):
def test_with_docstring(self, param):
""" Это док-стринг, содержащий не-ascii символы """
pass

if sys.version_info.major == 3 and sys.version_info.minor >= 8:
from unittest import IsolatedAsyncioTestCase

class TestAsyncParameterizedExpandWithNoMockPatchForClass(IsolatedAsyncioTestCase):
expect([
"test_one_async_function('foo1')",
"test_one_async_function('foo0')",
"test_one_async_function(42)",
"test_one_async_function_patch_decorator('foo1', 'umask')",
"test_one_async_function_patch_decorator('foo0', 'umask')",
"test_one_async_function_patch_decorator(42, 'umask')",
])

@parameterized.expand([(42,), "foo0", param("foo1")])
async def test_one_async_function(self, foo):
missing_tests.remove("test_one_async_function(%r)" % (foo, ))

@parameterized.expand([(42,), "foo0", param("foo1")])
@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))

0 comments on commit f00dbab

Please sign in to comment.