Bibliotango
Bibliotango

Reputation: 193

Why is my simple __eq__ implementation giving an AssertionError?

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>                                        

Upvotes: 3

Views: 1513

Answers (1)

Andrew Clark
Andrew Clark

Reputation: 208605

Your __eq__() seems to be working correctly. You are using assertNotEqual(), which will raise the AssertionError if the two arguments are equal. You provided the same arguments to each RollResult object used in the assertion so they are equal, hence the failure.

It looks like you either want to be using assertEqual(), or change it so that copy1 and copy2 are constructed differently.

Upvotes: 5

Related Questions