Sunday, March 20, 2005

Unit Testing Is Hard For All The Right Reasons

Last week I started a little experimental software project whose primary purpose is to apply standard software development techniques diligently and with rigor. This means coming up with a rough "top level" design, strict interface bounds, blackbox construction, and unit testing each component fully. The requirements for the project were laid out by a third party so that developers' influence wouldn't introduce a conflict of interest (ie coding the path of least resistance). I'm currently working on one of the main components of the system. Having come up with a strategy for the design and implemented the pieces of this component, I wrote up some unit tests for each piece and found it quite a challenge to cover all possible scenarios. In fact, writing the test cases is, at least for these relatively simple classes, harder and more time consuming than writing the classes themselves! I've already reaped some benefits from it however. In the testing phase I found a couple of one-off bugs that I might not have detected so soon without testing.

To help me out, I read through the following links and got some great pointers:

A Dozen Ways to Get the Testing Bug in the New Year (Particularly good)
Top 12 Reasons to Write Unit Tests
JUnit Primer

1 Comments:

Blogger outpostbabu said...

One drawback I've found to writing the unit tests is that sometimes you have to open up a class's API to test methods that are public but do not expose internal data. For example, I have a class that stores an internal cursor. The cursor's location is never known outside of the class and doesn't need to be, the class is a simulator. But to test the methods that move the cursor (they do this by increments), I think I need to know the value of the cursor. Otherwise I am not testing it directly. Is this an indication of a design flaw or just a drawback to external unit testing, where the test is run outside of the class itself?

1:10 AM  

Post a Comment

<< Home