16.5. Measuring Code Coverage

It is incredibly useful to know what parts of your code are not getting exercised. This helps you to know when you have areas that are lacking in unit tests. In some cases, the coverage tool itself can show you bugs. For example, suppose you have an if statement that “should be” executed about half the time. If you find that it is never executed, that is an easily pinpointed bug.

The rcov command-line utility (and its corresponding library) are the work of Mauricio Fernandez. Install it as a gem.

To run it in the simplest case, simply invoke it with your program as a parameter:

rcov myfile.rb

Your program will run, but at the same time, rcov will be gathering statistics. By default, it will create a directory called ...

Get The Ruby Way: Solutions and Techniques in Ruby Programming, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.