RCov for RSpec 2 not detecting coverage correctly? (not Rails!)

1.2k views Asked by At

Preface
I've just started getting into Ruby and try to not only learn the language but also some development strategies. As kind of a beginner I'm concentrating on Test and Behaviour Driven Development. (yes, I'm doing both for comparison purposes)

With my small software project I'm using

  • UnitTest (TDD)
  • Cucumber (BDD)
  • Rspec (TDD and BDD)

On various places I encountered RCov as a tool for telling me how much of my actual code I'm really testing.

I set up the following RakeTask in my Rakefile for the covarage analysis of the UnitTests:

desc "Run RCov to get coverage of UnitTests"
Rcov::RcovTask.new(:rcov_units) do |t|
  t.pattern = 'tests/**/tc_*.rb'
  t.verbose = true
  t.rcov_opts << "--html"
  t.rcov_opts << "--text-summary"
  t.output_dir = "coverage/tests"
end

This works fine and I'm getting a nice coloured HTML report in coverage/tests.

Problem Introduction
Similar I wrote the following RakeTasks for RCov to be used for coverage analysis of my specs:

desc "Run RCov to get coverage of Specs"
Rcov::RcovTask.new(:rcov_spec) do |t|
  t.pattern = 'spec/**/*_spec.rb'
  t.verbose = true
  t.rcov_opts << "--html"
  t.rcov_opts << "--text-summary"
  t.output_dir = "coverage/spec"
end

Problem Definition
However, the generated HTML report in coverage/spec looks somehow incomplete and almost failed.

None of the tested method bodies are marked as covered and thus red. However, I'm 100% sure they are executed within the specs. Only the lines def method_name(args) and class ClassName are marked 'green'. (as well lines with attr_reader :instance_variable)

Am I missing something?


$: ruby --version  
ruby 1.8.7 (2011-02-18 patchlevel 334) [x86_64-linux]

$: rspec --version  
2.8.0

$: rcov --version  
rcov 0.9.11 2010-02-28

$: rake --version  
rake, version 0.9.2
3

There are 3 answers

0
gioele On

Make sure that you are requiring rcov early enough.

From http://rubydoc.info/github/relevance/rcov/master/Rcov/CodeCoverageAnalyzer:

Note that you must require 'rcov' before the code you want to analyze is parsed (i.e. before it gets loaded or required). You can do that by either invoking ruby with the -rrcov command-line option or just:

1
Serhii Potapov On

I've got similar trouble. I am 100% sure that specs are not executed when I run them win RCov. But they do when I disable RCov.

Downgrading RSpec to version 2.6.0 helped me.

0
rlpowell On

I fixed this by adding:

RSpec::Core::Runner.autorun

to the bottom of my spec file; worked even with rspec 2.9