Agile Web Development

Build it. Launch it. Love it.

Rails rcov

rails_rcov provides easy-to-use Rake tasks to determine the code coverage of your unit, functional, and integration tests using Mauricio Fernandez’s rcov tool.


First, install rcov from Mauricio’s web site []. Make sure it’s on your system path, so that typing rcov on the command line actually runs it. THIS PLUGIN DOESN’T DO ANYTHING BESIDES GENERATE ERRORS UNLESS YOU INSTALL RCOV FIRST. RCOV CONTAINS ALL THE MAGIC, THIS PLUGIN JUST RUNS IT.

Second, install this plugin. If your project is source-controlled by Subversion (which it should be, really), the easiest way to install this is via Rails’ plugin script:

  ./script/plugin install -x

If you’re not using Subversion, or if you don’t want it adding svn:externals in your project, remove the -x switch:

  ./script/plugin install


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 a few options:

  rake test:units:rcov SORT=(name|loc|coverage)
  rake test:units:rcov SORT_REVERSE=(YES|Y|TRUE|T|1)
  rake test:units:rcov THRESHOLD=(INT)
  rake test:units:rcov NO_COLOR=(YES|Y|TRUE|T|1)
  rake test:units:rcov PROFILE=(YES|Y|TRUE|T|1)
  rake test:units:rcov SHOW_WARNINGS=(YES|Y|TRUE|T|1)

These options parallel the rcov options, so check the rcov documentation if this isn’t clear.

Running rake test:rcov will run the unit tests, functional tests, and integration tests sequentially via rcov, and output the results to your_rails_app/coverage/(units|functionals|integration).





Written by Coda Hale .


License Rails' (MIT)
Rating (35 votes)
Owner Coda Hale
Created 27 May 2006


  • Avatar
    15 February 2007

    rake test:units:rcov, test:functionals:rcov, and test:integration:rcov all run fine for me, but rake test:rcov seems to be broken:

    rake aborted! Don't know how to build task 'test:rcov'

    I'm using Rails 1.2 with rcov 0.7.0 on Ubuntu Edgy, which has Ruby 1.8.4.

  • Avatar
    21 February 2007

    rake -T rcov reports:

    rake test:functionals:clobber_rcov # Remove Rcov reports for functional tests rake test:functionals:rcov # Run all functional tests with Rcov to measure coverage rake test:integration:clobber_rcov # Remove Rcov reports for integration tests rake test:integration:rcov # Run all integration tests with Rcov to measure coverage rake test:plugins:clobber_rcov # Remove Rcov reports for plugin tests rake test:plugins:rcov # Run all plugin tests with Rcov to measure coverage rake test:recent:clobber_rcov # Remove Rcov reports for recent tests rake test:recent:rcov # Run all recent tests with Rcov to measure coverage rake test:test:clobber_rcov # Remove Rcov reports for test tests rake test:test:rcov # Run all test tests with Rcov to measure coverage rake test:uncommitted:clobber_rcov # Remove Rcov reports for uncommitted tests rake test:uncommitted:rcov # Run all uncommitted tests with Rcov to measure coverage rake test:units:clobber_rcov # Remove Rcov reports for unit tests rake test:units:rcov # Run all unit tests with Rcov to measure coverage

    rake test:test:rcov seems to work fine

  • anonymous
    28 February 2007

    The options seem to do nothing for me. rake test:units:rcov produces the same as rake test:units:rcove SORT=coverage

  • anonymous
    28 February 2007

    The codahale blog shows the correct way to pass command lines to rcov:

    For example, to sort by coverage, and not use color output:

    > rake test:units:rcov RCOV_PARAMS="--sort=coverage --no-color"

    Be sure to quote all the rcov options together. For a list of options, use

    > rcov --help

  • Avatar
    Tadd Giles
    5 May 2007

    I'm seeing the same problem as core. test:rcov isn't showing as a task in rake --tests test:units:rcov is there as well as the others.

  • Avatar
    11 May 2007

    I've the same problem as core. Windows XP, ruby 1.8.6, rails 1.2.3

  • Avatar
    27 May 2007

    I have a patch to rails_rcov which adds a working test:rcov task. It's posted on my blog:

    Hope this helps!

  • Avatar
    21 July 2009

    I am not able to get how rcov calculates the test coverage %. I have tested all the action except the protected and still test coverage is 64%. Need some revelation.... Thanks

Add a comment