I'm comparing two class instances with each other. Below is my sample test code:
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)
This generates an assertion error:
AssertionError: <test.Dog object at 0x0000020444FCA520> != <test.Dog object at 0x0000020444F97D60>
Is there a way to compare two instances directly and get a more helpful error message, such as the field that caused the assertion to fail? The only way I've found is to compare fields directly like below. Is there a less tedious way?
self.assertEqual(d1.age, d2.age)
self.assertEqual(d1.name, d2.name)