0

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

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

编辑:我实际上想rake test:foo与 有点不同rake test:units,因为我希望它在我简单地运行时运行rake test

4

1 回答 1

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
于 2013-01-08T17:22:32.823 回答