edge badge

Handles all logic behind +rails test+ command.

Namespace
Methods
N
P
R
S
Class Public methods
new(files, options)

Creates a new TestRunner object with a list of test file paths.

# File railties/lib/rails/commands/test_runner.rb, line 99
def initialize(files, options)
  @files = files

  Rails.application.load_tasks
  Rake::Task['db:test:load'].invoke

  if options.delete(:fixtures)
    if defined?(ActiveRecord::Base)
      ActiveSupport::TestCase.send :include, ActiveRecord::TestFixtures
      ActiveSupport::TestCase.fixture_path = "#{Rails.root}/test/fixtures/"
      ActiveSupport::TestCase.fixtures :all
    end
  end

  MiniTest::Unit.runner.options = options
  MiniTest::Unit.output = SilentUntilSyncStream.new(MiniTest::Unit.output)
end
parse_arguments(arguments)

Parses arguments and sets them as option flags

# File railties/lib/rails/commands/test_runner.rb, line 40
def parse_arguments(arguments)
  options = {}
  orig_arguments = arguments.dup

  OptionParser.new do |opts|
    opts.banner = "Usage: rails test [path to test file(s) or test suite]"

    opts.separator ""
    opts.separator "Run a specific test file(s) or a test suite, under Rails'"
    opts.separator "environment. If the file name(s) or suit name is omitted,"
    opts.separator "Rails will run all tests."
    opts.separator ""
    opts.separator "Specific options:"

    opts.on '-h', '--help', 'Display this help.' do
      puts opts
      exit
    end

    opts.on '-e', '--environment NAME', String, 'Specifies the environment to run this test under' do |e|
      options[:environment] = e
    end

    opts.on '-f', '--fixtures', 'Load fixtures in test/fixtures/ before running the tests' do
      options[:fixtures] = true
    end

    opts.on '-s', '--seed SEED', Integer, "Sets random seed" do |m|
      options[:seed] = m.to_i
    end

    opts.on '-v', '--verbose', "Verbose. Show progress processing files." do
      options[:verbose] = true
    end

    opts.on '-n', '--name PATTERN', "Filter test names on pattern (e.g. /foo/)" do |n|
      options[:filter] = n
    end

    opts.separator ""
    opts.separator "Support types of test suites:"
    opts.separator "-------------------------------------------------------------"
    opts.separator "* models (test/models/**/*)"
    opts.separator "* helpers (test/helpers/**/*)"
    opts.separator "* units (test/{models,helpers,unit}/**/*"
    opts.separator "* controllers (test/controllers/**/*)"
    opts.separator "* mailers (test/mailers/**/*)"
    opts.separator "* functionals (test/{controllers,mailers,functional}/**/*)"
    opts.separator "* integration (test/integration/**/*)"
    opts.separator "-------------------------------------------------------------"

    opts.parse! arguments
    orig_arguments -= arguments
  end
  options
end
start(files, options = {})

Creates a new TestRunner object with an array of test files to run based on the arguments. When no arguments are provided, it runs all test files. When a suite argument is provided, it runs only the test files in that suite. Otherwise, it runs the specified test file(s).

# File railties/lib/rails/commands/test_runner.rb, line 12
def start(files, options = {})
  original_fixtures_options = options.delete(:fixtures)
  options[:fixtures] = true

  case files.first
  when nil
    new(Dir['test/**/*_test.rb'], options).run
  when 'models'
    new(Dir['test/models/**/*_test.rb'], options).run
  when 'helpers'
    new(Dir['test/helpers/**/*_test.rb'], options).run
  when 'units'
    new(Dir['test/{models,helpers,unit}/**/*_test.rb'], options).run
  when 'controllers'
    new(Dir['test/controllers/**/*_test.rb'], options).run
  when 'mailers'
    new(Dir['test/mailers/**/*_test.rb'], options).run
  when 'functionals'
    new(Dir['test/{controllers,mailers,functional}/**/*_test.rb'], options).run
  when 'integration'
    new(Dir['test/integration/**/*_test.rb'], options).run
  else
    options[:fixtures] = original_fixtures_options
    new(files, options).run
  end
end
Instance Public methods
run()

Runs test files by evaluating each of them.

# File railties/lib/rails/commands/test_runner.rb, line 118
def run
  @files.each { |filename| load(filename) }
end