Rcov is a handy tool to make sure that your tests have at least run every line of code in your application. This is very useful if you have forgotten to write a test for a method, or if inside of a method, you forgot to test a conditional statement. Because I find this handy, I incorporate the rcov plugin in my rails applications.
Running rake test:functionals:rcov, my report shows me a long list of items, some of which I don’t want to see. I don’t want to see the coverage of my models in the report. I should get that report by running rake test:units:rcov. Luckily, the plugin allows for me to set arguments like SHOW_ONLY="app/models". But get this, I’m lazy and I don’t want to type that argument let alone remember it every time I want to run the rcov tests. What to do?
Rake It Up
The options are simply environment variables. By using rake, we can create some tasks that set these environment variables. So here’s what mine looks like:
task :default_rcov_params_for_units do RCOV_PARAMS = ENV['RCOV_PARAMS'] = "--sort=coverage" SHOW_ONLY = ENV['SHOW_ONLY'] = "app/models|lib|app/concerns|app/helpers" end task :default_rcov_params_for_functionals do RCOV_PARAMS = ENV['RCOV_PARAMS'] = "--sort=coverage" SHOW_ONLY = ENV['SHOW_ONLY'] = "app/controllers" end
That means now I can do something like rake default_rcov_params_for_units test:units:rcov. Good, but there is better. We can have default_rcov_params_for_units become a prerequisite for test:units:rcov. That will make it happen automagically.
namespace :test do
namespace :units do
desc 'With RCOV_PARAMS="--sort=coverage" SHOW_ONLY="app/models|lib|app/concerns|app/helpers"'
task :rcov => [:default_rcov_params_for_units]
end
desc 'With RCOV_PARAMS="--sort=coverage" SHOW_ONLY="app/controllers"'
namespace :functionals do
task :rcov => [:default_rcov_params_for_functionals]
end
end
Follow Up
Tada! That was easy. This is one example of customizing your rake profile. Don’t be afraid to try other methods as well.
March 30th, 2007 at 1:16 pm #Jason Perry
Cool stuff. Here’s the one I use.
require 'rake/clean' RBINDIR = Config::CONFIG['bindir'] RBIN = "#{RBINDIR}/rcov" if File.exists?(RBIN) # output goes to subdirectories of this one RCOV_OUT = "coverage" # "rake clobber" to remove output directory (along with other generated stuff) CLOBBER.include(RCOV_OUT) # don't report coverage on these files RCOV_EXCLUDE = %w(boot.rb environment.rb routes.rb vendor test/functional test/unit).join(',') # RCOV command, run as though from the commandline. Amend as required or perhaps move to config/environment.rb? RCOV = "#{RBINDIR}/ruby #{RBIN} --exclude #{RCOV_EXCLUDE} --output #{RCOV_OUT}" namespace :test do desc "generate a coverage report for unit and functional tests together in #{RCOV_OUT}/index.html" task :coverage do rm_rf RCOV_OUT sh "#{RCOV} test/unit/*.rb test/functional/*.rb" end namespace :coverage do desc "generate a unit test coverage report in #{RCOV_OUT}/index.html" task :unit do sh "#{RCOV} test/unit/*.rb" end desc "generate a functional test coverage report in #{RCOV_OUT}/index.html" task :functional do sh "#{RCOV} test/functional/*.rb" end end end end