import uuid from unittest import TestCase class MyClass: """ Pretend this class represents an object in a database or whatever. The point is there are some properties which cannot be set/known ahead of time. """ def __init__(self, property_1: int, property_2: str, property_3: float) -> None: self.uuid = uuid.uuid4() self.property_1 = property_1 self.property_2 = property_2 self.property_3 = property_3 def build_list(n: int) -> list[MyClass]: """Pretend this method is complicated""" return [MyClass(i, str(i), float(i)) for i in range()] class MyTest(TestCase): def setUp(self): # For these test cases, I want "equality" of two MyClass instances # to mean that they have the same properties, other than uuid. # The uuid is not important and not what I'm checking for here. self.addTypeEqualityFunc( MyClass, lambda first, second, msg: all( getattr(first, attr) == getattr(second, attr) for attr in ["property_1", "property_2", "property_3"] ), ) def test_build_list_1(self): # This test case fails, which is disappointing because it's so tidy! self.assertEqual( build_list(3), [MyClass(0, "0", 0.0), MyClass(1, "1", 1.0), MyClass(2, "2", 2.0)], ) def test_build_list_2(self): # This test case passes... my_list = build_list(3) # ...but this feels stupid: self.assertEqual(my_list[0], MyClass(0, "0", 0.0)) self.assertEqual(my_list[1], MyClass(1, "1", 1.0)) self.assertEqual(my_list[2], MyClass(2, "2", 2.0))