rails engines testing with rcov

Ever wanted to do coverage tests on an engine? Well, it really hurts that using the default test runner like “rake test:engines:my_engine:rcov” (that is added by the rcov helper plugin for rails) will run the tests, but ignore everything below the “vendor” folder. Unfortunately this includes the engine files as well. And at the time I tried it last, this exclusion was hard coded into rcov.

Figured out how to do it anyway – add this to your engine’s tasks (using gacl_engine from my engine…)


require 'rake/clean'

# output directory - removed with "rake clobber"
CLOBBER.include("coverage")

# RCOV command, run as though from the commandline.  Amend as required or perhaps move to config/environment.rb?
RCOV = "rcov"

desc "generate a coverage report in coverage/gacl_engine"
namespace :gacl_engine do
  task :test_coverage do
    sh "cd vendor/plugins/gacl_engine;#{RCOV} --rails -T -Ilib:test --output ../../../coverage/gacl_engine test/all_tests.rb"
  end
end

Then create test runners in your engine test directory.

all_tests.rb:

# Combine unit and functional tests in one run
%w( unit_tests functional_tests ).each do |f|
  require File.dirname(__FILE__) + "/#{f}.rb"
end

unit_tests.rb:

# Run all unit tests
Dir[File.dirname(__FILE__) + "/unit/**/*_test.rb"].each do |f|
  require f
end

functional_tests.rb and so on are accordingly created. Use the rake task and you’ve got coverage tests on engines…

Explore posts in the same categories: Ruby/Rails

Comment: