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:
1task :default_rcov_params_for_units do
2 RCOV_PARAMS = ENV['RCOV_PARAMS'] = "--sort=coverage"
3 SHOW_ONLY = ENV['SHOW_ONLY'] = "app/models|lib|app/concerns|app/helpers"
4end
5
6task :default_rcov_params_for_functionals do
7 RCOV_PARAMS = ENV['RCOV_PARAMS'] = "--sort=coverage"
8 SHOW_ONLY = ENV['SHOW_ONLY'] = "app/controllers"
9end
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.
1namespace :test do
2 namespace :units do
3 desc 'With RCOV_PARAMS="--sort=coverage" SHOW_ONLY="app/models|lib|app/concerns|app/helpers"'
4 task :rcov => [:default_rcov_params_for_units]
5 end
6
7 desc 'With RCOV_PARAMS="--sort=coverage" SHOW_ONLY="app/controllers"'
8 namespace :functionals do
9 task :rcov => [:default_rcov_params_for_functionals]
10 end
11end
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