Unit tests: Are they bad?

testing unittest mocks stubs

03|01|2010

Jay fields wrote a blog post about the maintainability of tests. Basically his takeaways were the following:

  1. You should probably write unit tests
  2. Only focus on the test cases which provides the most business value.
  3. Make tests more concise.

I have been on the other side of the fence, and have gone berserk writing tests for all cases, working hard to increase the coverage of my code. I have even followed the "Unit tests should only access itself, and should only interact with other objects via mocks and stubs" advice. All that led to just more maintainability, so I agree that there is a striking balance to maximize the ROI of your testing efforts.

Most of the data validation parts are important

In order to address maintainability and conciseness while still testing almost all of your validations, we just use macros. An example of this would be:

The bulk of the work here would be writing the matchers, but if you create enough reusable matchers anyway, the return of investment for writing the matchers would be very high. (I've written Rails 3 / Rspec 2 only matchers which can be found here.

Conciseness is king!

Again, I just have to stress the value of matchers here. A concrete case here is date parsing, and its importance to us (the product makers) that it just works. Example:

Test wisely

If you charge by the hour, you have to take into consideration that every programming decision you make will cost your client money. If you're working for your own product, you have to weigh the benefit of having 90% test coverage versus having all the cool effects in your UI for example.

blog comments powered by Disqus