为复杂对象重用pytest断言逻辑

时间:2017-06-26 10:33:11

标签: python pytest

如何重用对象的python“智能”报告?

E.g:

class Foo(object):
    def __init__(self, text, sequence):
        self.text = text
        self.sequence = sequence

    def __eq__(self, other):
        return type(self) == type(other) and self.text == other.text and self.sequence == other.sequence

    def __ne__(self, other):
        return not self.__eq__(other)

我想要的是获取断言报告,就像我断言原始字段一样,例如(当然有意义的是不打印满足比较运算符的属性 - 并将失败的属性报告连接在一起)。类似的东西:

    def test_magic():
>       assert Foo(text='a', sequence=[1]) == Foo(text='a', sequence=[1, 2])
E       assert Foo(text='a', sequence=[1]) == Foo(text='a', sequence=[1, 2])
E           attribute `sequence`:
E              [1] == [1, 2]
E              Right contains more items, first extra item: 2
E        Use -v to get the full diff

我可以通过编写一些插件等来调用内部比较器吗?或者推荐的解决方案是什么?

0 个答案:

没有答案