DZone 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

Snippets has posted 5883 posts at DZone. View Full User Profile

Rake Task To Run A Single Rails Unit Or Functional Test

07.28.2006
| 12654 views |
  • submit to reddit
        // 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

Snippets Manager replied on Wed, 2008/03/19 - 5:38pm

now supports test:x -> first file matching x*_test.rb (searched in order unit/functional/integration/any folder) (same for spec)

Snippets Manager replied on Wed, 2008/03/19 - 5:38pm

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