0

I have a two classes, like this:

class A:
    def someFunction(self):
        print("does work")

class B:
    def __init__():
        self.instance_a = A()

I want to mock A.someFunction() in a unit test. How do I mock the class variable, self.instance_a?

Brennan Macaig
  • 313
  • 1
  • 2
  • 9
  • 1
    `self.instance_a = A()` directly in the class? You'd get an error for that, is your question related to that or is that a typo while posting your question? – Abdul Aziz Barkat Aug 09 '23 at 13:57
  • Does this answer your question? [How to mock just the method inside the class](https://stackoverflow.com/questions/47407741/how-to-mock-just-the-method-inside-the-class) – Abdul Aziz Barkat Aug 09 '23 at 14:02
  • @AbdulAzizBarkat sorry, you're right. It should be inside `__init__():` – Brennan Macaig Aug 09 '23 at 14:08
  • I don't think the linked thread here answers my question, no. I am trying to mock the use of `self.instance_a` in class B. I wish to mock the entire class and all of it's functions. – Brennan Macaig Aug 09 '23 at 14:12
  • Are you asking this? https://stackoverflow.com/questions/39004540/mock-entire-python-class – mkrieger1 Aug 09 '23 at 14:28

1 Answers1

0

I suppose your production code is contained in the following file prod.py:

class A:
    def someFunction(self):
        print("does work")

class B:
    def __init__(self):   # <--- here in your code you miss the self argument
        self.instance_a = A()

A possible test code which mocks the method someFunction() of the class A is the following:

import unittest
from unittest import mock
from prod import B

# this function is used for mocking the method someFunction()
def someFunction_mocked():
    print('some function mocked')

class MyTestCase(unittest.TestCase):

    # the following method uses the production code
    def test_something(self):
        sut = B()
        sut.instance_a.someFunction()

    # the following method substitute the object points by instance_a by a Mock object
    def test_something_2(self):
        sut = B()
        with mock.patch.object(sut, 'instance_a') as mock_a:
            mock_a.someFunction.side_effect = someFunction_mocked
            sut.instance_a.someFunction()

if __name__ == '__main__':
    unittest.main()

The test code is saved in the same folder of the file prod.py. The function someFunction() is substituted by the function someFunction_mocked() which prints the message some function mocked instead the message does work.

The output of the execution of the test code is:

does work
some function mocked
..
----------------------------------------------------------------------
Ran 2 tests in 0.001s

OK

where the output is produces as following:

  • the message does work is printed by the test method test_something()
  • the message some function mocked is printed by the test method test_something_2(). In this second method is present the instruction mock.patch.object() which performs the mocking.
frankfalse
  • 1,553
  • 1
  • 4
  • 17