2017-03-23 6 views
0

私はpytestで新しく、アサーションをカスタマイズする方法を見ていました。私がFoo(1)== Foo(1)を比較しても、pytestのウェブサイトのこの例は失敗します。どんな考え?pytest_assertrepr_compareが失敗するだけです

http://docs.pytest.org/en/latest/assert.html#defining-your-own-assertion-comparison

構成:

# 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 

答えて

0

奇妙な、あなたのテストは私のために渡します。最初は失敗しましたが、私は__eq__()メソッドのインデントを台無しにしました。タブやスペースなどが混在していないことを確認してください。

あなたの問題ではないと仮定して、EclipseまたはPyCharmを使用している場合は、デバッガで実行することをお勧めします。等価ステートメントがどのような値を比較しているかを確認してください。 Emacsを使用している場合、私のlive-py-modeをインストールして何が起こっているのかを調べることができます。

関連する問題