Version 0.8.1 of the rcov code coverage tool for Ruby addresses the
problems experienced by ruby 1.8.6-p11[01] (and in particular Leopard) users,
and includes a few new features.
If you are new to rcov, take a look at
this sample report.
In addition to indicating which code has been covered by your tests, rcov
allows you to navigate through your code easily. rcov records where each
method was called from and can generate fully cross-referenced reports,
letting you inspect the control flow. This is most useful when you're trying
to understand the overall organization of third-party code or you're refactoring.
"Spec-only" mode
You can indicate that only code executed inside a spec must be considered
covered (this is similar to --test-unit-only).
Here's a minimal example. Consider this target code:
require'rubygems'require'spec'require'bowling'describeBowlingdobefore(:each)do@bowling=Bowling.newendit"should score 0 for gutter game"do20.times{@bowling.hit(0)}@bowling.score.should==0endendBowling.new.num_throws
If you invoke rcov with the --spec-only option, you'll obtain:
$ rcov --text-coverage --no-color --spec-only bowling_spec.rb
.
Finished in 0.016369 seconds
1 example, 0 failures
================================================================================
bowling.rb
================================================================================
!! # bowling.rb
!! class Bowling
def hit(pins)
end
def score
0
end
!!
!! def num_throws
!! 1
!! end
!! end
================================================================================
bowling_spec.rb
================================================================================
!! require 'spec'
!! require 'bowling'
describe Bowling do
before(:each) do
@bowling = Bowling.new
end
it "should score 0 for gutter game" do
20.times { @bowling.hit(0) }
@bowling.score.should == 0
end
end
!!
!! Bowling.new.num_throws
Note how the num_throws method is not covered: it wasn't executed inside the
spec. This allows you to differentiate deliberate from "accidental" code coverage.