pytest_assertrepr_compare only失败

2024-06-26 14:47:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我是pytest的新成员,正在研究如何定制断言。这个来自pytest网站的例子失败了,即使我比较Foo(1)==Foo(1)。知道为什么吗?在

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)]

测试:

^{pr2}$

结果:

$ 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

Tags: andinstancespytestrightfoopytestdef
1条回答
网友
1楼 · 发布于 2024-06-26 14:47:28

奇怪,你的测试对我来说通过了。起初它失败了,但我把__eq__()方法的缩进搞砸了。检查一下你没有混淆制表符和空格之类的东西。在

假设这不是您的问题,我建议您在使用Eclipse或PyCharm时尝试使用调试器运行它。查看相等语句比较的值。如果您使用的是Emacs,您可以尝试安装my live-py-mode来查看发生了什么。在

相关问题 更多 >