How can I check if a @patched-out python method is called, without changing its behavior?

前端 未结 1 1239
我在风中等你
我在风中等你 2021-01-15 04:41

I\'ve been using the python mock module for my test-cases. Frequently I decorate my test cases with the @patch(\'my_method\') decorator. Then in the body of the

相关标签:
1条回答
  • 2021-01-15 05:06

    If you want to use patch just for its call-logging functionality, without mocking out the original function's behavior, then specify the original function as the side_effect of the mock:

    @patch('my_function', side_effect=my_function)
    ...
    

    The side_effect name is misleading. If a mock has a side_effect function, calling the mock will call the side_effect and return or raise whatever the side_effect returns or raises. Using the original function as side_effect means we get the original function's behavior. (side_effect can be a few other kinds of thing besides functions, but we don't need that functionality here.)

    0 讨论(0)
提交回复
热议问题