Home:ALL Converter>Mock python decorator in unit tests

Mock python decorator in unit tests

Ask Time:2016-04-27T03:42:50         Author:scdekov

Json Formatter

I am trying to test a function that is decorated. Is there a way to mock a decorator and test function in isolation, when decorator is already applied?

import mock


def decorator(func):
    def wrapper(*args, **kwargs):
        return 1
    return wrapper


def mocked(func):
    def wrapper(*args, **kwargs):
        return 2
    return wrapper


@decorator
def f():
    return 0


with mock.patch('test.decorator') as d:
    d.side_effect = mocked
    assert f() == 2  # error

Author:scdekov,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/36874321/mock-python-decorator-in-unit-tests
yy