From b4df25366a3c8f133f8329bc35f1d53926704b5a Mon Sep 17 00:00:00 2001 From: Prem Sichanugrist and Chris Toomey Date: Fri, 25 Jan 2013 13:44:36 -0500 Subject: Add `rails test` command to run the test suite To run the whole test suite: $ 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 $ rails test [models,helpers,units,controllers,mailers,...] For more information, see `rails test --help`. This command will eventually replacing `rake test:*`, and `rake test` command will actually invoking `rails test` instead. --- railties/test/application/test_runner_test.rb | 183 ++++++++++++++++++++++++++ 1 file changed, 183 insertions(+) create mode 100644 railties/test/application/test_runner_test.rb (limited to 'railties/test') diff --git a/railties/test/application/test_runner_test.rb b/railties/test/application/test_runner_test.rb new file mode 100644 index 0000000000..49bce508ee --- /dev/null +++ b/railties/test/application/test_runner_test.rb @@ -0,0 +1,183 @@ +require 'isolation/abstract_unit' + +module ApplicationTests + class TestRunnerTest < ActiveSupport::TestCase + include ActiveSupport::Testing::Isolation + + def setup + build_app + create_schema + end + + def teardown + teardown_app + end + + def test_should_not_display_heading + create_test_file + run_test_command.tap do |output| + assert_no_match /Run options:/, output + assert_no_match /Running tests:/, output + end + 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' + assert_match /1 tests, 1 assertions, 0 failures/, run_test_command("test/models/foo_test.rb") + end + + def test_run_multiple_files + create_test_file :models, 'foo' + create_test_file :models, 'bar' + assert_match /2 tests, 2 assertions, 0 failures/, run_test_command("test/models/foo_test.rb test/models/bar_test.rb") + end + + def test_run_file_with_syntax_error + app_file 'test/models/error_test.rb', <<-RUBY + require 'test_helper' + def; end + RUBY + + error_stream = Tempfile.new('error') + redirect_stderr(error_stream) { run_test_command('test/models/error_test.rb') } + assert_match /SyntaxError/, error_stream.read + end + + def test_invoke_rake_test_prepare + app_file "lib/tasks/test.rake", <<-RUBY + namespace :test do + task :prepare do + puts "Hello World" + end + end + RUBY + create_test_file + assert_match /Hello World/, run_test_command + end + + def test_run_models + create_test_file :models, 'foo' + create_test_file :models, 'bar' + create_test_file :controllers, 'foobar_controller' + run_test_command("models").tap do |output| + assert_match /FooTest/, output + assert_match /BarTest/, output + assert_match /2 tests, 2 assertions, 0 failures/, output + end + end + + def test_run_helpers + create_test_file :helpers, 'foo_helper' + create_test_file :helpers, 'bar_helper' + create_test_file :controllers, 'foobar_controller' + run_test_command('helpers').tap do |output| + assert_match /FooHelperTest/, output + assert_match /BarHelperTest/, output + assert_match /2 tests, 2 assertions, 0 failures/, output + end + end + + def test_run_units + create_test_file :models, 'foo' + create_test_file :helpers, 'bar_helper' + create_test_file :unit, 'baz_unit' + create_test_file :controllers, 'foobar_controller' + run_test_command('units').tap do |output| + assert_match /FooTest/, output + assert_match /BarHelperTest/, output + assert_match /BazUnitTest/, output + assert_match /3 tests, 3 assertions, 0 failures/, output + end + end + + def test_run_controllers + create_test_file :controllers, 'foo_controller' + create_test_file :controllers, 'bar_controller' + create_test_file :models, 'foo' + run_test_command('controllers').tap do |output| + assert_match /FooControllerTest/, output + assert_match /BarControllerTest/, output + assert_match /2 tests, 2 assertions, 0 failures/, output + end + end + + def test_run_mailers + create_test_file :mailers, 'foo_mailer' + create_test_file :mailers, 'bar_mailer' + create_test_file :models, 'foo' + run_test_command('mailers').tap do |output| + assert_match /FooMailerTest/, output + assert_match /BarMailerTest/, output + assert_match /2 tests, 2 assertions, 0 failures/, output + end + end + + def test_run_functionals + create_test_file :mailers, 'foo_mailer' + create_test_file :controllers, 'bar_controller' + create_test_file :functional, 'baz_functional' + create_test_file :models, 'foo' + run_test_command('functionals').tap do |output| + assert_match /FooMailerTest/, output + assert_match /BarControllerTest/, output + assert_match /BazFunctionalTest/, output + assert_match /3 tests, 3 assertions, 0 failures/, output + end + end + + def test_run_integration + create_test_file :integration, 'foo_integration' + create_test_file :models, 'foo' + run_test_command('integration').tap do |output| + assert_match /FooIntegration/, output + assert_match /1 tests, 1 assertions, 0 failures/, output + end + end + + def test_run_whole_suite + types = [:models, :helpers, :unit, :controllers, :mailers, :functional, :integration] + types.each { |type| create_test_file type, "foo_#{type}" } + run_test_command('') .tap do |output| + types.each { |type| assert_match /Foo#{type.to_s.camelize}Test/, output } + assert_match /7 tests, 7 assertions, 0 failures/, output + end + end + + private + def run_test_command(arguments = 'test/unit/test_test.rb') + Dir.chdir(app_path) { `bundle exec rails test #{arguments}` } + end + + def create_schema + app_file 'db/schema.rb', '' + end + + def redirect_stderr(target_stream) + previous_stderr = STDERR.dup + $stderr.reopen(target_stream) + yield + target_stream.rewind + ensure + $stderr = previous_stderr + end + + def create_test_file(path = :unit, name = 'test') + app_file "test/#{path}/#{name}_test.rb", <<-RUBY + require 'test_helper' + + class #{name.camelize}Test < ActiveSupport::TestCase + def test_truth + puts "#{name.camelize}Test" + assert true + end + end + RUBY + end + end +end -- cgit v1.2.3 From 176b57c5430ddae7668114994c35b3293b0a05a5 Mon Sep 17 00:00:00 2001 From: Prem Sichanugrist Date: Fri, 1 Feb 2013 17:21:09 -0500 Subject: 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`. --- railties/test/application/test_runner_test.rb | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'railties/test') diff --git a/railties/test/application/test_runner_test.rb b/railties/test/application/test_runner_test.rb index 49bce508ee..249bfd1d5d 100644 --- a/railties/test/application/test_runner_test.rb +++ b/railties/test/application/test_runner_test.rb @@ -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}` } -- cgit v1.2.3 From 1a0c58b2988a24a783b4f9a658ac629922125551 Mon Sep 17 00:00:00 2001 From: Prem Sichanugrist Date: Wed, 6 Feb 2013 01:03:17 -0500 Subject: Load fixtures only when running suites, or `-f` * `rails test -f` will run the test suites with all fixtures loaded * New application will now generated without `fixtures :all` line enabled by default. --- railties/test/application/test_runner_test.rb | 51 +++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) (limited to 'railties/test') diff --git a/railties/test/application/test_runner_test.rb b/railties/test/application/test_runner_test.rb index 249bfd1d5d..71e2b403af 100644 --- a/railties/test/application/test_runner_test.rb +++ b/railties/test/application/test_runner_test.rb @@ -1,4 +1,5 @@ require 'isolation/abstract_unit' +require 'active_support/core_ext/string/strip' module ApplicationTests class TestRunnerTest < ActiveSupport::TestCase @@ -170,11 +171,61 @@ module ApplicationTests end end + def test_not_load_fixtures_when_running_single_test + create_model_with_fixture + create_fixture_test :models, 'user' + assert_match /0 users/, run_test_command('test/models/user_test.rb') + assert_match /3 users/, run_test_command('test/models/user_test.rb -f') + end + + def test_load_fixtures_when_running_test_suites + create_model_with_fixture + types = [:models, :helpers, [:units, :unit], :controllers, :mailers, + [:functionals, :functional], :integration] + + types.each do |type, directory| + directory ||= type + create_fixture_test directory + assert_match /3 users/, run_test_command(type) + Dir.chdir(app_path) { FileUtils.rm_f "test/#{directory}" } + end + end + private def run_test_command(arguments = 'test/unit/test_test.rb') Dir.chdir(app_path) { `bundle exec rails test #{arguments}` } end + def create_model_with_fixture + script 'generate model user name:string' + + app_file 'test/fixtures/users.yml', <<-YAML.strip_heredoc + vampire: + id: 1 + name: Koyomi Araragi + crab: + id: 2 + name: Senjougahara Hitagi + cat: + id: 3 + name: Tsubasa Hanekawa + YAML + + Dir.chdir(app_path) { `bundle exec rake db:migrate` } + end + + def create_fixture_test(path = :unit, name = 'test') + app_file "test/#{path}/#{name}_test.rb", <<-RUBY + require 'test_helper' + + class #{name.camelize}Test < ActiveSupport::TestCase + def test_fixture + puts "\#{User.count} users (\#{__FILE__})" + end + end + RUBY + end + def create_schema app_file 'db/schema.rb', '' end -- cgit v1.2.3 From df85dfa6fa6f7ae9a0b72eb9b9a254d2d5560f38 Mon Sep 17 00:00:00 2001 From: Dalibor Nasevic Date: Fri, 8 Feb 2013 00:53:11 +0100 Subject: Improve wording for rails test command --- railties/test/application/test_runner_test.rb | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) (limited to 'railties/test') 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 -- cgit v1.2.3 From b51673fbd9563bd3ffa22e22255ca1cef80cfb6d Mon Sep 17 00:00:00 2001 From: Prem Sichanugrist Date: Mon, 25 Feb 2013 17:04:01 -0500 Subject: Update Rake tasks to call `rails test` instead Also, print out deprecation warning for other rake tasks except `rake test` and `rake` (default) --- railties/test/application/rake_test.rb | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) (limited to 'railties/test') diff --git a/railties/test/application/rake_test.rb b/railties/test/application/rake_test.rb index 09f2ad1209..a9e0e1bcb7 100644 --- a/railties/test/application/rake_test.rb +++ b/railties/test/application/rake_test.rb @@ -91,19 +91,9 @@ module ApplicationTests raise 'models' RUBY - app_file "test/controllers/one_controller_test.rb", <<-RUBY - raise 'controllers' - RUBY - - app_file "test/integration/one_integration_test.rb", <<-RUBY - raise 'integration' - RUBY - silence_stderr do output = Dir.chdir(app_path) { `rake test 2>&1` } assert_match 'models', output - assert_match 'controllers', output - assert_match 'integration', output end end @@ -135,6 +125,19 @@ module ApplicationTests end end + def test_rake_test_deprecation_messages + Dir.chdir(app_path){ `rails generate scaffold user name:string` } + Dir.chdir(app_path){ `rake db:migrate` } + + %w(run recent uncommitted models helpers units controllers functionals integration).each do |test_suit_name| + output = Dir.chdir(app_path) { `rake test:#{test_suit_name} 2>&1` } + assert_match /DEPRECATION WARNING: `rake test:#{test_suit_name}` is deprecated/, output + end + + assert_match /DEPRECATION WARNING: `rake test:single` is deprecated/, + Dir.chdir(app_path) { `rake test:single TEST=test/models/user_test.rb 2>&1` } + end + def test_rake_routes_calls_the_route_inspector app_file "config/routes.rb", <<-RUBY AppTemplate::Application.routes.draw do -- cgit v1.2.3 From 3ed41e579e45464aa6e6342783b77f9ec29e339c Mon Sep 17 00:00:00 2001 From: Prem Sichanugrist Date: Sat, 9 Mar 2013 16:03:09 -0500 Subject: Make sure that `rails test` load test in test env --- railties/test/application/test_runner_test.rb | 45 ++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) (limited to 'railties/test') diff --git a/railties/test/application/test_runner_test.rb b/railties/test/application/test_runner_test.rb index 810748682a..7a5a428845 100644 --- a/railties/test/application/test_runner_test.rb +++ b/railties/test/application/test_runner_test.rb @@ -191,6 +191,39 @@ module ApplicationTests end end + def test_run_different_environment_using_env_var + app_file 'test/unit/env_test.rb', <<-RUBY + require 'test_helper' + + class EnvTest < ActiveSupport::TestCase + def test_env + puts Rails.env + end + end + RUBY + + assert_match /development/, Dir.chdir(app_path) { `RAILS_ENV=development bundle exec rails test test/unit/env_test.rb` } + end + + def test_run_different_environment_using_e_tag + app_file 'test/unit/env_test.rb', <<-RUBY + require 'test_helper' + + class EnvTest < ActiveSupport::TestCase + def test_env + puts Rails.env + end + end + RUBY + + assert_match /development/, run_test_command('-e development test/unit/env_test.rb') + end + + def test_generated_scaffold_works_with_rails_test + create_scaffold + assert_match /0 failures, 0 errors, 0 skips/, run_test_command('') + end + private def run_test_command(arguments = 'test/unit/test_test.rb') Dir.chdir(app_path) { `bundle exec rails test #{arguments}` } @@ -211,7 +244,7 @@ module ApplicationTests name: Tsubasa Hanekawa YAML - Dir.chdir(app_path) { `bundle exec rake db:migrate` } + run_migration end def create_fixture_test(path = :unit, name = 'test') @@ -251,5 +284,15 @@ module ApplicationTests end RUBY end + + def create_scaffold + script 'generate scaffold user name:string' + Dir.chdir(app_path) { File.exist?('app/models/user.rb') } + run_migration + end + + def run_migration + Dir.chdir(app_path) { `bundle exec rake db:migrate` } + end end end -- cgit v1.2.3