From df85dfa6fa6f7ae9a0b72eb9b9a254d2d5560f38 Mon Sep 17 00:00:00 2001 From: Dalibor Nasevic Date: Fri, 8 Feb 2013 00:53:11 +0100 Subject: [PATCH] Improve wording for rails test command --- guides/source/testing.md | 7 ++-- railties/CHANGELOG.md | 23 +++++++------ railties/lib/rails/commands/test_runner.rb | 34 +++++++++---------- .../rails/app/templates/test/test_helper.rb | 4 +-- railties/test/application/test_runner_test.rb | 16 ++++----- 5 files changed, 43 insertions(+), 41 deletions(-) diff --git a/guides/source/testing.md b/guides/source/testing.md index 12f7260c62..40bf2603c4 100644 --- a/guides/source/testing.md +++ b/guides/source/testing.md @@ -1,8 +1,7 @@ A Guide to Testing Rails Applications ===================================== -This guide covers built-in mechanisms offered by Rails to test your -application. +This guide covers built-in mechanisms in Rails for testing your application. After reading this guide, you will know: @@ -234,7 +233,7 @@ 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 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 using `-n` switch with the `test method name`. ```bash $ rails test test/models/post_test.rb -n test_the_truth @@ -245,7 +244,7 @@ Finished tests in 0.009064s, 110.3266 tests/s, 110.3266 assertions/s. 1 tests, 1 assertions, 0 failures, 0 errors, 0 skips ``` -This will run all the test methods from the test case. Note that `test_helper.rb` is in the `test` directory, hence this directory needs to be added to the load path using the `-I` switch. +This will run all test methods from the test case. Note that `test_helper.rb` is in the `test` directory, hence this directory needs to be added to the load path using the `-I` switch. The `.` (dot) above indicates a passing test. When a test fails you see an `F`; when a test throws an error you see an `E` in its place. The last line of the output is the summary. diff --git a/railties/CHANGELOG.md b/railties/CHANGELOG.md index 4145938063..e3b1bc37c6 100644 --- a/railties/CHANGELOG.md +++ b/railties/CHANGELOG.md @@ -19,7 +19,7 @@ *Terence Lee* -* Rails now generate a `test/test_helper.rb` file with `fixtures :all` commented out by default, +* 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. @@ -27,24 +27,27 @@ *Prem Sichanugrist* -* Add `rails test` command to run the test suite +* Add `rails test` command for running tests - To run the whole test suite: + To run all tests: $ rails test - To run the test file(s): - - $ rails test test/unit/foo_test.rb [test/unit/bar_test.rb ...] - - To run the test suite + To run a test suite $ rails 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 ...] + + 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 replacing `rake test:*`, and `rake test` - command will actually invoking `rails test` instead. + This command will eventually replace `rake test:*` and `rake test` tasks *Prem Sichanugrist and Chris Toomey* diff --git a/railties/lib/rails/commands/test_runner.rb b/railties/lib/rails/commands/test_runner.rb index ae901f05f8..92c4d4e593 100644 --- a/railties/lib/rails/commands/test_runner.rb +++ b/railties/lib/rails/commands/test_runner.rb @@ -2,13 +2,13 @@ require 'optparse' require 'minitest/unit' module Rails - # Handling the all the logic behind +rails test+ command. + # Handles all logic behind +rails test+ command. class TestRunner class << self - # Parse the test suite name from the arguments array and pass in a list - # 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. + # 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 @@ -36,18 +36,18 @@ module Rails end end - # Parse arguments and set them as option flags + # 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 type]" + opts.banner = "Usage: rails test [path to test file(s) or test suite]" opts.separator "" - opts.separator "Run single test file, or a test suite, under Rails'" + 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 the test suites." + opts.separator "Rails will run all tests." opts.separator "" opts.separator "Specific options:" @@ -91,7 +91,7 @@ module Rails end end - # Create a new +TestRunner+ object with a list of test file paths. + # Creates a new +TestRunner+ object with a list of test file paths. def initialize(files, options) @files = files Rake::Task['test:prepare'].invoke @@ -108,25 +108,25 @@ module Rails MiniTest::Unit.output = SilentUntilSyncStream.new(MiniTest::Unit.output) end - # Run the test files by evaluate each of them. + # 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 to be used to silence unnecessary output from - # MiniTest, as MiniTest calls +output.sync = true+ right before output the - # first test result. + # 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 - # Create a +SilentUntilSyncStream+ object by given a stream object that - # this stream should set +MiniTest::Unit.output+ to after +sync+ has been + # 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 - # Swap +MiniTest::Unit.output+ to another stream when +sync+ is true. + # Swaps +MiniTest::Unit.output+ to another stream when +sync+ is true. def sync=(sync) if sync @target_stream.sync = true diff --git a/railties/lib/rails/generators/rails/app/templates/test/test_helper.rb b/railties/lib/rails/generators/rails/app/templates/test/test_helper.rb index 754e99e09f..f0aeeee827 100644 --- a/railties/lib/rails/generators/rails/app/templates/test/test_helper.rb +++ b/railties/lib/rails/generators/rails/app/templates/test/test_helper.rb @@ -6,8 +6,8 @@ class ActiveSupport::TestCase <% unless options[:skip_active_record] -%> ActiveRecord::Migration.check_pending! - # Uncomment the `fixtures` line below to setup all fixtures in test/fixtures/*.yml for all tests - # in alphabetical order. + # Uncomment the `fixtures :all` line below to setup all fixtures in test/fixtures/*.yml + # for all tests in alphabetical order. # # Note: You'll currently still have to declare fixtures explicitly in integration tests # -- they do not yet inherit this setting diff --git a/railties/test/application/test_runner_test.rb b/railties/test/application/test_runner_test.rb index 71e2b403af..810748682a 100644 --- a/railties/test/application/test_runner_test.rb +++ b/railties/test/application/test_runner_test.rb @@ -141,11 +141,11 @@ module ApplicationTests end end - def test_run_whole_suite - types = [:models, :helpers, :unit, :controllers, :mailers, :functional, :integration] - types.each { |type| create_test_file type, "foo_#{type}" } + def test_run_all_suites + suites = [:models, :helpers, :unit, :controllers, :mailers, :functional, :integration] + suites.each { |suite| create_test_file suite, "foo_#{suite}" } run_test_command('') .tap do |output| - types.each { |type| assert_match /Foo#{type.to_s.camelize}Test/, output } + suites.each { |suite| assert_match /Foo#{suite.to_s.camelize}Test/, output } assert_match /7 tests, 7 assertions, 0 failures/, output end end @@ -180,13 +180,13 @@ module ApplicationTests def test_load_fixtures_when_running_test_suites create_model_with_fixture - types = [:models, :helpers, [:units, :unit], :controllers, :mailers, + suites = [:models, :helpers, [:units, :unit], :controllers, :mailers, [:functionals, :functional], :integration] - types.each do |type, directory| - directory ||= type + suites.each do |suite, directory| + directory ||= suite create_fixture_test directory - assert_match /3 users/, run_test_command(type) + assert_match /3 users/, run_test_command(suite) Dir.chdir(app_path) { FileUtils.rm_f "test/#{directory}" } end end