Wednesday, September 1, 2010

The Importance of Unit Testing

So, a while back, a friend and I started working on developing a game in a new language, mainly as a means of learning that language. Because of this, it didn't really matter how well the code was written, or if we tested it all that thoroughly. However, once our code seemed pretty stable, we decided to go ahead and open it up to the Open Source community. At this point, we started getting reports of some weird errors that we had never seen.

As it turns out, we had tested our game within the confines of what "normally" happens in a game, and in those situations, everything worked out fine. What we did not test is what happens when something "abnormal" happens, ie, the game runs out of money, the game is especially high scoring, etc. Now, of course, these do not often happen - in fact, we did not even think about them. Also, since we simply had a stack trace, it was at first confusing to try to hunt these bugs down, as the piece of code looked correct. Had we written unit tests to test both positive and negative test conditions, then theoretically, we would have caught this and our users wouldn't have had to report it. Realistically, however, you can't catch everything. Needless to say, there are unit tests for these conditions now.