Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Patch/Mock class method but still call original method

Tags:

I want to use patch to record all function calls made to a function in a class for a unittest, but need the original function to still run as expected. I created a dummy code example below:

from mock import patch  class A(object):     def __init__(self):         self._a = 1  class B(A):     def __init__(self):         super(B, self).__init__() # TypeError: super() argument 1 must be type, not MagicMock         self._b = 11      def bar(self, b):         self._b = self._b + 1 + b      def foo(self, b):         self.bar(b)  class MockB(B):     def foo(self, b):         super(MockB, self).foo(self, b)  @patch('main.B') def main(b_class):     b_class.side_effect = MockB      b = B()      print b._b # 11     b.foo(0)     print b._b # 12  main() 

In my case, the instance of the class b = B() is not actually in the main function but in another module, so I can't Mock the instance. I need it to generically be a decorator for all instances of B.

Summary: I am not sure how to individually mock the class method on it's own, but still call the original method. After, I want to use something like call_args_list where I can see all calls made to foo().

like image 285
mojo1mojo2 Avatar asked Apr 29 '19 07:04

mojo1mojo2


People also ask

What is the difference between mock and MagicMock?

Mock vs. So what is the difference between them? MagicMock is a subclass of Mock . It contains all magic methods pre-created and ready to use (e.g. __str__ , __len__ , etc.). Therefore, you should use MagicMock when you need magic methods, and Mock if you don't need them.

What is Side_effect in mock Python?

side_effect: A function to be called whenever the Mock is called. See the side_effect attribute. Useful for raising exceptions or dynamically changing return values. The function is called with the same arguments as the mock, and unless it returns DEFAULT , the return value of this function is used as the return value.

What is the difference between mock and patch?

Patching vs Mocking: Patching a function is adjusting it's functionality. In the context of unit testing we patch a dependency away; so we replace the dependency. Mocking is imitating. Usually we patch a function to use a mock we control instead of a dependency we don't control.

What is MagicMock Python?

MagicMock. MagicMock objects provide a simple mocking interface that allows you to set the return value or other behavior of the function or object creation call that you patched. This allows you to fully define the behavior of the call and avoid creating real objects, which can be onerous.


1 Answers

I think you are looking for the wraps Mock parameter. Search the official documentation for wraps. Accessing attributes returns a mock object. Calling methods gives the real method result instead, if a return value is not configured for the mock.

like image 171
progmatico Avatar answered Sep 20 '22 14:09

progmatico