How to perform a transitivity mocking of classes/objects in python unit testing

38 Views Asked by At

I am new to unittesting in python and was trying to mock a object explained like below:

i have three scripts:

  • test1.py (Class : test1)
  • test2.py (Class : test2)
  • test3.py (Class : test3)

test2.py is creating object of test3.py test1.py is creating object of test2.py

I want to mock test3 inside test1, so that when it creates object of test2 it consider mocking object of test3.

test3.py:

class test3():

    def __init__(self):
        print("Created object os myclass")
        self.name = "rahul"

test2.py:

from test3 import test3

class test2():

    def __init__(self):
        self.sirName = "Rohilla"
        self.test3object = test3()

test1.py:

from unittest import mock
from test3 import test3
from test2 import test2

class test1():

    @mock.patch('test3.test3') # -> here i tried __name__+'.test3' but that didn't work
    def fun2(self, myclass):
        mockObject = mock.MagicMock()
        mockObject = {}
        myclass.return_value = mockObject
        test2Object = test2() # -> here test2Object doesn't have mockobject

main1 = test1()

main1.fun2()

My problem is test2Object doesn't contain mockedobject for test3. can someone suggest me any other way?

0

There are 0 best solutions below