# Coverage does not work

**URL:** <https://www.ruby-forum.com/t/coverage-does-not-work/51265>\
**Category:** Rails\
**Created:** [December 28, 2005, 10:17pm UTC](https://www.ruby-forum.com/t/coverage-does-not-work/51265 "2005-12-28T22:17:40Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![szczepiq](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/fbc32d/32.png) [@szczepiq](https://www.ruby-forum.com/u/szczepiq)\
**Post date:** [December 28, 2005, 10:17pm UTC](https://www.ruby-forum.com/t/coverage-does-not-work/51265/1 "2005-12-28T22:17:40Z")

</div>

Hi,

I’ve installed coverage (gem install coverage) and try to run tests with  
-rcoverage option on, but following error occurs:  
ruby: No such file to load – coverage (LoadError)

I try:  
ruby -rcoverage mytest.rb

1. I have rubygems in evn var RUBYOPT
2. Gem seems to be installed properly (I can read doc via gems, the  
files  
exist in the …/gem/… dirs)

Does anyone know what is the problem?

Thanks in advance  
Szczepan

---

<div class="post-metadata">

**Author:** ![szczepiq](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/fbc32d/32.png) [@szczepiq](https://www.ruby-forum.com/u/szczepiq)\
**Post date:** [December 28, 2005, 10:29pm UTC](https://www.ruby-forum.com/t/coverage-does-not-work/51265/2 "2005-12-28T22:29:43Z")

</div>

Try Insurance instead, I’ve found it works pretty well.

[http://insurance.rubyforge.org/](http://insurance.rubyforge.org/)

- Jamie

---

<div class="post-metadata">

**Author:** ![szczepiq](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/fbc32d/32.png) [@szczepiq](https://www.ruby-forum.com/u/szczepiq)\
**Post date:** [December 29, 2005, 2:43am UTC](https://www.ruby-forum.com/t/coverage-does-not-work/51265/3 "2005-12-29T02:43:48Z")

</div>

I had the same problem, and while I didn’t really figure out why it was  
happening, I figured out an easy workaround.

For me, doing ‘-rcoverage’ from the command line didn’t work, but if I  
put  
“require ‘coverage’” into my app, that worked.

But I didn’t WANT my app to require coverage, since then I’d get  
coverage  
all the time (or at least on every test, if I put it into test\_helper.rb  
or  
someplace like that.

To get around this conundrum, I made a file called  
test/enable\_coverage.rb,  
which contains:

require ‘rubygems’  
require ‘coverage’

Then, from the command line, I can do something like:

ruby -rtest/enable\_coverage.rb test/unit/mytest.rb

I don’t actually run it this way. Instead, I made a Rake task to do  
coverage, and I had the Rake task include that file, like this:

desc “Run the unit, functional, and agent tests and generate coverage  
stats. You must have the coverage gem installed for this to work.”  
Rake::TestTask.new(:test\_coverage =\> [:prepare\_test\_database]) do |t|  
t.libs \<\< “test”  
fl = FileList.new.include(‘test/enable\_coverage.rb’,  
‘test/unit/ **/\*\_test.rb’, 'test/functional/** /_\_test.rb’,  
'test/agent/\*\*/_\_test.rb’)  
t.test\_files = fl  
t.verbose = true  
end

---

<div class="post-metadata">

**Author:** ![szczepiq](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/fbc32d/32.png) [@szczepiq](https://www.ruby-forum.com/u/szczepiq)\
**Post date:** [December 29, 2005, 7:51pm UTC](https://www.ruby-forum.com/t/coverage-does-not-work/51265/4 "2005-12-29T19:51:19Z")

</div>

Insurance looks cool but I will wait for the next release of win32 ruby  
since Insurance requires 1.8.3

What about coverage?

Michael,  
One enhancement I would make to your idea:

My custom tasks file (placed in lib/tasks) looks:  
require ‘coverage’ if ENV[‘coverage’]==‘true’

from command line I invoke standard rake builds (test\_units, recent,  
etc.)  
but if I want coverage I insert ‘coverage=true’, eg:

rake test\_units coverage=true

Now I have almost the same development evironment as I would use  
-rcoverage.  
The problem is that in every new project I have to place my custom rake  
file  
(I don’t want to make dependencies in RUBY\_HOME/lib/… /rails to  
coverage).

I hate leaving some blank areas on the map behind. I think that gems in  
general do not work with ruby command line -r option (I tested it with  
some). Is it the problem with win2k I am using for development? Gems  
usage  
manual does not shed light on the matter (it states only about require  
‘xxx’  
in the script. On the other hand in Pragmatic book about Rails the  
author is  
using coverage from command line -r option…

Question: Can I use gem library just by including -rxxx in ruby command  
line  
(I have rubygems in RUBYOPT)?

Thanks  
Szczepan

2005/12/29, Michael S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

---

<div class="post-metadata">

**Author:** ![szczepiq](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/fbc32d/32.png) [@szczepiq](https://www.ruby-forum.com/u/szczepiq)\
**Post date:** [January 8, 2006, 2:53am UTC](https://www.ruby-forum.com/t/coverage-does-not-work/51265/5 "2006-01-08T02:53:32Z")

</div>

On 12/29/05, Szczepan F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Michael,  
> One enhancement I would make to your idea:
> 
> My custom tasks file (placed in lib/tasks) looks:  
> require ‘coverage’ if ENV[‘coverage’]==‘true’
> 
> from command line I invoke standard rake builds (test\_units, recent, etc.)  
> but if I want coverage I insert ‘coverage=true’, eg:
> 
> rake test\_units coverage=true

I made a file lib/tasks/default.rake with only “require ‘coverage’ if  
ENV[‘coverage’]==‘true’”, but when I run “rake coverage=true” only  
index.html, environment.rb.html, routes.rb.html, and boot.rb.html are  
generated. The quality of those results isn’t very good anyway. It  
counts continuation lines as not being covered and stopped midway  
through my environment.rb for some reason.

Is it working better than this on your system?

> Thanks  
> Szczepan

Sincerely,

Tom L.  
[http://AllTom.com/](http://AllTom.com/)  
[http://GadgetLife.org/](http://GadgetLife.org/)

---

<div class="post-metadata">

**Author:** ![szczepiq](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/fbc32d/32.png) [@szczepiq](https://www.ruby-forum.com/u/szczepiq)\
**Post date:** [January 8, 2006, 4:23am UTC](https://www.ruby-forum.com/t/coverage-does-not-work/51265/6 "2006-01-08T04:23:45Z")

</div>

Hi

Move following line:  
require ‘coverage’ if ENV[“coverage”] == ‘true’

to top of your test\_helper.rb

🙂

2006/1/8, Tom L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):
