Unit Testing Costs and Benefits

There is always a lot of discussions about the costs and benefits of unit testing. The opposition was exacerbated by the adoption of Test-Driven Development (TDD), a technique that recommends writing unit tests before you write the code. In this blog post, Steven Sanderson discusses unit testing with a costs and benefits perspective.

Steven Sanderson gives two reasons for writing his post: the need to understand the reasons that make unit testing worthwhile and the fact that we don’t achieve 100% unit test coverage is not necessarily a bad thing.

According to him, there are also two basic reasons to write unit tests:

“Unit tests help you to design some code while you’re writing it, and also help to verify that your implementation actually does what you intended it to do.”

This lead to the conclusion that “the benefit of unit testing is correlated with the non-obviousness of the code under test.” The post then lists the different costs related to unit tests, both when you write them and you maintain them. Steven Sanderson provides a matrix where you can compare the benefits and the costs of unit tests. Depending on where your code is located, this should help you decide whether or not you should write unit tests. The blog contains a final discussions about the merits of Test-Driven Development and integration testing.

Read the complete blog post on http://blog.stevensanderson.com/2009/11/04/selective-unit-testing-costs-and-benefits/