Python pytest_assertrepr_compare only失败
我是pytest的新手,正在研究如何定制断言。这个来自pytest网站的例子失败了,即使我比较Foo(1)==Foo(1)。知道为什么吗 配置:Python pytest_assertrepr_compare only失败,python,unit-testing,testing,pytest,Python,Unit Testing,Testing,Pytest,我是pytest的新手,正在研究如何定制断言。这个来自pytest网站的例子失败了,即使我比较Foo(1)==Foo(1)。知道为什么吗 配置: # 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 == "==":
# 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)]
测试:
结果:
$ 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
奇怪,你的考试对我来说通过了。一开始它失败了,但我把
\uuuuueq\uuuu()
方法的缩进搞砸了。检查你没有混淆标签和空格之类的东西
假设这不是您的问题,如果您使用Eclipse或PyCharm,我建议您尝试使用调试器运行它。查看等式语句正在比较的值。如果您使用的是Emacs,您可以尝试安装my以查看发生了什么。奇怪的是,您的测试对我来说是通过的。一开始它失败了,但我把
\uuuuueq\uuuu()
方法的缩进搞砸了。检查你没有混淆标签和空格之类的东西
假设这不是您的问题,如果您使用Eclipse或PyCharm,我建议您尝试使用调试器运行它。查看等式语句正在比较的值。如果您使用的是Emacs,您可以尝试安装my来查看发生了什么
$ 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