比较两个实例时如何获得更有帮助的错误消息?

How to get more helpful error messages when comparing two instances?

我正在比较两个 class 实例。下面是我的示例测试代码:

from unittest import TestCase

class Dog:
    age: int
    name: str

    def __eq__(self, other):
        if not isinstance(other, Dog):
            return False

        return self.age == other.age and self.name == other.name


class MyTests(TestCase):
    def test_compare(self):    
    d1 = Dog()
    d1.age = 1
    d1.name = 'dog1'

    d2 = Dog()
    d2.age = 2
    d2.name = 'dog2'

    self.assertEqual(d1, d2)

这会产生断言错误:

AssertionError: <test.Dog object at 0x0000020444FCA520> != <test.Dog object at 0x0000020444F97D60>

有没有办法直接比较两个实例并得到更有用的错误信息,比如导致断言失败的字段?我发现的唯一方法是直接比较字段,如下所示。有没有更简单的方法?

self.assertEqual(d1.age, d2.age)
self.assertEqual(d1.name, d2.name)

我刚刚发现的另一种方法是创建一个辅助函数来比较两个实例。

不再需要实施 __eq__

它不那么乏味,因为我只需要编写一次函数:

def test_compare(self):        
    d1 = Dog()
    d1.age = 1
    d1.name = 'dog1'

    d2 = Dog()
    d2.age = 2
    d2.name = 'dog2'

    self.__assertDogsAreEqual(d1, d2)

def __assertDogsAreEqual(self, dog1: Dog, dog2: Dog):
    self.assertEqual(dog1.age, dog2.age)
    self.assertEqual(dog1.name, dog2.name)
    

它现在提供更有用的错误消息:

Traceback (most recent call last):
  File "test.py", line 119, in test_temp
    self.__assertDogsAreEqual(d1, d2)
  File "test.py", line 122, in __assertDogsAreEqual
    self.assertEqual(dog1.age, dog2.age)
AssertionError: 1 != 2

我可能会接受这个作为答案,但我仍然很好奇是否有更好的方法。

不过,这看起来确实是目前最实用的方法。

您可以在 class 中定义一个 __repr__ 方法。这用于创建对象的字符串表示形式以进行调试。

class Dog:
    age: int
    name: str

    def __init__(self, age, name):
        self.age = age
        self.name = name

    def __eq__(self, other):
        if not isinstance(other, Dog):
            return False

        return self.age == other.age and self.name == other.name

    def __repr__(self):
        return f"{self.__class__.__name__}({repr(self.age)}, {repr(self.name)})"

print(Dog(5, "Fido"))  # Prints "Dog(5, 'Fido')"