Bib*_*ngo 3 python unit-testing equality operator-overloading python-3.x
我正在尝试按照其他问题的代码示例在 Python 3.3 中实现标准相等运算符。我收到断言错误,但我不知道出了什么问题。我在这里错过了什么?
class RollResult:
def __init__(self, points, unscored_dice):
self.points = points
self.unscored_dice = unscored_dice
def __eq__(self, other):
return (self.points == other.points and self.unscored_dice == other.unscored_dice)
Run Code Online (Sandbox Code Playgroud)
这是测试。许多其他测试都已通过,因此基本设置是正确的。这是我对班级的第一次测试,我以前从未尝试过单元测试相等重载,所以这也可能是测试的错误。
class TestRollResultClass(unittest.TestCase):
def test_rollresult_equality_overload_does_not_test_for_same_object(self):
copy1 = RollResult(350,2)
copy2 = RollResult(350,2)
self.assertNotEqual(copy1,copy2)
Run Code Online (Sandbox Code Playgroud)
结果:
AssertionError: <greed.RollResult object at 0x7fbc21c1b650> == <greed.RollResult object at 0x7fbc21c1b650>
Run Code Online (Sandbox Code Playgroud)
你的__eq__()似乎工作正常。您正在使用assertNotEqual(),如果两个参数相等,它将引发 AssertionError 。您为断言中使用的每个对象提供了相同的参数RollResult,因此它们相等,因此失败。
看起来您要么想要使用assertEqual(),要么更改它以便copy1和 的copy2构造不同。
| 归档时间: |
|
| 查看次数: |
1361 次 |
| 最近记录: |