Version 0.8.0 of the rcov code coverage tool for Ruby introduces new
output modes and superior emacs integration. It also fixes a nasty bug in the rcovrt extension. If you have been getting
segfaults or bus errors when using rcov with RSpec, give 0.8.0 a try.

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.

class ExampleTest < Test::Unit::TestCase
def test_example_with_nothing_in_rescue
assert_raises(RuntimeError) do
Example.new.with_nothing_in_rescue
end
end
def test_example_with_something_in_rescue
assert_raises(RuntimeError) do
Example.new.with_something_in_rescue
end
end
end

and this spec:

require 'example'
require 'rubygems'
gem 'rspec'
require 'spec'

context "Example" do
specify "with_nothing_in_rescue should raise error" do
lambda {Example.new.with_nothing_in_rescue}.should_raise
end
specify "with_something_in_rescue should raise error" do
lambda {Example.new.with_something_in_rescue}.should_raise
end
end

All the tests and specs pass and RCov correctly reports that the rescue block in #with_something_in_rescue is covered. RCov does not, however, report that the rescue block in #with_nothing_in_rescue is covered.

mfp 2007-02-28 (Wed) 13:24:13

First of all, thank you for the detailed report.

rcov depends on Ruby's event_hook system, which unfortunately doesn't report any event in the above case (so there's no way for rcov to know whether the rescue clause has actually been executed or not).

One workaround would be adding a dummy expression in the rescue clause, like

rescue => e
nil
end

This would be enough to make Ruby report a "line" event and allow rcov to detect that line.

Hooray! - James (2007-02-28 (Wed) 09:13:00)

Thank you, and thank you to all the contributors. I really enjoy this tool.

Combined coverage report - Brittain (2007-02-28 (Wed) 09:52:50)

In 0.7 rcov introduced coverage reports that would aggregate your functionals and units, but I've never found adequate examples to get this working. Would you post something straightforward for this please?

Thanks.

mfp 2007-02-28 (Wed) 13:28:02

You can find some Rake tasks that allow you to aggregate functional, unit and integration runs in the rcov FAQ.
("How can I run all rails tests, like in the example?")

Weird String bug - Toby (2007-05-24 (Thr) 13:01:48)

<code>
print %Q{

{'one'}

two

}
</code>

If you run this with rcov, the code runs fine, but in the coverage report you'll discover that lines 1-3 were reported as not run. One work around is to use another charactor as the multi-line string delimiter.