Python Testing

I just finished my first major use of Python testing tools. I just wrote a patch for Trac to add groups support to its fine grained permissions.

As I started writing the patch I was having trouble wrapping my head around all the different possibilities for conflicting rules and how they should take precedence. I started testing a few of them by hand, but I quickly realized that making sure that I covered all the scenarios was going to be difficult. Thankfully there were some examples in the code for using doctest.

Once I had figured out a couple of things about using doctest, I found it was the first testing tool I've actually enjoyed using. Doctest allows you to write your tests as basic textual documentation, then insert snippets from the Python command line. Doctest will run the command line sections and make sure that the output matches the document. This makes it incredibly easy to write tests, and the tests are quite readable and useful as examples.

On a sidenote, I got a complimentary copy of the Python Cookbook today. They contacted me a while back about using some submissions I had made on the cookbook website, but when they released the list of included recipies none of my submissions seemed to be included, so I wasn't expecting to receive a copy of the book. I actually considered picking up a copy at PyCon, but now I'm glad I waited.