Question

So I've got some methods returning some rather complex objects, and I'd like to test this output. I construct a unit test using unittest, and I find that the computed object and the expected object I constructed aren't equal. Too bad.

What gets me though, is that the error message doesn't print the two objects, only some bland repr. Each object defines __str__ and __eq__, and I know that the __str__, as verified by placing a raise ValueError in the __str__ function.

What gives?

Was it helpful?

Solution

Many places in Python use the repr() of an object instead of the str() as the repr() usually specifies one exact instance.

If you want to see the actual str() instead, many assertWhatever methods allow a custom error message:

assertEqual(my_obj1, my_obj2, '%s != %s' % (my_obj1, my_obj2))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top