1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Add support for MiniTest flags in TestRunner

Any flags that got set will be passed through to MiniTest::Unit.runner,
such as `-n`, `-s-, and `-v`.
This commit is contained in:
Prem Sichanugrist 2013-02-01 17:21:09 -05:00
parent 9f75f7735a
commit 176b57c543
4 changed files with 93 additions and 59 deletions

View file

@ -38,7 +38,6 @@ Rails creates a `test` folder for you as soon as you create a Rails project usin
```bash
$ ls -F test
controllers/ helpers/ mailers/ test_helper.rb
fixtures/ integration/ models/
```
@ -235,13 +234,10 @@ Finished tests in 0.009262s, 107.9680 tests/s, 107.9680 assertions/s.
1 tests, 1 assertions, 0 failures, 0 errors, 0 skips
```
You can also run a particular test method from the test case by running the test using Ruby and use the `-n` switch with the `test method name`.
You can also run a particular test method from the test case by running the test and use the `-n` switch with the `test method name`.
```bash
Run options: -n test_the_truth --seed 23064
# Running tests:
$ rails test test/models/post_test.rb -n test_the_truth
.
Finished tests in 0.009064s, 110.3266 tests/s, 110.3266 assertions/s.
@ -265,10 +261,7 @@ end
Let us run this newly added test.
```bash
Run options: -n test_should_not_save_post_without_title --seed 38984
# Running tests:
$ rails test test/models/post_test.rb -n test_should_not_save_post_without_title
F
Finished tests in 0.044632s, 22.4054 tests/s, 22.4054 assertions/s.
@ -308,11 +301,7 @@ end
Now the test should pass. Let us verify by running the test again:
```bash
$ ruby -Itest test/models/post_test.rb -n test_should_not_save_post_without_title
Run options: -n test_should_not_save_post_without_title --seed 62114
# Running tests:
$ rails test test/models/post_test.rb -n test_should_not_save_post_without_title
.
Finished tests in 0.047721s, 20.9551 tests/s, 20.9551 assertions/s.
@ -337,11 +326,7 @@ end
Now you can see even more output in the console from running the tests:
```bash
$ ruby -Itest test/models/post_test.rb -n test_should_report_error
Run options: -n test_should_report_error --seed 22995
# Running tests:
$ rails test test/models/post_test.rb -n test_should_report_error
E
Finished tests in 0.030974s, 32.2851 tests/s, 0.0000 assertions/s.

View file

@ -83,15 +83,12 @@ when 'server'
when 'test'
$LOAD_PATH.unshift("./test")
require 'rails/commands/test_runner'
if ["-h", "--help"].include?(ARGV.first)
Rails::TestRunner.help_message
exit
else
require APP_PATH
Rails.application.require_environment!
Rails.application.load_tasks
Rails::TestRunner.start(ARGV)
end
options = Rails::TestRunner.parse_arguments(ARGV)
require APP_PATH
Rails.application.require_environment!
Rails.application.load_tasks
Rails::TestRunner.start(ARGV, options)
when 'dbconsole'
require 'rails/commands/dbconsole'

View file

@ -9,54 +9,85 @@ module Rails
# of file to a new +TestRunner+ object, then invoke the evaluation. If
# the argument is not a test suite name, it will be treated as a file
# name and passed to the +TestRunner+ instance right away.
def start(arguments)
case arguments.first
def start(files, options)
case files.first
when nil
new(Dir['test/**/*_test.rb']).run
new(Dir['test/**/*_test.rb'], options).run
when 'models'
new(Dir['test/models/**/*_test.rb']).run
new(Dir['test/models/**/*_test.rb'], options).run
when 'helpers'
new(Dir['test/helpers/**/*_test.rb']).run
new(Dir['test/helpers/**/*_test.rb'], options).run
when 'units'
new(Dir['test/{models,helpers,unit}/**/*_test.rb']).run
new(Dir['test/{models,helpers,unit}/**/*_test.rb'], options).run
when 'controllers'
new(Dir['test/controllers/**/*_test.rb']).run
new(Dir['test/controllers/**/*_test.rb'], options).run
when 'mailers'
new(Dir['test/mailers/**/*_test.rb']).run
new(Dir['test/mailers/**/*_test.rb'], options).run
when 'functionals'
new(Dir['test/{controllers,mailers,functional}/**/*_test.rb']).run
new(Dir['test/{controllers,mailers,functional}/**/*_test.rb'], options).run
when 'integration'
new(Dir['test/integration/**/*_test.rb']).run
new(Dir['test/integration/**/*_test.rb'], options).run
else
new(arguments).run
new(files, options).run
end
end
# Print out the help message which listed all of the test suite names.
def help_message
puts "Usage: rails test [path to test file(s) or test suite type]"
puts ""
puts "Run single test file, or a test suite, under Rails'"
puts "environment. If the file name(s) or suit name is omitted,"
puts "Rails will run all the test suites."
puts ""
puts "Support types of test suites:"
puts "-------------------------------------------------------------"
puts "* models (test/models/**/*)"
puts "* helpers (test/helpers/**/*)"
puts "* units (test/{models,helpers,unit}/**/*"
puts "* controllers (test/controllers/**/*)"
puts "* mailers (test/mailers/**/*)"
puts "* functionals (test/{controllers,mailers,functional}/**/*)"
puts "* integration (test/integration/**/*)"
puts "-------------------------------------------------------------"
# Parse arguments and set them as option flags
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 type]"
opts.separator ""
opts.separator "Run single test file, 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 the test suites."
opts.separator ""
opts.separator "Specific options:"
opts.on '-h', '--help', 'Display this help.' do
puts opts
exit
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 |a|
options[:filter] = a
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
end
# Create a new +TestRunner+ object with a list of test file paths.
def initialize(files)
def initialize(files, options)
@files = files
Rake::Task['test:prepare'].invoke
MiniTest::Unit.runner.options = options
MiniTest::Unit.output = SilentUntilSyncStream.new(MiniTest::Unit.output)
end

View file

@ -149,6 +149,27 @@ module ApplicationTests
end
end
def test_run_named_test
app_file 'test/unit/chu_2_koi_test.rb', <<-RUBY
require 'test_helper'
class Chu2KoiTest < ActiveSupport::TestCase
def test_rikka
puts 'Rikka'
end
def test_sanae
puts 'Sanae'
end
end
RUBY
run_test_command('test/unit/chu_2_koi_test.rb -n test_rikka').tap do |output|
assert_match /Rikka/, output
assert_no_match /Sanae/, output
end
end
private
def run_test_command(arguments = 'test/unit/test_test.rb')
Dir.chdir(app_path) { `bundle exec rails test #{arguments}` }