Sample (fully) cross-referenced report at
This release includes two RubyGems packages: a binary one for Win32 and
platform-independent one for all those with a compiler, (or a lot of
if willing to run rcov in pure-Ruby mode), so
gem install rcov
rcov is a code coverage tool for Ruby. It is commonly used for viewing
test coverage of target code. It features:
- fast execution: 20-300 times faster than previous tools
- multiple analysis modes: standard, bogo-profile, “intentional
- detection of uncovered code introduced since the last run
- fairly accurate coverage information through code linkage inference
- cross-referenced XHTML and several kinds of text reports
- support for easy automation with Rake and Rant
What’s new in 0.6.0
See http://eigenclass.org/hiki.rb?rcov+0.6.0 for the detailed change
0.6.0 features a new differential coverage mode
which will tell you when you’ve added new code that was not covered by
!!! Uncovered code introduced in app/models/article.rb
### app/models/article.rb:44 # Find all articles on a certain date def self.find_all_by_date(year, month = nil, day = nil) !! from, to = self.time_delta(year, month, day) !! Article.find(:all, :conditions => ["articles.created_at
BETWEEN ? AND ? AND articles.published != 0", from, to], :order =>
‘articles.created_at DESC’, :include => [:categories, :trackbacks,
# Find one article on a certain date
rcov 0.6.0 ships with a compiler plugin for integration with vim
(contributions for other editors/IDEs welcome).
Cross-referenced reports, which were recently introduced, have been
to indicate where methods are called from and which methods were called
each line (–xrefs). In addition to that, cross-referenced report
is now over 4 times faster for applications with deep call stacks (such
The last version is available at
How do I use it?
In the common scenario, your tests are under test/ and the target code
(whose coverage you want) is in lib/. In that case, all you have to do
use rcov to run the tests (instead of testrb), and a number of XHTML
with the code coverage information will be generated, e.g.
rcov -Ilib test/*.rb
will execute all the .rb files under test/ and generate the code
report for the target code (i.e. for the files in lib/) under coverage/.
target code needs not be under lib/; rcov will detect is as long as it
require()d by the tests. rcov is smart enough to ignore “uninteresting”
files: the tests themselves, files installed in Ruby’s standard
etc. See rcov --help for the list of regexps rcov matches filenames
rcov can also be used from Rake; see README.rake or the RDoc
for more information.
rcov can output information in several formats, and perform different
of analyses in addition to plain code coverage. See rcov --help for a
description of the available options.
See http://eigenclass.org/hiki.rb?rcov (once again) for screenshots.
Take a look at a sample code coverage report generated by rcov at
The text report (also used by default in RcovTasks) resembles
| File | Lines | LOC | COV |
|lib/rcov.rb | 572 | 358 | 91.3% |
|Total | 572 | 358 | 91.3% |
91.3% 1 file(s) 572 Lines 358 LOC
The (undecorated) textual output with execution count information looks
$ rcov --no-html --text-counts b.rb
a, b, c = (1…3).to_a |
10.times do |
a += 1 |
20.times do |i| |
b += i |
b.times do |
c += (j = (b-a).abs) > 0 ? j : 0 |
rcov can detect when you’ve added code that was not covered by your unit
$ rcov --text-coverage-diff --no-color test/*.rb Started ....................................... Finished in 1.163085 seconds. 39 tests, 415 assertions, 0 failures, 0 errors ================================================================================ !!!!! Uncovered code introduced in lib/rcov.rb ### lib/rcov.rb:207 def precompute_coverage(comments_run_by_default = true) changed = false lastidx = lines.size - 1 if (!is_code?(lastidx) || /^__END__$/ =~ @lines[-1]) &&
!! # mark the last block of comments
!! @coverage[lastidx] ||= :inferred
!! (lastidx-1).downto(0) do |i|
!! break if is_code?(i)
!! @coverage[i] ||= :inferred
(0…lines.size).each do |i|
next if @coverage[i]
line = @lines[i]
- reported problem with heredocs: they were not being marked as a whole
the “header” wasn’t reported by Ruby.
- reported problem with the last line of literal data structs not being
covered if there was stuff after the end delimiter
- reported problem with blocks were the first line is not being marked
and ditto for the last line when end/} is followed by more stuff
- reported that the last comment block was not being marked even when
it was the last thing in the file
rcov is released under the terms of Ruby’s license.
rcov includes xx 0.1.0, which is subject to the following conditions:
ePark Labs Public License version 1
Copyright © 2005, ePark Labs, Inc. and contributors
All rights reserved.
Redistribution and use in source and binary forms, with or without
are permitted provided that the following conditions are met:
- Redistributions of source code must retain the above copyright
list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright
this list of conditions and the following disclaimer in the
and/or other materials provided with the distribution.
- Neither the name of ePark Labs nor the names of its contributors
used to endorse or promote products derived from this software
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.