RCov: Must have development tool for TDDers

March 20, 2009

Although you are using a test driven development to create your latest app, how do you know that you tests are covering all your code? If done correctly it should, as you need to write tests, then when they fail, write the code to make them pass, but how many of us follow that religiously? Even if you do, how does somebody else know you are?

Enter code coverage, as defined by Wikipedia it is “a measure used in software testing” which “describes the degree to which the source code of a program has been tested”. Sounds snazzy eh? In layman terms it tells you whether a line of code has a test which covers it or not. An good example is a branch, one execution path may be tested, where as another may not. 100% code coverage means that you code has tests for every single line of code. In an ideal world all projects should 100% coverage, but in reality it is most likely much lower than this, however certain saftey-critical ssystems specifiy this as a requirement.

For my project (in Rails) I am using RCov. If you are already using RSpec, once you have RCov installed you can use the ready-made rake task:

rake spec:rcov

This produces pretty HTML reports as shown below!

picture-1Looks like I’ve been slacking…