jeudi 23 mars 2017

pytest_assertrepr_compare only fails

I'm new at pytest and was taking a look on how to customize assertions. This example from pytest's website just fails, even if I compare Foo(1) == Foo(1). Any idea why?

http://ift.tt/2nc0ioE

# content of conftest.py
from test_foocompare import Foo
def pytest_assertrepr_compare(op, left, right):
    if isinstance(left, Foo) and isinstance(right, Foo) and op == "==":
        return ['Comparing Foo instances:',
                '   vals: %s != %s' % (left.val, right.val)]

# content of test_foocompare.py
class Foo:
    def __init__(self, val):
        self.val = val

    def __eq__(self, other):
        return self.val == other.val

def test_compare():
    f1 = Foo(1)
    f2 = Foo(1)
    assert f1 == f2

$ pytest -q test_foocompare.py
F
======= FAILURES ========
_______ test_compare ________

    def test_compare():
        f1 = Foo(1)
        f2 = Foo(1)
>       assert f1 == f2
E       assert Comparing Foo instances:
E            vals: 1 != 1

Aucun commentaire:

Enregistrer un commentaire