Cockatoo Software

Test Coverage

Published on 27 June, 2010 by Keith Pitty

If you are an enthusiastic proponent of test-driven development you may assume that you don’t need to worry about how well your automated tests cover your codebase. After all, you always write tests before you write your code, don’t you? And your tests comprehensively cover every logic path in your code, don’t they? If that is the case, you may be surprised when a test coverage tool shows some gaps.

Confidence in Automated Tests

The better coverage your automated tests have of your codebase, the more robust your application will prove to be as it changes over time.

Whether the code is being changed to fix a bug, satisfy an enhancement request or is being refactored to improve it’s internal design, the more complete the test suite, the less chance there will be of new bugs creeping into the code. One way of measuring the completeness of a test suite is to use a code coverage tool to report which code is covered and which is not covered by automated tests.

rcov: A Code Coverage Tool for Ruby

An example of an excellent, open source code coverage tool for Ruby programmers is rcov. Running rcov provides a report which brings into focus the Ruby code that has poor test coverage. To give you an idea, a small snippet follows:

rcov example

Each row of the report shows how much of the Ruby code in that file is covered by tests. A percentage is displayed as well as a graph. Naturally, the less red and the more green the better!

Drilling down into one of the rows of the report will highlight exactly which lines of code are covered by tests and which aren’t. Suffice to say, I’ve found rcov to be an excellent tool.

Other Tools

Naturally, there are many different programming languages and, consequently, many different code coverage tools. In the realm of Java programming, I am aware that Clover from Atlassian has an excellent reputation.

Conclusion

I hope this brief article has encouraged those of you involved with developing and testing software to use code coverage tools rather than blindly assume that your test coverage is good enough. And, if you need assistance with using code coverage tools, please do contact Cockatoo Software.

Keith Pitty is the owner of Cockatoo Software Pty Limited. He has over 30 years of professional software development experience and has a passion for providing software solutions that help people. Away from computers he loves sport, especially cricket, golf and Australian Rules football.