Been paying attention to Mauricio Fernandez’s rcov, but have no idea how to easily integrate it into your Rails application? Well, you had no idea how to do that. In about 15 seconds, you’ll be all over the code-coverage bandwagon. Ready? GO!
First, Install Rcov
I shouldn’t need to say this. Go here, download, untar, and
ruby setup.rb && sudo ruby setup.rb install. Go! Go! Go!
Second, Install rails_rcov
./script/plugin install -x http://svn.codahale.com/rails_rcov
Still partying like it’s 1995?
./script/plugin install http://svn.codahale.com/rails_rcov
Third, Bask In The Joy Of Rake Magic
For each test:blah task you have for your Rails project, rails_rcov adds two more: test:blah:rcov and test:blah:clobber_rcov.
Running rake test:units:rcov, for example, will run your unit tests through rcov and write the code coverage reports to your_rails_app/coverage/units. Running test:units:clobber_rcov will erase the generated report for the unit tests.
Each rcov task can take two optional arguments:
RCOV_PARAMS, whose argument is passed along to rcov, and
SHOW_ONLY, which limits the files displayed in the report. Here are some examples:
# sort by coverage rake test:units:rcov RCOV_PARAMS="--sort=coverage" # show callsites and hide fully covered files rake test:units:rcov RCOV_PARAMS="--callsites --only-uncovered" # only show files which begin with "app/models" rake test:units:rcov SHOW_ONLY="app/models"
Check the rcov documentation for more details.
Please note that rails_rcov has only been tested with a Bash shell, and any other environment could well explode in your face.
Fourth, Marvel At How You Ever Survived Without Mauricio’s Crazy Metaprogramming Robot Watching Your Back
Is that cool or what? All that red in the report is work you haven’t done yet, so get crackin’! Don’t stop shooting until everything’s green! Hup hup hup!