2013-04-12 140 views
1

類中重寫assertEqual便當我重寫unittest.TestCase用我自己的課堂,我想一些額外的功能添加到assertEqual類型錯誤派生

class MyTestCase(unittest.TestCase):   
    def __init__(self,*args, **kwargs): 
     unittest.TestCase.__init__(self, *args, **kwargs) 

    def _write_to_log(self): 
     print "writing to log..." 

    def assertEqual(self, first, second, msg=None): 
     self._write_to_log() 
     unittest.TestCase.assertEqual(first, second, msg) 

但我正在逐漸TypeError: unbound method assertEqual() must be called with TestCase instance as first argument (got int instance instead)

回答

1

你打電話給assertEqual作爲一個類方法,而不通過一個實例:這就是爲什麼Python抱怨方法是unbound

你應該使用:

super(MyTestCase, self).assertEqual(first, second, msg) 
2

你忘了在selfassertEqual經過:

unittest.TestCase.assertEqual(self, first, second, msg) 

你真的應該使用super()整個覆蓋:

class MyTestCase(unittest.TestCase):   
    def __init__(self,*args, **kwargs): 
     super(MyTestCase, self).__init__(*args, **kwargs) 

    def assertEqual(self, first, second, msg=None): 
     self._write_to_log() 
     super(MyTestCase, self).assertEqual(first, second, msg)