Recipe 7.24. Analyzing Code Coverage with Rcov


Problem

Contributed by: Diego Scataglini

You've written your application and plenty of tests to go with it. Now you want to find the areas you may have missed in your test coverage.

Solution

Rcov is a code coverage analysis tool for Ruby. You can use it with Rails applications to analyze your test coverage. To get started, open a terminal window, and install the Rcov RubyGem:

$ sudo gem install rcov             

Windows users who installed Ruby with the One-Click Installer should choose the mswin32 version.

Once you have rcov installed, change your working directory to the root of the Rails application you want to analyze, and run the following command:

$ rcov test/units/*             

The output of this command will be similar to that of rake test:units. The magic happens when rcov finishes running your tests and produces a detailed report. After running the command, you'll have a folder named coverage in the root of your application directory. This is where you'll find a coverage report based on the tests that you just ran. To see the code coverage report, open the index.html file this directory in a browser.

Discussion

Rcov is a great tool for spotting deficiencies in test coverage. This solution discusses only the quickest and easiest way to use Rcov in your work flow. Rcov provides many different analysis modes (bogo-profile, "intentional testing," dependency analysis, etc.) and output choices (XHTML, decorated text output, text report). You can filter out folders or files, and set thresholds so the report will not show files with coverage above a certain percentage. The differential code coverage report is particularly useful. This report tells you if you've added new code that is not covered by the tests, or if changes to the application mean that some of the code is no longer tested. To run a differential coverage report, you first run rcov with the --save option to save the coverage status; later, you can run Rcov with the -D option to see what has changed since the last saved report.

Figure 7-1 shows the main page of the generated Rcov code coverage report. Notice how easy it is to see where your test coverage is weakest.

Figure 7-1. The main page of an Rcov code coverage report


The index page contains links to each class of your application. It's easy to see what's going on: red indicates untested code. From the main report, you can drill down into any of the classes listed and see the details of that class's coverage.

Figure 7-2 shows the detailed view for the forum.rb class. On this page, the color of each line indicates whether that part of the code was covered by your tests. Here you can see that three lines of code aren't tested. Your code coverage is represented as a ratio of lines covered by tests, to either the total lines of code (e.g., percent code coverage), or the total number of lines including whitespace (e.g., percent total coverage).

Figure 7-2. Coverage report detail


Run rcov --help, and experiment with the options. For example, rcov --callsites --xrefs test/unit/*.rb produces a hyperlinked and cross-referenced report, showing you which methods were called and from where.

See Also

  • The official Rcov site at http://eigenclass.org/hiki.rb?rcov




Rails Cookbook
Rails Cookbook (Cookbooks (OReilly))
ISBN: 0596527314
EAN: 2147483647
Year: 2007
Pages: 250
Authors: Rob Orsini

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net