创建我自己的rake测试:foo任务

时间:2013-01-08 17:09:34

标签: ruby-on-rails rake

我有一堆不是单元测试或功能测试的测试,它们的格式为test/foo/special_test.rb

我想创建一个像rake test:units这样的rake任务,它将运行foo文件夹中的所有测试。我该怎么做?

修改:我实际上希望rake test:foorake test:units略有不同,因为我希望它能够运行当我只做rake test时。

1 个答案:

答案 0 :(得分:1)

我不记得这是从哪里来的,所以不幸的是我无法给予正确的认可,但这应该有效。我说“应该”,因为我已经停止使用它,但是从我的git历史中抓住了它。

# 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

您可以删除默认的“测试”任务并重新定义,以便在您运行rake test时它也会自动运行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