2

I am trying to implement a standard equality operator in Python 3.3, following code samples from other questions. I'm getting an assertion error, but I can't figure out what's broken. What did I miss here?

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)

And here's the test. Many other tests are passing, so the basic setup is right. This is my first test of the class and I've never tried unit testing equality overloads before, so it may be the fault of the test as well.

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)

Result:

AssertionError: <greed.RollResult object at 0x7fbc21c1b650> == <greed.RollResult object at 0x7fbc21c1b650>                                        
4

1 回答 1

4

__eq__()似乎工作正常。您正在使用assertNotEqual(),如果两个参数相等,它将引发 AssertionError 。您为断言中使用的每个对象提供了相同的参数,RollResult因此它们相等,因此失败。

看起来您要么想要使用assertEqual(),要么对其进行更改,以便以不同copy1copy2方式构造。

于 2013-11-12T19:29:27.443 回答