claude computing
claude computing

Reputation: 329

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

Upvotes: 2

Views: 327

Answers (1)

blhsing
blhsing

Reputation: 106936

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

Upvotes: 3

Related Questions