I am trying to write some unittests on a class which is derived from another, but I have some difficulties to mock the parent class init method, which afaik you
For the first solution, change the return value of the __init__
method to None
.
@patch("Derived.Parent.__init__")
def test_init_001(self, mock_parent_init):
mock_parent_init.return_value = None # <---
a = Derived("I am Derived")
mock_parent_init.assert_called_with("I am Derived")
self.assertEquals(a.parent_list, ["PARENT", "DERIVED"])
For the second solution, patch Parent.Imported
:
@patch("Parent.Imported") # <---
def test_init_002(self, mock_parent_init):
a = Derived("I am Derived")
mock_parent_init.assert_called_with("I am Derived")
self.assertEquals(a.parent_list, ["PARENT", "DERIVED"])