wolever / parameterized

Parameterized testing with any Python test framework
Other
833 stars 105 forks source link

Custom decorator for expanded tests #166

Open wn opened 1 year ago

wn commented 1 year ago

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 commented 9 months ago

does any one know a way to do that?

xmnlab commented 9 months ago

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:
        ...