Search code examples
pythonpytestdecorator

Python - use same function with different sets of decorators


I'm new to Python, so please sorry if the question is dumb.

I have a few pytest test functions, which can be used both as sanity tests and as regression tests. So the functions themselves are the same, but the values used in @pytest.mark.parametrize decorators are different.

On top of this, I need to mark my tests so the CI jobs will know what to run - so the final code looks like

@pytest.mark.service1
@pytest.mark.parametrize('args', regression_object_to_take_args_from)
def regression_test(args): 
    pass

@pytest.mark.service1
@pytest.mark.parametrize('args', sanity_object_to_take_args_from)
def sanity_test(args): 
    pass

where regression_test and sanity_test have the same exact content.

How can I use the same function instance for both cases?


Solution

  • There are different approaches here. Probably the simplest is to just use a helper function:

    def _same_logic(args):
        # same stuff
        ...
    
    @pytest.mark.service1
    @pytest.mark.parametrize('args', regression_object_to_take_args_from)
    def regression_test(args): 
        _same_logic(args)
    
    @pytest.mark.service1
    @pytest.mark.parametrize('args', sanity_object_to_take_args_from)
    def sanity_test(args): 
        _same_logic(args)
    

    Or, another approach literally decorates the same function with different sets of decorators, just do the decoration manually:

    def _same_logic(args):
        # same stuff
        ...
    
    regression_test = pytest.mark.service1(
        pytest.mark.parametrize('args', regression_object_to_take_args_from)(
            _same_logic
        )
    )
    
    regression_test = pytest.mark.service1(
        pytest.mark.parametrize('args', sanity_object_to_take_args_from)(
            _same_logic
        )
    )
    

    Reminder,

    @foo
    @bar(some_value)
    def baz(arg):
        ...
    

    is the same as:

    def baz(arg):
        ...
    
    baz = foo(bar(some_value)(baz))