spike
spike

Reputation: 10004

Create my own rake test:foo task

I have a bunch of tests that aren't unit or functional tests, they're of the format test/foo/special_test.rb

I want to create a rake task like rake test:units that will run all the tests in the foo folder. How do I do this?

Edit: I'd actually like rake test:foo to be a little different from rake test:units, in that I do not want it to run when I do simply rake test.

Upvotes: 0

Views: 54

Answers (1)

MrDanA
MrDanA

Reputation: 11647

I don't remember where this is from, so unfortunately I can't give proper acknowledgement, but this should work. I say "should" because I've stopped using it, but grabbed it from my git history.

# First, 'reopen' the default :test namespace and create your custom task.
namespace :test do
  Rake::TestTask.new(:foo_tests => ["test:prepare", "other_dependent_rake_tasks"] ) do |t|
    DatabaseCleaner.strategy = :transaction # If using this.

    t.libs << "test"
    # Will also get subfolders within test/foo
    t.test_files = FileList['test/foo/**/*_test.rb', 'test/foo/*_test.rb']
  end
end

You can remove the default "test" task and redefine it so that when you run rake test it will automatically also run rake test:foo_tests.

remove_task "test"

desc 'Adding onto Rails regular tests'
task :test do
  # Add all the names of tests you want run here.
  errors = %w(test:units test:functionals test:integration test:foo_tests).collect do |task|
    begin
      puts "Running: #{task}"
      Rake::Task[task].invoke
      nil
    rescue => e
      task
    end
  end.compact
  abort "Errors running #{errors * ', '}!" if errors.any?
end

Upvotes: 1

Related Questions