Showing posts with label systems. Show all posts
Showing posts with label systems. Show all posts

Friday, October 06, 2006

On unit testing

I've been hacking on the Buildbot for a month or so now. I've released two patches for it, a Bonsai Poller and a Tinderbox Mail Notifier. I noticed recently that Buildbot has a lot of test cases in it's tree. I've heard the phrases "unit test" and "test case" before but I didn't know what they were until today. My patches are more or less complete and have been tested but I still think it would be nice to have test cases for them. The buildbot tree will change, bonsai and tinderbox may change, and this should be a relatively simple way to get me introduced to the concept of unit testing.

I spent an hour this afternoon reading Buildbot test cases and other unit testing documents. The Buildbot uses Twisted's unit testing framework for it's tests. I found the docs for those here. This went into the specifics of writing test cases with Twisted but after reading it I still didn't feel comfortable with the concept. There was a link to Dive Into Python on the Twisted page. This is an excellent read for anyone new to unit testing. It goes from no source to test case to working code.

I've done much of what is involved in making a test case without actually making a test case. One of the key things I like about unit testing is that it defines your API. I was chatting about this in #seneca and shaver said "that also has the nice effect of keeping you honest about what you really _need_". This is a very good thing for someone like me! I went through 3 or 4 revisions of my BonsaiPoller classes before coming to the final version. I don't think this is necessarily a bad thing but I know this happened because I didn't think enough about what it should look like before attempting to implement it. Before writing the final revision I actually wrote a script that defined how I was going to use the class. Looking back on it, that was a rudimentary test case. It wasn't well organized, it wasn't testing small pieces at a time, but from a design standpoint it accomplished the same thing.

I see lots of similarities to unit testing and things I've encountered in my classes. We often get "test mains" from professors to test our assignments before handing them in. These are often in the form of small tests to every part of our classes. In my systems course we just finished doing Scenarios. To me, a scenario seems like a test case for use case description. There is obvious differences but while a test case tests the validity of code, a scenario tests the validity of a use case description.

Unit testing is very interesting! I plan to adopt it whenever I can. I expect that I will go from idea to working code quicker if I do it right.