Python – How do you simulate a function that has a decorator applied in a unit test?

How do you simulate a function that has a decorator applied in a unit test?… here is a solution to the problem.

How do you simulate a function that has a decorator applied in a unit test?

@lolcat_decorator1
@loldog_decorator2
@lolrat_decorator3
def lolanimal(*args, **kwargs):
  ....

I’m sure I’ll unit test these decorators separately. But these decorators will first do something with the arguments passed to lolanimal, and then lolanimal will do something with those modified parameters (one of the decorators may insert a new keyword argument into **kwargs.).

So what’s the best way to simulate it?

Thanks

Solution

My first thought was to create a _lolanimal method that encapsulates all the actual functionality of lolanimal and then make lolanimal a wrapper that passes _lolanimal. You can then run all tests against the _lolanimal with data that you have full control over.

You can also create a second decorator that will read the config value or some kind of test mode stuff before the first decorator, if the config value will override the lolspecific decorator is true….

Related Problems and Solutions