Closed
Description
Originally reported by: BitBucket: PeterSlickers, GitHub: PeterSlickers
I recently started using pytest und thus read some the documentation.
On page "http://pytest.org/latest/assert.html" in section "Defining your own assertion comparison" I found an example 'test_foocompare.py'.
class Foo:
def __init__(self, val):
self.val = val
f1 = Foo(1)
f2 = Foo(2)
assert f1 == f2
Class 'foo' is very simple and it does not define any of Pythons magic methods for comparison like ne, cmp etc. Thus, a comparison of two instances of Foo will be accomplished by comparing the unique ID of the two objects:
id(f1) == id(f2)
Even if both instances are initiated with the same value, the comparison for equality will fail:
id(Foo(1)) == id(Foo(1)) --> False
Applying the deep inspection produces the following misleading result:
'=================================================== FAILURES ===================================================
_________________________________________________ test_compare _________________________________________________
def test_compare():
f1 = Foo(1)
f2 = Foo(1)
> assert f1 == f2
E assert Comparing Foo instances:
E vals: 1 != 1
test_foocompare.py:9: AssertionError
1 failed in 0.01 seconds
With best regards
Peter