2013-04-12 111 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)