我用我自己的类重写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)
?
答案 0 :(得分:2)
您忘了将self
传递给assertEqual
:
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)
答案 1 :(得分:1)
您正在调用assertEqual
作为类方法,而不传递实例:这就是Python抱怨该方法未绑定的原因。
您应该使用:
super(MyTestCase, self).assertEqual(first, second, msg)