Search code examples
pythonunit-testingtestingpython-mock

group 2+ mock.patch into one


Say I have:

import mock
...

@mock.patch("function_1")
@mock.patch("function_2")
def my_test(self, f1, f2):
    f1.return_value="foo"
    f2.return_value="bar"
    ...

function_1 and function_2 are very similar and mocked out in multiple test functions. I would love to modularize this pattern (of patching the two functions). Is there such a way? The ideal outcome would look something like below.

@grouppatch("function_1_and_2")
def my_test(self):
    ...

Solution

  • You can use a function that takes target objects to be patched as parameters, and returns a decorator function that iterates through the target objects to use mock.patch to patch the object for the decorated function:

    def grouppatch(*targets):
        def decorator(func):
            for target in targets:
                func = mock.patch(target)(func)
            return func
        return decorator
    

    so that:

    @grouppatch('builtins.bool', 'builtins.int')
    def my_test(mock_bool, mock_int):
        mock_bool.return_value = True
        mock_int.return_value = 100
        print(bool(False), int(10))
    
    my_test()
    

    outputs:

    True 100