Is Your Code Easy to Test?
- By Matt Stephens
Everyone (give or take) knows that it pays to unit-test your code; and it follows that it really pays to make sure your code is easy to test. This measure of your code’s quality could be called “testability”. But what exactly does testability mean? And wouldn’t it be great if the level of your code’s testability could be measured?
On his blog, Jay Flowers has written up a list of criteria that you can use to measure your code’s testability.
The criteria include questions like: “Is the constructor visibility public?”; “Is there a default constructor?”, “Is the test subject loosely coupled to other types?”, and so on.
An answer of no to any of these loaded questions earns a point (i.e. a red mark).So your ideal testability score would be 0.
It seems like an interesting exercise, but I very much doubt you’d want to go through all of your code measuring its testability - otherwise you'd never actually deliver anything to your users (working code or otherwise). Perhaps just one or two classes as a quick eye-opener. After that, you can focus on making your code nice and testable the first time you write it.