How to mock super class method that is called from sub class — python unittest

I have a class A and B(A) in the following way (ignoring the imports).

# file_1
class A:

    def __init__():
        ...

    def some_method():
        ...

# file_2
class B(A):

    def __init__():
        super().__init__()

        self.some_method()

# test_file
class BTest(unittest.TestCase):

    @mock.patch('module.file_1.A.some_method')
    def test_something(self, mock_some_method):
        b = B()
    ...

Running the test will call the some_method real method, not the mocked one. How to mock some_method before instantiating class B.

Leave a Comment