我正在尝试测试返回类型__repr__
。它不是一个字符串,那它是什么?这里发生了什么事?
import unittest
class MyClass(unittest.TestCase):
class Dog(object):
def __init__(self, initial_name):
self._name = initial_name
def get_self(self):
return self
def __repr__(self):
return "Dog named '" + self._name + "'"
def runTest(self):
fido = self.Dog("Fido")
self.assertEqual("Dog named 'Fido'", fido.get_self()) #Fails!
test=MyClass("runTest")
runner=unittest.TextTestRunner()
runner.run(test)
运行这个给出:
FAIL: runTest (__main__.MyClass)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/xxxxx/fido.py", line 15, in runTest
self.assertEqual("Dog named 'Fido'", fido.get_self())
AssertionError: "Dog named 'Fido'" != Dog named 'Fido'
----------------------------------------------------------------------
Ran 1 test in 0.006s
FAILED (failures=1)
我怎样才能让这个测试通过?