Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 209
Bruce Avatar asked Oct 31 '11 18:10

Bruce


2 Answers

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

like image 190
jacobsimeon Avatar answered Oct 12 '22 23:10

jacobsimeon


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

like image 31
murphyslaw Avatar answered Oct 12 '22 21:10

murphyslaw