Friday, May 02, 2008

Unit Testing

Writing unit tests may be a bit of an acquired taste, but I don't think that the value of writing them can be overstated for any moderately complex software project.

Having a full suites of well-written unit tests gives developers the peace of mind to confidently refactor code that isn't their own, or to refactor code that they have written but are no longer happy with.

Writing unit tests before writing the actual code has an added benefit as well. It provides a developer with an extra opportunity to think about how clients of the code are going to use it. This in turn helps identify potential bugs much earlier in the development cycle.

Add it all up, and the end result is that the act of writing unit tests results in higher quality code. The popularity of NUnit helped Microsoft realize this, which is why Visual Studio 2005 Team System delivered integrated unit testing tools.

salesforce.com came to the same conclusion about the importance of unit testing and made it a fundamental component of Apex, their on demand programming language. Writing unit tests for Apex is a remarkably simple task, which is good, because Apex is the first programming language to require developers to write unit tests for their code - a minimum of 75% code coverage is required for all production Apex code. I anticipate that more languages will begin to follow suit, especially those designed to run in multi-tenant environments.

Having seen the value of unit testing personally and having seen the industry's acceptance of the value of unit testing, it's a bit surprising that more university computer science curriculums don't emphasize writing unit tests. Maybe it's because the teaching assistants like keeping their unit testing harnesses a secret...

No comments: