Mock parent class __init__ method

后端 未结 1 1986
不思量自难忘°
不思量自难忘° 2021-02-05 12:57

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

相关标签:
1条回答
  • 2021-02-05 13:39

    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"])
    
    0 讨论(0)
提交回复
热议问题