Search code examples
ruby-on-rails-3testingrake-task

How do I write Rails3 rake task to run a single unit or functional test


Is it possible to write a rake task that will run a single specified Unit test or a single specified Functional test?


Solution

  • You can run a particular test method from the test case by using the -n switch with the test method name.

    $ ruby -Itest test/unit/post_test.rb -n test_the_truth
    
    Loaded suite unit/post_test
    Started
    .
    Finished in 0.023513 seconds.
    
    1 tests, 1 assertions, 0 failures, 0 errors
    

    http://guides.rubyonrails.org/testing.html

    It means your rake task will look like

    task :tester => :environment do
      `cd #{Rails.root} && ruby -Itest test/unit/post_test.rb -n test_the_truth`
    end