Tip #25 – Leverage Code Coverage Tools
Code coverage tools help with defect detection and prevention as part of continuous integration. I emphasized the importance of automated tests with regard to resuable assets – code coverage is no less important.
Code coverage reporting provides a window into the health of your codebase.
- the depth of your automated tests – what percentage of code is being executed by the tests?
- are there unused blocks of code – including functions, even classes – that are not executed?
- they are invaluable in helping detect complex code – are some classes too bulky/ripe for refactoring? Code coverage will make these painfully obvious to the developer.
Code coverage thus not only helps you identify opportunities for better tests it also helps you eliminate dead/bloated code and rip apart code that is too complex. I wrote earlier post on developing reusable assets for use first – I recommend identifying extensions/flexibility on a as-needed basis for reusable asset evolution. Code coverage will point out areas for achieving this objective as well.
Getting started is very simply – ff you are using apache maven2 for instance in the java world, it is extremely simple to set these up and use them by adding a few entires to the pom.xml file. Below is the plugin declaration for Cobertura – for plugin’s full usage instructions go here.
You can run code coverage by simply executing: mvn cobertura:cobertura from the project root folder- this will generate instrumented java classes, execute automated unit tests, and produce a code and branch coverage report.