How do I get code coverage stats for my Sinatra app?

2.2k views Asked by At

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.

2

There are 2 answers

1
murphyslaw On

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

4
jacobsimeon On

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