Is it possible to use assert_equal to compare objects? I keep seeing this error:
AssertionError: <ex49.parser.Sentence object at 0x01F1BAF0> !=
<ex49.parser.Sentence object at 0x01F1BB10>
The relevant code fragment:
def test_parse_subject():
testsentence = "princess go east"
result = lexicon.scan(testsentence)
Sent = parse_sentence(result)
ResultSent = Sentence(('subject', 'princess'),
('verb', 'go'),
('object', 'east'))
print ResultSent.subject
print ResultSent.verb
print ResultSent.object
print Sent.subject
print Sent.verb
print Sent.object
assert_equal(Sent, ResultSent)
The print outputs on screen suggests that the objects have the same contents – yet the assertion error shows up. Why is this? Is there some way to use assert_equal to override this?
I believe you need to implement the __eq__ method on the Sentence class.
Python unittest documentation
Python data model documentation
An example:
Now comment the __eq__ method and see the difference.