6

I have written a Sinatra app (web site), and I would like to collect code coverage information for the site's code. I'm new to Ruby, but Google tells me that rcov is a good code coverage tool. Unfortunately, all the information I can find online shows only how to get code coverage information about test cases - I want code coverage information about my site itself.

The particular site files I want to profile are in the "sdk" and "sdk/vendor" directories, so where I would normally run my site with "ruby site.rb" I instead tried the following:

rcov -Isdk -Isdk/vendor site.rb

It showed the Sinatra start-up text, but then immediately exited instead of waiting for web requests like my Sinatra app normally would.

Can someone tell me the trick of running my site with code coverage enabled? I want to run the site, hit it with a series of requests, and then stop the site; after which I want to look at the accumulated code coverage stats from the whole series of requests.

I'm currently using Ruby 1.8.7.

Bruce
  • 8,202
  • 6
  • 37
  • 49
  • could you please elaborate a little bit more as to what you understand to be 'codecoverage for an app'? because traditionally it means the coverage of test cases – robustus Nov 03 '11 at 10:59
  • I have a web site implemented using Sinatra. I would like to exercise the site, and measure how much of the site's code gets run during that exercise. I am not using any test framework to automate the exercise of the site. – Bruce Nov 03 '11 at 18:38
  • It seems like this should be simple, but I'm just missing some key insight. – Bruce Nov 03 '11 at 18:44

2 Answers2

4

SimpleCov is perfect for this. If you're using RSpec and Bundler setup is super easy

in your gem file

gem 'simplecov'

then

$ bundle install

In spec/spec_helper.rb (before anything else)

require 'simplecov'
SimpleCov.start

then: $ rspec spec

Simplecov generates a really nice coverage report at coverage/index.html

jacobsimeon
  • 2,012
  • 1
  • 18
  • 20
  • I'm not using rspec - as I point out in my question, I want code coverage on my site files, not on tests. – Bruce Nov 02 '11 at 01:14
  • Fair enough. I guess I misunderstood the question. Are you saying you want to test which lines of code are being executed given a certain request or set of requests? I'd suggest simply writing some unit or integration tests. Under what circumstances would it be useful to have a coverage report other than for your test suite? – jacobsimeon Nov 02 '11 at 05:53
  • That is correct. I am testing some server-side code that runs in a fairly complicated environment, with interactions between the browser, the Sinatra server, and other remote web services. It is quite likely that I could have used one of the Ruby test harnesses and mocked out all the bits of the configuration not directly being tested, but I'm new to Ruby so I found it simpler and safer to simply implement the complex system and then exercise it from the browser. Having done that, now I'm curious how much of the Ruby code I am testing is actually being hit by my tests. – Bruce Nov 02 '11 at 19:17
  • To be clear, some of the Ruby code running in Sinatra is my testing code, and some of it is the library I am testing. – Bruce Nov 02 '11 at 19:19
1

Maybe you could take a look at SimpleCov, which advertises its simple usage for any kind of coverage analysis.

murphyslaw
  • 640
  • 5
  • 12
  • It looks nice, but I'm currently using Ruby 1.8.7, and it looks like SimpleCov only supports 1.9+. I'll update my problem description above with this info. – Bruce Oct 31 '11 at 18:54