Completely new to Python unit testing. A bit of code.
class a():
def __init__(param1, param2)
self.param1 = param1
self.param2 = param2
self._problem = SomeOtherClass()
def method1(self):
self._problem.some_method()
def method2(self):
self._problem.some_other_method()
def problem(self):
return self._problem
Now , I am writing unit tests for class a. For testing the init, method1 and method2, I somehow need to mock the instance of SomeOtherClass along with its 2 methods that has been called. Now I though of using a fixture as:
@pytest.fixture
def problem():
pro = mock.MagicMock()
pro.some_method = mock.MagicMock()
pro.some_other_metho = mock.MagicMock()
Now the test :
def test_init_success(problem):
a_instance = a(12, 13)
assert a_instance.problem == problem
The other option I have is to use a patch. Which one should I use?
EDIT1 : The assertion in test_init_will fail as problem was not passed as param for the init method. Is there any workaround for this ?