ShiftEleven

Customize Your Rake Files

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.

Comments

comments powered by Disqus