Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom decorator for expanded tests #166

Open
wn opened this issue Apr 13, 2023 · 2 comments
Open

Custom decorator for expanded tests #166

wn opened this issue Apr 13, 2023 · 2 comments

Comments

@wn
Copy link

wn commented Apr 13, 2023

I was wondering if its possible to inject decorators to expanded functions.

# this won't work
@inject_a_flag_to_setup
@parameterized.expand([1,2,3])
def foo(x):
    return x
# suggestion
@parameterized.expand([1,2,3], decorators=[inject_a_flag_to_setup])
def foo(x): 
    return x

to become

@inject_a_flag_to_setup
def foo_1_1(x):
  return x

@inject_a_flag_to_setup
def foo_2_2(x):
  return x

@inject_a_flag_to_setup
def foo_3_3(x):
  return x
@xmnlab
Copy link

xmnlab commented Dec 7, 2023

does any one know a way to do that?

@xmnlab
Copy link

xmnlab commented Dec 7, 2023

I manage to have this working with this:

def setup(f: Callable):
    def _f(self, some_arg: str):
        self.some_function_from_my_test_class()
        return f(self, some_arg)
    _f.__name__ = f.__name__
    _f.__annotations__ = f.__annotations__
    _f.__docs__ = getattr(f, "__docs__", "")
    return _f

for some reason it didn't work with functools.wraps, what would be much better

and I call it with:

class MyTestCase(TestCase):
    @parameterized.expand(LIST_OF_INPUTS)
    @setup
    def test_my_func(self, some_arg: str) -> None:
        ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants