unit testing - Mock patch decorators python -
hi mock decorator since don't want calling/executing function. can't seem find solution below code
# decorator located in project # located in custom.mydecorators.decorator_file.custom_decorator base = declarative_base() def custom_decorator(func): @wraps(func) def wrapper(*args, **kwargs): print("this message still printed when try patch function") try: #code here my_var = coolclass() retval = func(*args, **kwargs) except exception e: #rollback code here raise e return retval return wrapper
now i'm trying patch using code
patch('custom.mydecorators.decorator_file.custom_decorator', lambda x: x).start() class testmockdecoratorscallingclass(unittest.testcase): def test_should_return_success_if_decorators_are_mocked(self): # code here
my decorators work in non unittest file. if mock decorator fails saying local variable 'my_var' referenced before assignment
note: my_var inside decorator i'm trying mock/patch print message still executed when try patch it
Comments
Post a Comment