How do you mock patch a python class and get a new Mock object for each instantiation?
Here’s a quick’n’dirty example to get you going: import mock import unittest class ClassToPatch(): def __init__(self, *args): pass def some_func(self): return id(self) class UUT(): def __init__(self, *args): resource_1 = ClassToPatch() resource_2 = ClassToPatch() self.test_property = (resource_1.some_func(), resource_2.some_func()) class TestCase1(unittest.TestCase): @mock.patch(‘__main__.ClassToPatch’, autospec = True) def test_1(self, mock1): ctpMocks = [mock.Mock(), mock.Mock()] ctpMocks[0].some_func.return_value = “funky” ctpMocks[1].some_func.return_value = … Read more