这是__repr__
一个名为的类中的方法Grid
def __repr__(self):
return 'Grid(%r, %r)' % (self.rows, self.cols)
unittest
我已经在一个模块中放置了一些基本测试,以检查是否eval
可以在没有失败的情况下进行相等测试,看起来像这样:
# One of the tests inside a unittest.TestCase subclass
def test_grid(self):
grid = Grid(3, 4)
self.assertEqual(eval(repr(grid)), grid)
现在,这是测试报告(我已将此测试与其他测试分开):
======================================================================
FAIL: test_grid (tests.test_core.TestCore)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/user/Desktop/sample/tests/test_core.py", line 14, in test_grid
self.assertEqual(eval(repr(grid)), grid)
AssertionError: Grid(3, 4) != Grid(3, 4)
----------------------------------------------------------------------
Ran 1 test in 0.003s
FAILED (failures=1)
断言异常消息让我更加困惑。不Grid(3, 4) != Grid(3, 4)
应该是False
吗?