Run specific tests via Rake

Journal entry
July 28, 2006

Geoffrey Grosenbach recently discoved what’s effectively method_missing for rake tasks. I’ve taken his experimental proposed rake task, and changed (I’m reluctant to say “improved”) it slighty.

This modified version uses a different syntax (rake test:foo:bar instead rake foo_bar) and regex matching for test names.

Usage

$ rake test:blog
=> Runs the full BlogTest unit test if it exists. If not, looks for BlogControllerTest and runs that.
$ rake test:blog:create
=> Runs the tests matching /create/ in the BlogTest unit test or BlogController functional test as described above. 
$ rake test:blog_controller
=> Runs all tests in the BlogControllerTest functional test
$ rake test:blog_controller:create
=> Runs the tests matching /create/ in the BlogControllerTest functional test	

Code

# Run specific tests or test files
# 
# rake test:blog
# => Runs the full BlogTest unit test
# 
# rake test:blog:create
# => Runs the tests matching /create/ in the BlogTest unit test
# 
# rake test:blog_controller
# => Runs all tests in the BlogControllerTest functional test
# 
# rake test:blog_controller
# => Runs the tests matching /create/ in the BlogControllerTest functional test	
rule "" do |t|
  # test:file:method
  if /test:(.*)(:([^.]+))?$/.match(t.name)
    arguments = t.name.split(":")[1..-1]
    file_name = arguments.first
    test_name = arguments[1..-1] 
    
    if File.exist?("test/unit/#{file_name}_test.rb")
      run_file_name = "unit/#{file_name}_test.rb" 
    elsif File.exist?("test/functional/#{file_name}_controller_test.rb")
      run_file_name = "functional/#{file_name}_controller_test.rb" 
    elsif File.exist?("test/functional/#{file_name}_test.rb")
      run_file_name = "functional/#{file_name}_test.rb" 
    end
    
    sh "ruby -Ilib:test test/#{run_file_name} -n /#{test_name}/" 
  end
end

Do whatever you want with the above code, it’s yours now.

Categories
Selling out
Did you know?
Jakob is an independent web application developer who builds awesome stuff for the web. You can hire him to build awesome stuff for you.

Comments and Trackbacks

topfunky July 28, 2006

Nice! I think your syntax is better than mine, although having to type the whole word "controller" is a little verbose.

Since many people use similarly named models and controllers, there may be no way around it.

Jakob S July 28, 2006

Yeah, that's pretty much why I modified it; I have both a person_test.rb and a person_controller_test.rb.

The above could probably be improved, by trying to look for a controller if the unit test isn't found, though. So rake test:person would run test/units/person.rb if it exists, and if not, look for test/functionals/person_controller.rb and run that.

Jakob S July 28, 2006

So be it. The above code has been updated so rake test:foo now checks for FooTest first, and if that doesn't exist, looks for FooControllerTest.

aurelian August 1, 2006

What if I also use diffrent modules to group my controllers?
So the tests will be also grouped in (eg) admin/#{file_name}_controller_test.rb

Jakob S August 1, 2006

It should be fairly easy to rewrite the code to handle that case as well - if it even needs rewriting. Go for it, there's no magic going on, it's just Ruby.

Labrat August 17, 2006

This was very helpful and I used it as a foundation to add redgreen to all rake test tasks.

Commenting on this entry has been closed.