Python Unit Test - compare content of iterables in Python < 2.7
Question: How can I check if an array of custom class is sorted in the order that I
want? I know that there is assertSequenceEqual in Python 2.7 and up, but I'm
using Python 2.6, and while I might be able to do this using a forloop... is
there better something in Python < 2.7 that I can use?

    
    
      actual_sorted = list.sort()
    
      expected_sorted = [MyClass("wed", 20), 
                         MyClass("wed", 19,),
                         MyClass("thu", 18,),
                         MyClass("wed", 17,)]
    
      self.assertEqual(actual_sorted, expected_sorted) # Well, this doesn't work.
    

Thank you in advance!


Answer: As BrenBarn said, you have to add an `__eq__` method. I was writing a full
example in parallel before I saw his answer:

    
    
    import unittest
    
    
    class MyClass(object):
        def __init__(self, day_name, day_num):
            self.day_name = day_name
            self.day_num = day_num
    
        def __eq__(self, other):
            return other.day_name == self.day_name and other.day_num == self.day_num
    
    
    class ATestCase(unittest.TestCase):
        def test_foo(self):
            actual_sorted = [MyClass("wed", 20),
                               MyClass("wed", 19),
                               MyClass("thu", 18),
                               MyClass("wed", 17)]
    
            expected_sorted = [MyClass("wed", 20),
                               MyClass("wed", 19),
                               MyClass("thu", 18),
                               MyClass("wed", 17)]
            self.assertEqual(actual_sorted, expected_sorted)
    
    
    if __name__ == '__main__':
        unittest.main()
    

