Say you have a bunch of Rails tests. Good boy!

Sometimes, in Rails (unfortunately), the results of the previous test affect the results of the next test. For example, if a previous test loads in some data into a database table, and the current test reads in that data without clearing/reloading the data (which happens if the current test neglects to include a fixtures :stuff line, then you’re going to have inconsistent test results.

The fix (put into lib/tasks/test.rake):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
require 'test/rails/rake_tasks'

desc "Runs all the tests one at a time"
namespace :test do
  task :separately do |t|
    # Get all the test files
    files = Dir["#{RAILS_ROOT}/test/*/*.rb"] 
    files.each do |file|
      puts "Running #{ File.basename(file) }"
      `rake db:test:clone_structure`
      output = `ruby #{ file }`  
      if not $?.success?
        puts output
        exit 1
      end
    end
  end
end

Leave a Reply