how do I set return_value of a mocked class instance when I don’t have access to the instance itself?

Suppose I have some function that instantiates and uses an instance of B, calling the member function bar on it.

How can I set return_value on a mocked instance of B when I’m testing my A class, given that I don’t have access to the instance of B? Maybe some code would illustrate this better:

import unittest
import unittest.mock
import pandas

class A:
    def foo(self):
        b = B()

class B:
    def bar():
        return 1

class MyTestCase(unittest.TestCase):
    def test_case_1(self, MockB):
        = 2
        a = A()
        self.assertEqual(, 2)

test_case = MyTestCase()

This fails with;

AssertionError: <MagicMock name='B().bar()' id='140542513129176'> != 2

Apparently the line = 2 didn’t modify the return value of the method.


You have just 1 mistake in your code. Replace this line: = 2

To: = 2

And it would work.

I assume the piece of code you pasted is just a toy example. If the class A and B lies on another file e.g. src/somedir/, don’t forget to patch the full path.

class MyTestCase(unittest.TestCase):


To further expand on this, you can see some usage in the docs:

>>> class Class:
...     def method(self):
...         pass
>>> with patch('__main__.Class') as MockClass:
...     instance = MockClass.return_value
...     instance.method.return_value = 'foo'
...     assert Class() is instance
...     assert Class().method() == 'foo'

So in your case:

  • is like calling a static method e.g. print(
  • is like calling a class/instance method e.g. print(MockB().bar())

To visualize this:

import unittest.mock

class SomeClass:
    def method(self):
        return 1

def test_mock(mock_class):

    mock_class.method.return_value = -10
    mock_class.return_value.method.return_value = -20


$ python3 
<MagicMock name='SomeClass' id='140568144584128'>
<MagicMock name='SomeClass()' id='140568144785952'>
  • As you can see, mock_class.return_value is the one used for instance operations such as SomeClass().method().