Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Rake task to run a single rails unit or functional test (See related posts)

// stolen from here: http://nubyonrails.com/articles/2006/07/28/foscon-and-living-dangerously-with-rake

##
# Run a single test in Rails.
#
#   rake blogs_list
#   => Runs test_list for BlogsController (functional test)
#
#   rake blog_create
#   => Runs test_create for BlogTest (unit test)

rule "" do |t|
  if /(.*)_([^.]+)$/.match(t.name)
    file_name = $1
    test_name = $2
    if File.exist?("test/unit/#{file_name}_test.rb")
      file_name = "unit/#{file_name}_test.rb" 
    elsif File.exist?("test/functional/#{file_name}_controller_test.rb")
      file_name = "functional/#{file_name}_controller_test.rb" 
    else
      raise "No file found for #{file_name}" 
    end
    sh "ruby -Ilib:test test/#{file_name} -n /^test_#{test_name}/" 
  end
end

Comments on this post

grosser posts on Mar 19, 2008 at 17:39
http://pragmatig.wordpress.com/2008/03/19/testing-a-single-example-spec-testcase-test/

- same syntax test:blog:create
- searches for matching files in unit/functional/integration
- works with rspec too rake spec:blog:create
grosser posts on Apr 05, 2008 at 12:44
now supports test:x -> first file matching x*_test.rb (searched in order unit/functional/integration/any folder) (same for spec)

You need to create an account or log in to post comments to this site.


Click here to browse all 4860 code snippets

Related Posts