diff options
-rw-r--r-- | guides/source/testing.md | 28 | ||||
-rw-r--r-- | railties/CHANGELOG.md | 24 | ||||
-rw-r--r-- | railties/lib/rails/commands.rb | 9 | ||||
-rw-r--r-- | railties/lib/rails/commands/test_runner.rb | 146 | ||||
-rw-r--r-- | railties/test/application/test_runner_test.rb | 6 |
5 files changed, 19 insertions, 194 deletions
diff --git a/guides/source/testing.md b/guides/source/testing.md index 70061dc815..858d4d2334 100644 --- a/guides/source/testing.md +++ b/guides/source/testing.md @@ -222,10 +222,10 @@ TIP: You can see all these rake tasks and their descriptions by running `rake -- ### Running Tests -Running a test is as simple as invoking the file containing the test cases through `rails test` command. +Running a test is as simple as invoking the file containing the test cases through `rake test` command. ```bash -$ rails test test/models/post_test.rb +$ rake test test/models/post_test.rb . Finished tests in 0.009262s, 107.9680 tests/s, 107.9680 assertions/s. @@ -236,7 +236,7 @@ Finished tests in 0.009262s, 107.9680 tests/s, 107.9680 assertions/s. You can also run a particular test method from the test case by running the test and using `-n` switch with the `test method name`. ```bash -$ rails test test/models/post_test.rb -n test_the_truth +$ rake test test/models/post_test.rb TESTOPTS='-n test_the_truth' . Finished tests in 0.009064s, 110.3266 tests/s, 110.3266 assertions/s. @@ -260,7 +260,7 @@ end Let us run this newly added test. ```bash -$ rails test test/models/post_test.rb -n test_should_not_save_post_without_title +$ rake test test/models/post_test.rb TESTOPTS='-n test_should_not_save_post_without_title' F Finished tests in 0.044632s, 22.4054 tests/s, 22.4054 assertions/s. @@ -300,7 +300,7 @@ end Now the test should pass. Let us verify by running the test again: ```bash -$ rails test test/models/post_test.rb -n test_should_not_save_post_without_title +$ rake test test/models/post_test.rb TESTOPTS='-n test_should_not_save_post_without_title' . Finished tests in 0.047721s, 20.9551 tests/s, 20.9551 assertions/s. @@ -325,7 +325,7 @@ end Now you can see even more output in the console from running the tests: ```bash -$ rails test test/models/post_test.rb -n test_should_report_error +$ rake test test/models/post_test.rb TESTOPTS='-n test_should_report_error' E Finished tests in 0.030974s, 32.2851 tests/s, 0.0000 assertions/s. @@ -761,14 +761,14 @@ You don't need to set up and run your tests by hand on a test-by-test basis. Rai | Tasks | Description | | ------------------------ | ----------- | -| `rails test` | Runs all unit, functional and integration tests. You can also simply run `rails test` as Rails will run all the tests by default| -| `rails test controllers` | Runs all the controller tests from `test/controllers`| -| `rails test functionals` | Runs all the functional tests from `test/controllers`, `test/mailers`, and `test/functional`| -| `rails test helpers` | Runs all the helper tests from `test/helpers`| -| `rails test integration` | Runs all the integration tests from `test/integration`| -| `rails test mailers` | Runs all the mailer tests from `test/mailers`| -| `rails test models` | Runs all the model tests from `test/models`| -| `rails test units` | Runs all the unit tests from `test/models`, `test/helpers`, and `test/unit`| +| `rake test` | Runs all unit, functional and integration tests. You can also simply run `rake test` as Rails will run all the tests by default| +| `rake test:controllers` | Runs all the controller tests from `test/controllers`| +| `rake test:functionals` | Runs all the functional tests from `test/controllers`, `test/mailers`, and `test/functional`| +| `rake test:helpers` | Runs all the helper tests from `test/helpers`| +| `rake test:integration` | Runs all the integration tests from `test/integration`| +| `rake test:mailers` | Runs all the mailer tests from `test/mailers`| +| `rake test:models` | Runs all the model tests from `test/models`| +| `rake test:units` | Runs all the unit tests from `test/models`, `test/helpers`, and `test/unit`| There're also some test commands which you can initiate by running rake tasks: diff --git a/railties/CHANGELOG.md b/railties/CHANGELOG.md index ae13c3ccc9..e4a08f68c1 100644 --- a/railties/CHANGELOG.md +++ b/railties/CHANGELOG.md @@ -38,37 +38,23 @@ *Sam Ruby* -* Rails now generates a `test/test_helper.rb` file with `fixtures :all` commented out by default, - since we don't want to force loading all fixtures for user when a single test is run. However, - fixtures are still going to be loaded automatically for test suites. - - To force all fixtures to be create in your database, use `rails test -f` to run your test. - - *Prem Sichanugrist* - -* Add `rails test` command for running tests +* Improved `rake test` command for running tests To run all tests: - $ rails test + $ rake test To run a test suite - $ rails test [models,helpers,units,controllers,mailers,...] + $ rake test:[models,helpers,units,controllers,mailers,...] To run a selected test file(s): - $ rails test test/unit/foo_test.rb [test/unit/bar_test.rb ...] + $ rake test test/unit/foo_test.rb [test/unit/bar_test.rb ...] To run a single test from a test file - $ rails test test/unit/foo_test.rb -n test_the_truth - - For more information, see `rails test --help`. - - This command will eventually replace `rake test:*` and `rake test` tasks. - - *Prem Sichanugrist and Chris Toomey* + $ rake test test/unit/foo_test.rb TESTOPTS='-n test_the_truth' * Improve service pages with new layout (404, etc). diff --git a/railties/lib/rails/commands.rb b/railties/lib/rails/commands.rb index 41d3722c18..0d1286031c 100644 --- a/railties/lib/rails/commands.rb +++ b/railties/lib/rails/commands.rb @@ -80,15 +80,6 @@ when 'server' server.start end -when 'test' - $LOAD_PATH.unshift("./test") - require 'rails/commands/test_runner' - options = Rails::TestRunner.parse_arguments(ARGV) - ENV['RAILS_ENV'] ||= options[:environment] || 'test' - - require APP_PATH - Rails::TestRunner.start(ARGV, options) - when 'dbconsole' require 'rails/commands/dbconsole' Rails::DBConsole.start diff --git a/railties/lib/rails/commands/test_runner.rb b/railties/lib/rails/commands/test_runner.rb deleted file mode 100644 index d8857bd183..0000000000 --- a/railties/lib/rails/commands/test_runner.rb +++ /dev/null @@ -1,146 +0,0 @@ -require 'optparse' -require 'minitest/unit' - -module Rails - # Handles all logic behind +rails test+ command. - class TestRunner - class << self - # 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). - 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 - - # Parses arguments and sets 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]" - - 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 - end - - # Creates a new +TestRunner+ object with a list of test file paths. - 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 - - # Runs test files by evaluating each of them. - def run - @files.each { |filename| load(filename) } - end - - # A null stream object which ignores everything until +sync+ has been set - # to true. This is only used to silence unnecessary output from MiniTest, - # as MiniTest calls +output.sync = true+ right before it outputs the first - # test result. - class SilentUntilSyncStream < File - # Creates a +SilentUntilSyncStream+ object by giving it a target stream - # object that will be assigned to +MiniTest::Unit.output+ after +sync+ is - # set to true. - def initialize(target_stream) - @target_stream = target_stream - super(File::NULL, 'w') - end - - # Swaps +MiniTest::Unit.output+ to another stream when +sync+ is true. - def sync=(sync) - if sync - @target_stream.sync = true - MiniTest::Unit.output = @target_stream - end - - super - end - end - end -end diff --git a/railties/test/application/test_runner_test.rb b/railties/test/application/test_runner_test.rb index 405f6af295..180fc315d1 100644 --- a/railties/test/application/test_runner_test.rb +++ b/railties/test/application/test_runner_test.rb @@ -29,12 +29,6 @@ module ApplicationTests assert_match "Current Environment: test", run_test_command('test/unit/env_test.rb') end - def test_run_shortcut - create_test_file :models, 'foo' - output = Dir.chdir(app_path) { `bundle exec rails t test/models/foo_test.rb` } - assert_match "1 tests, 1 assertions, 0 failures", output - end - def test_run_single_file create_test_file :models, 'foo' create_test_file :models, 'bar' |