Crawlicious

tools for web business

Make Unit Testing Python Nicer

| 0 comments

When doing unittesing in python it can get really annoying to compare dictionaries using the unittest.TestCase functions.  If they fail, the output is very messy, so I wrote these functions to help do the comparison.  Actually, there are a bunch of functions that I made to make testing a lot better in python.  You can download the file from TestAssertions.py.

Here is an example of what you can do with it…


self.assertEqualLists(l1, l2)

self.assertEqualDictionaries(d1, d2)

self.assertEqualDoms(d1, d2)

self.assertEqualMoney(m1, m2)

In addition to these helpful functions that produce much nicer output than the ugly self.assertEquals, I have also included  a really great class that will gather all test cases into a suite automatically.

Here is how you use that….


def suite():
 return DynamicTestSuite(modulename=__file__, namespace=globals(), skiptests=['MyCommonTest']).makeSuite()

One more thing, I used to try to pretty print deeply nested dictionaries and lists, but now I just use the following to get a really nice representation of deeply nested lists and dictionaries.  pprint is now my friend!


import pprint
 print "out=%s"%pprint.pformat(output)
 print "exp=%s"%pprint.pformat(expected)

Have fun!

Leave a Reply