2017-08-14 17:08:09 +00:00
# frozen_string_literal: true
2016-08-06 17:16:09 +00:00
require " isolation/abstract_unit "
require " env_helpers "
2013-01-25 18:44:36 +00:00
module ApplicationTests
class TestRunnerTest < ActiveSupport :: TestCase
2015-03-21 12:15:56 +00:00
include ActiveSupport :: Testing :: Isolation , EnvHelpers
2013-01-25 18:44:36 +00:00
def setup
build_app
create_schema
end
def teardown
teardown_app
end
2019-02-01 11:12:40 +00:00
def test_run_via_backwards_compatibility
2017-06-25 17:08:26 +00:00
require " minitest/rails_plugin "
2017-02-21 07:25:15 +00:00
assert_nothing_raised do
Minitest . run_via [ :ruby ] = true
end
2017-06-25 17:08:26 +00:00
assert Minitest . run_via [ :ruby ]
2017-02-21 07:25:15 +00:00
end
2013-01-25 18:44:36 +00:00
def test_run_single_file
2016-08-06 17:16:09 +00:00
create_test_file :models , " foo "
create_test_file :models , " bar "
2013-05-07 00:38:45 +00:00
assert_match " 1 runs, 1 assertions, 0 failures " , run_test_command ( " test/models/foo_test.rb " )
2013-01-25 18:44:36 +00:00
end
2017-07-25 01:53:01 +00:00
def test_run_single_file_with_absolute_path
create_test_file :models , " foo "
create_test_file :models , " bar "
assert_match " 1 runs, 1 assertions, 0 failures " , run_test_command ( " #{ app_path } /test/models/foo_test.rb " )
end
2013-01-25 18:44:36 +00:00
def test_run_multiple_files
2016-08-06 17:16:09 +00:00
create_test_file :models , " foo "
create_test_file :models , " bar "
2013-05-07 00:38:45 +00:00
assert_match " 2 runs, 2 assertions, 0 failures " , run_test_command ( " test/models/foo_test.rb test/models/bar_test.rb " )
2013-01-25 18:44:36 +00:00
end
2017-07-25 01:53:01 +00:00
def test_run_multiple_files_with_absolute_paths
create_test_file :models , " foo "
create_test_file :controllers , " foobar_controller "
create_test_file :models , " bar "
assert_match " 2 runs, 2 assertions, 0 failures " , run_test_command ( " #{ app_path } /test/models/foo_test.rb #{ app_path } /test/controllers/foobar_controller_test.rb " )
end
2013-01-25 18:44:36 +00:00
def test_run_file_with_syntax_error
2016-08-06 17:16:09 +00:00
app_file " test/models/error_test.rb " , <<-RUBY
2013-01-25 18:44:36 +00:00
require 'test_helper'
def ; end
RUBY
2017-09-03 16:55:26 +00:00
error = capture ( :stderr ) { run_test_command ( " test/models/error_test.rb " , stderr : true ) }
2015-01-30 11:04:57 +00:00
assert_match " syntax error " , error
2013-01-25 18:44:36 +00:00
end
def test_run_models
2016-08-06 17:16:09 +00:00
create_test_file :models , " foo "
create_test_file :models , " bar "
create_test_file :controllers , " foobar_controller "
2015-01-30 13:54:41 +00:00
run_test_command ( " test/models " ) . tap do | output |
2013-03-11 18:48:23 +00:00
assert_match " FooTest " , output
assert_match " BarTest " , output
2013-05-07 00:38:45 +00:00
assert_match " 2 runs, 2 assertions, 0 failures " , output
2013-01-25 18:44:36 +00:00
end
end
def test_run_helpers
2016-08-06 17:16:09 +00:00
create_test_file :helpers , " foo_helper "
create_test_file :helpers , " bar_helper "
create_test_file :controllers , " foobar_controller "
2015-01-30 13:54:41 +00:00
run_test_command ( " test/helpers " ) . tap do | output |
2013-03-11 18:48:23 +00:00
assert_match " FooHelperTest " , output
assert_match " BarHelperTest " , output
2013-05-07 00:38:45 +00:00
assert_match " 2 runs, 2 assertions, 0 failures " , output
2013-01-25 18:44:36 +00:00
end
end
def test_run_units
2016-08-06 17:16:09 +00:00
create_test_file :models , " foo "
create_test_file :helpers , " bar_helper "
create_test_file :unit , " baz_unit "
create_test_file :controllers , " foobar_controller "
2017-02-23 17:38:43 +00:00
2017-09-03 16:55:26 +00:00
rails ( " test:units " ) . tap do | output |
assert_match " FooTest " , output
assert_match " BarHelperTest " , output
assert_match " BazUnitTest " , output
assert_match " 3 runs, 3 assertions, 0 failures " , output
2013-01-25 18:44:36 +00:00
end
end
2019-01-16 15:28:13 +00:00
def test_run_channels
create_test_file :channels , " foo_channel "
create_test_file :channels , " bar_channel "
rails ( " test:channels " ) . tap do | output |
assert_match " FooChannelTest " , output
assert_match " BarChannelTest " , output
assert_match " 2 runs, 2 assertions, 0 failures " , output
end
end
2013-01-25 18:44:36 +00:00
def test_run_controllers
2016-08-06 17:16:09 +00:00
create_test_file :controllers , " foo_controller "
create_test_file :controllers , " bar_controller "
create_test_file :models , " foo "
2015-01-30 13:54:41 +00:00
run_test_command ( " test/controllers " ) . tap do | output |
2013-03-11 18:48:23 +00:00
assert_match " FooControllerTest " , output
assert_match " BarControllerTest " , output
2013-05-07 00:38:45 +00:00
assert_match " 2 runs, 2 assertions, 0 failures " , output
2013-01-25 18:44:36 +00:00
end
end
def test_run_mailers
2016-08-06 17:16:09 +00:00
create_test_file :mailers , " foo_mailer "
create_test_file :mailers , " bar_mailer "
create_test_file :models , " foo "
2015-01-30 13:54:41 +00:00
run_test_command ( " test/mailers " ) . tap do | output |
2013-03-11 18:48:23 +00:00
assert_match " FooMailerTest " , output
assert_match " BarMailerTest " , output
2013-05-07 00:38:45 +00:00
assert_match " 2 runs, 2 assertions, 0 failures " , output
2013-01-25 18:44:36 +00:00
end
end
2014-09-14 23:23:14 +00:00
def test_run_jobs
2016-08-06 17:16:09 +00:00
create_test_file :jobs , " foo_job "
create_test_file :jobs , " bar_job "
create_test_file :models , " foo "
2015-01-30 13:54:41 +00:00
run_test_command ( " test/jobs " ) . tap do | output |
2014-09-14 23:23:14 +00:00
assert_match " FooJobTest " , output
assert_match " BarJobTest " , output
assert_match " 2 runs, 2 assertions, 0 failures " , output
end
end
2018-12-30 21:17:16 +00:00
def test_run_mailboxes
create_test_file :mailboxes , " foo_mailbox "
create_test_file :mailboxes , " bar_mailbox "
create_test_file :models , " foo "
rails ( " test:mailboxes " ) . tap do | output |
assert_match " FooMailboxTest " , output
assert_match " BarMailboxTest " , output
assert_match " 2 runs, 2 assertions, 0 failures " , output
end
end
2013-01-25 18:44:36 +00:00
def test_run_functionals
2016-08-06 17:16:09 +00:00
create_test_file :mailers , " foo_mailer "
create_test_file :controllers , " bar_controller "
create_test_file :functional , " baz_functional "
create_test_file :models , " foo "
2017-02-23 17:38:43 +00:00
2017-09-03 16:55:26 +00:00
rails ( " test:functionals " ) . tap do | output |
assert_match " FooMailerTest " , output
assert_match " BarControllerTest " , output
assert_match " BazFunctionalTest " , output
assert_match " 3 runs, 3 assertions, 0 failures " , output
2013-01-25 18:44:36 +00:00
end
end
def test_run_integration
2016-08-06 17:16:09 +00:00
create_test_file :integration , " foo_integration "
create_test_file :models , " foo "
2015-01-30 13:54:41 +00:00
run_test_command ( " test/integration " ) . tap do | output |
2013-03-11 18:48:23 +00:00
assert_match " FooIntegration " , output
2013-05-07 00:38:45 +00:00
assert_match " 1 runs, 1 assertions, 0 failures " , output
2013-01-25 18:44:36 +00:00
end
end
2013-02-07 23:53:11 +00:00
def test_run_all_suites
2019-01-16 15:28:13 +00:00
suites = [ :models , :helpers , :unit , :channels , :controllers , :mailers , :functional , :integration , :jobs , :mailboxes ]
2013-02-07 23:53:11 +00:00
suites . each { | suite | create_test_file suite , " foo_ #{ suite } " }
2016-08-06 17:16:09 +00:00
run_test_command ( " " ) . tap do | output |
2013-03-11 18:48:23 +00:00
suites . each { | suite | assert_match " Foo #{ suite . to_s . camelize } Test " , output }
2019-01-16 15:28:13 +00:00
assert_match " 10 runs, 10 assertions, 0 failures " , output
2013-01-25 18:44:36 +00:00
end
end
2013-02-01 22:21:09 +00:00
def test_run_named_test
2016-08-06 17:16:09 +00:00
app_file " test/unit/chu_2_koi_test.rb " , <<-RUBY
2013-02-01 22:21:09 +00:00
require 'test_helper'
class Chu2KoiTest < ActiveSupport :: TestCase
def test_rikka
puts 'Rikka'
end
def test_sanae
puts 'Sanae'
end
end
RUBY
2016-08-06 17:16:09 +00:00
run_test_command ( " -n test_rikka test/unit/chu_2_koi_test.rb " ) . tap do | output |
2013-04-05 23:38:37 +00:00
assert_match " Rikka " , output
assert_no_match " Sanae " , output
end
end
def test_run_matched_test
2016-08-06 17:16:09 +00:00
app_file " test/unit/chu_2_koi_test.rb " , <<-RUBY
2013-04-05 23:38:37 +00:00
require 'test_helper'
class Chu2KoiTest < ActiveSupport :: TestCase
def test_rikka
puts 'Rikka'
end
def test_sanae
puts 'Sanae'
end
end
RUBY
2016-08-06 17:16:09 +00:00
run_test_command ( " -n /rikka/ test/unit/chu_2_koi_test.rb " ) . tap do | output |
2013-03-11 18:48:23 +00:00
assert_match " Rikka " , output
assert_no_match " Sanae " , output
2013-02-01 22:21:09 +00:00
end
end
2013-02-06 06:03:17 +00:00
def test_load_fixtures_when_running_test_suites
create_model_with_fixture
2015-01-30 13:54:41 +00:00
suites = [ :models , :helpers , :controllers , :mailers , :integration ]
2013-02-06 06:03:17 +00:00
2013-02-07 23:53:11 +00:00
suites . each do | suite , directory |
directory || = suite
2013-02-06 06:03:17 +00:00
create_fixture_test directory
2015-01-30 13:54:41 +00:00
assert_match " 3 users " , run_test_command ( " test/ #{ suite } " )
2013-02-06 06:03:17 +00:00
Dir . chdir ( app_path ) { FileUtils . rm_f " test/ #{ directory } " }
end
end
2013-04-06 00:12:23 +00:00
def test_run_with_model
2015-01-30 13:54:41 +00:00
skip " These feel a bit odd. Not sure we should keep supporting them. "
2013-04-06 00:12:23 +00:00
create_model_with_fixture
2016-08-06 17:16:09 +00:00
create_fixture_test " models " , " user "
2013-04-06 00:12:23 +00:00
assert_match " 3 users " , run_task ( [ " test models/user " ] )
assert_match " 3 users " , run_task ( [ " test app/models/user.rb " ] )
end
2013-03-09 21:03:09 +00:00
def test_run_different_environment_using_env_var
2015-01-30 13:54:41 +00:00
skip " no longer possible. Running tests in a different environment should be explicit "
2016-08-06 17:16:09 +00:00
app_file " test/unit/env_test.rb " , <<-RUBY
2013-03-09 21:03:09 +00:00
require 'test_helper'
class EnvTest < ActiveSupport :: TestCase
def test_env
puts Rails . env
end
end
RUBY
2016-08-06 17:16:09 +00:00
ENV [ " RAILS_ENV " ] = " development "
assert_match " development " , run_test_command ( " test/unit/env_test.rb " )
2013-03-09 21:03:09 +00:00
end
2015-03-21 12:15:56 +00:00
def test_run_in_test_environment_by_default
create_env_test
2016-08-06 17:16:09 +00:00
assert_match " Current Environment: test " , run_test_command ( " test/unit/env_test.rb " )
2015-03-21 12:15:56 +00:00
end
2015-01-30 13:54:41 +00:00
def test_run_different_environment
2015-03-21 12:15:56 +00:00
create_env_test
assert_match " Current Environment: development " ,
run_test_command ( " -e development test/unit/env_test.rb " )
end
def test_generated_scaffold_works_with_rails_test
create_scaffold
2016-08-06 17:16:09 +00:00
assert_match " 0 failures, 0 errors, 0 skips " , run_test_command ( " " )
2015-03-21 12:15:56 +00:00
end
2016-01-06 06:01:14 +00:00
def test_generated_controller_works_with_rails_test
create_controller
2016-08-06 17:16:09 +00:00
assert_match " 0 failures, 0 errors, 0 skips " , run_test_command ( " " )
2016-01-06 06:01:14 +00:00
end
2015-03-21 12:15:56 +00:00
def test_run_multiple_folders
2016-08-06 17:16:09 +00:00
create_test_file :models , " account "
create_test_file :controllers , " accounts_controller "
2015-03-21 12:15:56 +00:00
2016-08-06 17:16:09 +00:00
run_test_command ( " test/models test/controllers " ) . tap do | output |
assert_match " AccountTest " , output
assert_match " AccountsControllerTest " , output
assert_match " 2 runs, 2 assertions, 0 failures, 0 errors, 0 skips " , output
2015-03-21 12:15:56 +00:00
end
end
2017-07-25 01:53:01 +00:00
def test_run_multiple_folders_with_absolute_paths
create_test_file :models , " account "
create_test_file :controllers , " accounts_controller "
create_test_file :helpers , " foo_helper "
run_test_command ( " #{ app_path } /test/models #{ app_path } /test/controllers " ) . tap do | output |
assert_match " AccountTest " , output
assert_match " AccountsControllerTest " , output
assert_match " 2 runs, 2 assertions, 0 failures, 0 errors, 0 skips " , output
end
end
2015-03-21 12:15:56 +00:00
def test_run_with_ruby_command
2016-08-06 17:16:09 +00:00
app_file " test/models/post_test.rb " , <<-RUBY
2013-03-09 21:03:09 +00:00
require 'test_helper'
2015-03-21 12:15:56 +00:00
class PostTest < ActiveSupport :: TestCase
test 'declarative syntax works' do
puts 'PostTest'
assert true
2013-03-09 21:03:09 +00:00
end
end
RUBY
2015-03-21 12:15:56 +00:00
Dir . chdir ( app_path ) do
` ruby -Itest test/models/post_test.rb ` . tap do | output |
2016-08-06 17:16:09 +00:00
assert_match " PostTest " , output
assert_no_match " is already defined in " , output
2015-03-21 12:15:56 +00:00
end
end
2013-03-09 21:03:09 +00:00
end
2015-03-21 12:15:56 +00:00
def test_mix_files_and_line_filters
2016-08-06 17:16:09 +00:00
create_test_file :models , " account "
app_file " test/models/post_test.rb " , <<-RUBY
2015-03-21 12:15:56 +00:00
require 'test_helper'
class PostTest < ActiveSupport :: TestCase
def test_post
puts 'PostTest'
assert true
end
def test_line_filter_does_not_run_this
assert true
end
end
RUBY
2016-08-06 17:16:09 +00:00
run_test_command ( " test/models/account_test.rb test/models/post_test.rb:4 " ) . tap do | output |
assert_match " AccountTest " , output
assert_match " PostTest " , output
assert_match " 2 runs, 2 assertions " , output
2015-03-21 12:15:56 +00:00
end
end
2015-12-29 17:27:27 +00:00
def test_more_than_one_line_filter
2016-08-06 17:16:09 +00:00
app_file " test/models/post_test.rb " , <<-RUBY
2015-12-29 17:27:27 +00:00
require 'test_helper'
class PostTest < ActiveSupport :: TestCase
test " first filter " do
puts 'PostTest:FirstFilter'
assert true
end
test " second filter " do
puts 'PostTest:SecondFilter'
assert true
end
2017-04-06 23:40:52 +00:00
test " line filter does not run this " do
2015-12-29 17:27:27 +00:00
assert true
end
end
RUBY
2016-08-06 17:16:09 +00:00
run_test_command ( " test/models/post_test.rb:4:9 " ) . tap do | output |
assert_match " PostTest:FirstFilter " , output
assert_match " PostTest:SecondFilter " , output
assert_match " 2 runs, 2 assertions " , output
2015-12-29 17:27:27 +00:00
end
end
def test_more_than_one_line_filter_with_multiple_files
2016-08-06 17:16:09 +00:00
app_file " test/models/account_test.rb " , <<-RUBY
2015-12-29 17:27:27 +00:00
require 'test_helper'
class AccountTest < ActiveSupport :: TestCase
test " first filter " do
puts 'AccountTest:FirstFilter'
assert true
end
test " second filter " do
puts 'AccountTest:SecondFilter'
assert true
end
test " line filter does not run this " do
assert true
end
end
RUBY
2016-08-06 17:16:09 +00:00
app_file " test/models/post_test.rb " , <<-RUBY
2015-12-29 17:27:27 +00:00
require 'test_helper'
class PostTest < ActiveSupport :: TestCase
test " first filter " do
puts 'PostTest:FirstFilter'
assert true
end
test " second filter " do
puts 'PostTest:SecondFilter'
assert true
end
test " line filter does not run this " do
assert true
end
end
RUBY
2016-08-06 17:16:09 +00:00
run_test_command ( " test/models/account_test.rb:4:9 test/models/post_test.rb:4:9 " ) . tap do | output |
assert_match " AccountTest:FirstFilter " , output
assert_match " AccountTest:SecondFilter " , output
assert_match " PostTest:FirstFilter " , output
assert_match " PostTest:SecondFilter " , output
assert_match " 4 runs, 4 assertions " , output
2015-12-29 17:27:27 +00:00
end
end
2015-03-21 12:15:56 +00:00
def test_multiple_line_filters
2016-08-06 17:16:09 +00:00
create_test_file :models , " account "
create_test_file :models , " post "
2015-03-21 12:15:56 +00:00
2016-08-06 17:16:09 +00:00
run_test_command ( " test/models/account_test.rb:4 test/models/post_test.rb:4 " ) . tap do | output |
assert_match " AccountTest " , output
assert_match " PostTest " , output
2015-03-21 12:15:56 +00:00
end
end
2016-02-03 20:58:55 +00:00
def test_line_filters_trigger_only_one_runnable
2016-08-06 17:16:09 +00:00
app_file " test/models/post_test.rb " , <<-RUBY
2016-02-03 20:58:55 +00:00
require 'test_helper'
class PostTest < ActiveSupport :: TestCase
test 'truth' do
assert true
end
end
class SecondPostTest < ActiveSupport :: TestCase
test 'truth' do
assert false , 'ran second runnable'
end
end
RUBY
# Pass seed guaranteeing failure.
2016-08-06 17:16:09 +00:00
run_test_command ( " test/models/post_test.rb:4 --seed 30410 " ) . tap do | output |
assert_no_match " ran second runnable " , output
assert_match " 1 runs, 1 assertions " , output
2016-02-03 20:58:55 +00:00
end
end
2016-02-04 22:06:33 +00:00
def test_line_filter_with_minitest_string_filter
2016-08-06 17:16:09 +00:00
app_file " test/models/post_test.rb " , <<-RUBY
2016-02-04 22:06:33 +00:00
require 'test_helper'
class PostTest < ActiveSupport :: TestCase
test 'by line' do
puts 'by line'
assert true
end
test 'by name' do
puts 'by name'
assert true
end
end
RUBY
2016-08-06 17:16:09 +00:00
run_test_command ( " test/models/post_test.rb:4 -n test_by_name " ) . tap do | output |
assert_match " by line " , output
assert_match " by name " , output
assert_match " 2 runs, 2 assertions " , output
2016-02-04 22:06:33 +00:00
end
end
2015-03-21 12:15:56 +00:00
def test_shows_filtered_backtrace_by_default
create_backtrace_test
2016-08-06 17:16:09 +00:00
assert_match " Rails::BacktraceCleaner " , run_test_command ( " test/unit/backtrace_test.rb " )
2015-03-21 12:15:56 +00:00
end
def test_backtrace_option
create_backtrace_test
2016-08-06 17:16:09 +00:00
assert_match " Minitest::BacktraceFilter " , run_test_command ( " test/unit/backtrace_test.rb -b " )
assert_match " Minitest::BacktraceFilter " ,
run_test_command ( " test/unit/backtrace_test.rb --backtrace " )
2015-03-21 12:15:56 +00:00
end
def test_show_full_backtrace_using_backtrace_environment_variable
create_backtrace_test
2016-08-06 17:16:09 +00:00
switch_env " BACKTRACE " , " true " do
assert_match " Minitest::BacktraceFilter " , run_test_command ( " test/unit/backtrace_test.rb " )
2015-03-21 12:15:56 +00:00
end
end
def test_run_app_without_rails_loaded
# Simulate a real Rails app boot.
2016-08-06 17:16:09 +00:00
app_file " config/boot.rb " , <<-RUBY
2017-05-15 14:17:28 +00:00
ENV [ 'BUNDLE_GEMFILE' ] || = File . expand_path ( '../Gemfile' , __dir__ )
2015-03-21 12:15:56 +00:00
require 'bundler/setup' # Set up gems listed in the Gemfile.
RUBY
2016-08-06 17:16:09 +00:00
assert_match " 0 runs, 0 assertions " , run_test_command ( " " )
2013-03-09 21:03:09 +00:00
end
2015-08-30 10:33:37 +00:00
def test_output_inline_by_default
2017-12-20 21:59:41 +00:00
create_test_file :models , " post " , pass : false , print : false
2015-08-30 10:33:37 +00:00
2016-08-06 17:16:09 +00:00
output = run_test_command ( " test/models/post_test.rb " )
2018-06-29 15:51:57 +00:00
expect = %r{ Running: \ n \ nF \ n \ nFailure: \ nPostTest # test_truth \ [[^ \ ]]+test/models/post_test.rb:6 \ ]: \ nwups! \ n \ nrails test test/models/post_test.rb:4 \ n \ n \ n \ n }
2015-12-19 11:04:39 +00:00
assert_match expect , output
2015-08-30 10:33:37 +00:00
end
2015-10-07 20:43:29 +00:00
def test_only_inline_failure_output
2016-08-06 17:16:09 +00:00
create_test_file :models , " post " , pass : false
Hide Minitest's aggregated results if outputting inline.
We'd see the failures and errors reported after the run, which is needless, when we've already
reported them.
Turns:
```
.......................................S....................F
This failed
bin/rails test test/models/bunny_test.rb:14
....
Finished in 0.100886s, 1020.9583 runs/s, 1001.1338 assertions/s.
2) Failure:
BunnyTest#test_something_failing [/Users/kasperhansen/Documents/code/collection_caching_test/test/models/bunny_test.rb:15]:
This failed
103 runs, 101 assertions, 1 failures, 0 errors, 1 skips
You have skipped tests. Run with --verbose for details.
```
Into:
```
...................S.......................................F
This failed
bin/rails test test/models/bunny_test.rb:14
......................
Finished in 0.069910s, 1473.3225 runs/s, 1444.7143 assertions/s.
103 runs, 101 assertions, 1 failures, 0 errors, 1 skips
```
2015-10-07 20:23:26 +00:00
2016-08-06 17:16:09 +00:00
output = run_test_command ( " test/models/post_test.rb " )
2017-07-30 00:02:51 +00:00
assert_match %r{ Finished in.* \ n1 runs, 1 assertions } , output
Hide Minitest's aggregated results if outputting inline.
We'd see the failures and errors reported after the run, which is needless, when we've already
reported them.
Turns:
```
.......................................S....................F
This failed
bin/rails test test/models/bunny_test.rb:14
....
Finished in 0.100886s, 1020.9583 runs/s, 1001.1338 assertions/s.
2) Failure:
BunnyTest#test_something_failing [/Users/kasperhansen/Documents/code/collection_caching_test/test/models/bunny_test.rb:15]:
This failed
103 runs, 101 assertions, 1 failures, 0 errors, 1 skips
You have skipped tests. Run with --verbose for details.
```
Into:
```
...................S.......................................F
This failed
bin/rails test test/models/bunny_test.rb:14
......................
Finished in 0.069910s, 1473.3225 runs/s, 1444.7143 assertions/s.
103 runs, 101 assertions, 1 failures, 0 errors, 1 skips
```
2015-10-07 20:23:26 +00:00
end
2015-09-28 18:27:30 +00:00
def test_fail_fast
2016-08-06 17:16:09 +00:00
create_test_file :models , " post " , pass : false
2015-09-28 18:27:30 +00:00
assert_match ( / Interrupt / ,
2017-09-03 16:55:26 +00:00
capture ( :stderr ) { run_test_command ( " test/models/post_test.rb --fail-fast " , stderr : true ) } )
2015-09-28 18:27:30 +00:00
end
2017-12-20 21:59:41 +00:00
def test_run_in_parallel_with_processes
2018-12-19 16:19:28 +00:00
exercise_parallelization_regardless_of_machine_core_count ( with : :processes )
2018-12-18 18:25:35 +00:00
2017-12-20 21:59:41 +00:00
file_name = create_parallel_processes_test_file
2018-08-08 13:12:59 +00:00
app_file " db/schema.rb " , <<-RUBY
ActiveRecord :: Schema . define ( version : 1 ) do
create_table :users do | t |
t . string :name
end
end
RUBY
2017-12-20 21:59:41 +00:00
output = run_test_command ( file_name )
assert_match %r{ Finished in.* \ n2 runs, 2 assertions } , output
2018-08-08 13:12:59 +00:00
assert_no_match " create_table(:users) " , output
2017-12-20 21:59:41 +00:00
end
Fail parallel tests if workers exit early
Previously, if a test worker exited early, the in-flight test it was
supposed to run wasn't reported as a failure.
If all workers exited immediately, this would be reported as ex.
Finished in 1.708349s, 39.2192 runs/s, 79.0237 assertions/s.
67 runs, 135 assertions, 0 failures, 0 errors, 2 skips
This commit validates that all workers finish running tests by ensuring
that the queue is empty after they exit. This works because we signal
the workers to exit by pushing nil onto the queue, so that there should
be a number of items left in the queue matching potentially missed
tests.
2019-05-30 23:56:29 +00:00
def test_run_in_parallel_with_process_worker_crash
exercise_parallelization_regardless_of_machine_core_count ( with : :processes )
file_name = app_file ( " test/models/parallel_test.rb " , <<-RUBY)
require 'test_helper'
class ParallelTest < ActiveSupport :: TestCase
def test_crash
Kernel . exit 1
end
end
RUBY
output = run_test_command ( file_name )
assert_match %r{ Queue not empty, but all workers have finished. This probably means that a worker crashed and 1 tests were missed. } , output
end
2017-12-20 21:59:41 +00:00
def test_run_in_parallel_with_threads
2018-12-19 16:19:28 +00:00
exercise_parallelization_regardless_of_machine_core_count ( with : :threads )
2017-12-20 21:59:41 +00:00
file_name = create_parallel_threads_test_file
2018-08-08 13:12:59 +00:00
app_file " db/schema.rb " , <<-RUBY
ActiveRecord :: Schema . define ( version : 1 ) do
create_table :users do | t |
t . string :name
end
end
RUBY
2017-12-20 21:59:41 +00:00
output = run_test_command ( file_name )
assert_match %r{ Finished in.* \ n2 runs, 2 assertions } , output
2018-08-08 13:12:59 +00:00
assert_no_match " create_table(:users) " , output
2017-12-20 21:59:41 +00:00
end
2018-11-09 09:25:26 +00:00
def test_run_in_parallel_with_unmarshable_exception
2018-12-19 16:19:28 +00:00
exercise_parallelization_regardless_of_machine_core_count ( with : :processes )
2018-12-18 18:25:35 +00:00
2018-11-09 09:25:26 +00:00
file = app_file " test/fail_test.rb " , <<-RUBY
require " test_helper "
class FailTest < ActiveSupport :: TestCase
class BadError < StandardError
def initialize
super
@proc = - > { }
end
end
test " fail " do
raise BadError
assert true
end
end
RUBY
output = run_test_command ( file )
assert_match " DRb::DRbRemoteError: FailTest::BadError " , output
assert_match " 1 runs, 0 assertions, 0 failures, 1 errors " , output
end
2018-11-16 12:25:14 +00:00
def test_run_in_parallel_with_unknown_object
2018-12-19 16:19:28 +00:00
exercise_parallelization_regardless_of_machine_core_count ( with : :processes )
2018-11-16 12:25:14 +00:00
create_scaffold
2018-12-19 16:19:28 +00:00
2018-11-16 12:25:14 +00:00
app_file " config/environments/test.rb " , <<-RUBY
Rails . application . configure do
config . action_controller . allow_forgery_protection = true
config . action_dispatch . show_exceptions = false
end
RUBY
output = run_test_command ( " -n test_should_create_user " )
assert_match " ActionController::InvalidAuthenticityToken " , output
end
2015-09-06 12:21:18 +00:00
def test_raise_error_when_specified_file_does_not_exist
2017-09-03 16:55:26 +00:00
error = capture ( :stderr ) { run_test_command ( " test/not_exists.rb " , stderr : true ) }
2015-09-06 12:21:18 +00:00
assert_match ( %r{ cannot load such file.+test/not_exists \ .rb } , error )
end
2016-01-12 18:24:40 +00:00
def test_pass_TEST_env_on_rake_test
2016-08-06 17:16:09 +00:00
create_test_file :models , " account "
create_test_file :models , " post " , pass : false
2016-01-23 19:25:02 +00:00
# This specifically verifies TEST for backwards compatibility with rake test
2019-01-22 08:53:47 +00:00
# as `bin/rails test` already supports running tests from a single file more cleanly.
2016-10-29 03:05:58 +00:00
output = Dir . chdir ( app_path ) { ` bin/rake test TEST=test/models/post_test.rb ` }
2016-01-12 18:24:40 +00:00
assert_match " PostTest " , output , " passing TEST= should run selected test "
assert_no_match " AccountTest " , output , " passing TEST= should only run selected test "
2016-08-06 17:16:09 +00:00
assert_match " 1 runs, 1 assertions " , output
2016-01-12 18:24:40 +00:00
end
2016-04-23 09:22:04 +00:00
def test_pass_rake_options
2016-08-06 17:16:09 +00:00
create_test_file :models , " account "
2016-10-29 03:05:58 +00:00
output = Dir . chdir ( app_path ) { ` bin/rake --rakefile Rakefile --trace=stdout test ` }
2016-04-23 09:22:04 +00:00
2016-08-06 17:16:09 +00:00
assert_match " 1 runs, 1 assertions " , output
assert_match " Execute test " , output
2016-04-23 09:22:04 +00:00
end
2016-02-23 16:12:54 +00:00
def test_rails_db_create_all_restores_db_connection
2016-08-06 17:16:09 +00:00
create_test_file :models , " account "
2017-09-03 16:55:26 +00:00
rails " db:create:all " , " db:migrate "
output = Dir . chdir ( app_path ) { ` echo ".tables" | rails dbconsole ` }
2016-02-23 16:12:54 +00:00
assert_match " ar_internal_metadata " , output , " tables should be dumped "
end
def test_rails_db_create_all_restores_db_connection_after_drop
2016-08-06 17:16:09 +00:00
create_test_file :models , " account "
2017-09-03 16:55:26 +00:00
rails " db:create:all " # create all to avoid warnings
rails " db:drop:all " , " db:create:all " , " db:migrate "
output = Dir . chdir ( app_path ) { ` echo ".tables" | rails dbconsole ` }
2016-02-23 16:12:54 +00:00
assert_match " ar_internal_metadata " , output , " tables should be dumped "
end
2016-02-23 00:14:14 +00:00
def test_rake_passes_TESTOPTS_to_minitest
2016-08-06 17:16:09 +00:00
create_test_file :models , " account "
2016-10-29 03:05:58 +00:00
output = Dir . chdir ( app_path ) { ` bin/rake test TESTOPTS=-v ` }
2016-02-23 00:14:14 +00:00
assert_match " AccountTest # test_truth " , output , " passing TEST= should run selected test "
end
2017-12-07 01:33:16 +00:00
def test_running_with_ruby_gets_test_env_by_default
2017-12-08 21:23:31 +00:00
# Subshells inherit `ENV`, so we need to ensure `RAILS_ENV` is set to
# nil before we run the tests in the test app.
re , ENV [ " RAILS_ENV " ] = ENV [ " RAILS_ENV " ] , nil
2017-12-07 01:33:16 +00:00
file = create_test_for_env ( " test " )
results = Dir . chdir ( app_path ) {
` ruby -Ilib:test #{ file } ` . each_line . map { | line | JSON . parse line }
}
assert_equal 1 , results . length
failures = results . first [ " failures " ]
flunk ( failures . first ) unless failures . empty?
2017-12-08 21:23:31 +00:00
ensure
ENV [ " RAILS_ENV " ] = re
2017-12-07 01:33:16 +00:00
end
def test_running_with_ruby_can_set_env_via_cmdline
2017-12-08 21:23:31 +00:00
# Subshells inherit `ENV`, so we need to ensure `RAILS_ENV` is set to
# nil before we run the tests in the test app.
re , ENV [ " RAILS_ENV " ] = ENV [ " RAILS_ENV " ] , nil
2017-12-07 01:33:16 +00:00
file = create_test_for_env ( " development " )
results = Dir . chdir ( app_path ) {
2017-12-08 21:42:01 +00:00
` RAILS_ENV=development ruby -Ilib:test #{ file } ` . each_line . map { | line | JSON . parse line }
2017-12-07 01:33:16 +00:00
}
assert_equal 1 , results . length
failures = results . first [ " failures " ]
flunk ( failures . first ) unless failures . empty?
2017-12-08 21:23:31 +00:00
ensure
ENV [ " RAILS_ENV " ] = re
2017-12-07 01:33:16 +00:00
end
2016-02-23 00:14:14 +00:00
def test_rake_passes_multiple_TESTOPTS_to_minitest
2016-08-06 17:16:09 +00:00
create_test_file :models , " account "
2016-10-29 03:05:58 +00:00
output = Dir . chdir ( app_path ) { ` bin/rake test TESTOPTS='-v --seed=1234' ` }
2016-02-23 00:14:14 +00:00
assert_match " AccountTest # test_truth " , output , " passing TEST= should run selected test "
assert_match " seed=1234 " , output , " passing TEST= should run selected test "
end
2017-01-25 15:58:11 +00:00
def test_rake_runs_multiple_test_tasks
create_test_file :models , " account "
create_test_file :controllers , " accounts_controller "
output = Dir . chdir ( app_path ) { ` bin/rake test:models test:controllers TESTOPTS='-v' ` }
assert_match " AccountTest # test_truth " , output
assert_match " AccountsControllerTest # test_truth " , output
end
def test_rake_db_and_test_tasks_parses_args_correctly
create_test_file :models , " account "
output = Dir . chdir ( app_path ) { ` bin/rake db:migrate test:models TESTOPTS='-v' && echo ".tables" | rails dbconsole ` }
assert_match " AccountTest # test_truth " , output
assert_match " ar_internal_metadata " , output
end
2017-01-23 06:05:46 +00:00
def test_warnings_option
app_file " test/models/warnings_test.rb " , <<-RUBY
require 'test_helper'
def test_warnings
a = 1
end
RUBY
assert_match ( / warning: assigned but unused variable / ,
2017-09-03 16:55:26 +00:00
capture ( :stderr ) { run_test_command ( " test/models/warnings_test.rb -w " , stderr : true ) } )
2017-01-23 06:05:46 +00:00
end
2017-03-01 11:39:29 +00:00
def test_reset_sessions_before_rollback_on_system_tests
app_file " test/system/reset_session_before_rollback_test.rb " , <<-RUBY
require " application_system_test_case "
2018-04-04 01:58:39 +00:00
require " selenium/webdriver "
2017-03-01 11:39:29 +00:00
class ResetSessionBeforeRollbackTest < ApplicationSystemTestCase
def teardown_fixtures
puts " rollback "
super
end
Capybara . singleton_class . prepend ( Module . new do
def reset_sessions!
puts " reset sessions "
super
end
end )
test " dummy " do
end
end
RUBY
run_test_command ( " test/system/reset_session_before_rollback_test.rb " ) . tap do | output |
assert_match " reset sessions \n rollback " , output
assert_match " 1 runs, 0 assertions, 0 failures, 0 errors, 0 skips " , output
end
end
2018-11-08 18:03:04 +00:00
def test_reset_sessions_on_failed_system_test_screenshot
app_file " test/system/reset_sessions_on_failed_system_test_screenshot_test.rb " , << ~ RUBY
require " application_system_test_case "
2019-01-29 14:01:58 +00:00
require " selenium/webdriver "
2018-11-08 18:03:04 +00:00
class ResetSessionsOnFailedSystemTestScreenshotTest < ApplicationSystemTestCase
ActionDispatch :: SystemTestCase . class_eval do
def take_failed_screenshot
raise Capybara :: CapybaraError
end
end
Capybara . instance_eval do
def reset_sessions!
puts " Capybara.reset_sessions! called "
end
end
test " dummy " do
end
end
RUBY
output = run_test_command ( " test/system/reset_sessions_on_failed_system_test_screenshot_test.rb " )
assert_match " Capybara.reset_sessions! called " , output
end
2019-04-21 02:09:50 +00:00
def test_failed_system_test_screenshot_should_be_taken_before_other_teardown
app_file " test/system/failed_system_test_screenshot_should_be_taken_before_other_teardown_test.rb " , << ~ RUBY
require " application_system_test_case "
require " selenium/webdriver "
class FailedSystemTestScreenshotShouldBeTakenBeforeOtherTeardownTest < ApplicationSystemTestCase
ActionDispatch :: SystemTestCase . class_eval do
def take_failed_screenshot
puts " take_failed_screenshot called "
super
end
end
def teardown
puts " test teardown called "
super
end
test " dummy " do
end
end
RUBY
output = run_test_command ( " test/system/failed_system_test_screenshot_should_be_taken_before_other_teardown_test.rb " )
assert_match ( / take_failed_screenshot called \ n.*test teardown called / , output )
end
2017-03-04 13:54:03 +00:00
def test_system_tests_are_not_run_with_the_default_test_command
app_file " test/system/dummy_test.rb " , <<-RUBY
require " application_system_test_case "
class DummyTest < ApplicationSystemTestCase
test " something " do
assert true
end
end
RUBY
run_test_command ( " " ) . tap do | output |
assert_match " 0 runs, 0 assertions, 0 failures, 0 errors, 0 skips " , output
end
end
def test_system_tests_are_not_run_through_rake_test
app_file " test/system/dummy_test.rb " , <<-RUBY
require " application_system_test_case "
class DummyTest < ApplicationSystemTestCase
test " something " do
assert true
end
end
RUBY
output = Dir . chdir ( app_path ) { ` bin/rake test ` }
assert_match " 0 runs, 0 assertions, 0 failures, 0 errors, 0 skips " , output
end
def test_system_tests_are_run_through_rake_test_when_given_in_TEST
app_file " test/system/dummy_test.rb " , <<-RUBY
require " application_system_test_case "
2018-04-04 01:58:39 +00:00
require " selenium/webdriver "
2017-03-04 13:54:03 +00:00
class DummyTest < ApplicationSystemTestCase
test " something " do
assert true
end
end
RUBY
output = Dir . chdir ( app_path ) { ` bin/rake test TEST=test/system/dummy_test.rb ` }
assert_match " 1 runs, 1 assertions, 0 failures, 0 errors, 0 skips " , output
end
2013-01-25 18:44:36 +00:00
private
2017-09-03 16:55:26 +00:00
def run_test_command ( arguments = " test/unit/test_test.rb " , ** opts )
rails " t " , * Shellwords . split ( arguments ) , allow_failure : true , ** opts
2013-01-25 18:44:36 +00:00
end
2013-02-06 06:03:17 +00:00
def create_model_with_fixture
2017-09-03 16:55:26 +00:00
rails " generate " , " model " , " user " , " name:string "
2013-02-06 06:03:17 +00:00
2018-02-17 00:28:30 +00:00
app_file " test/fixtures/users.yml " , << ~ YAML
2013-02-06 06:03:17 +00:00
vampire :
id : 1
name : Koyomi Araragi
crab :
id : 2
name : Senjougahara Hitagi
cat :
id : 3
name : Tsubasa Hanekawa
YAML
2013-03-09 21:03:09 +00:00
run_migration
2013-02-06 06:03:17 +00:00
end
2016-08-06 17:16:09 +00:00
def create_fixture_test ( path = :unit , name = " test " )
2013-02-06 06:03:17 +00:00
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
2015-03-21 12:15:56 +00:00
def create_backtrace_test
2016-08-06 17:16:09 +00:00
app_file " test/unit/backtrace_test.rb " , <<-RUBY
2015-03-21 12:15:56 +00:00
require 'test_helper'
class BacktraceTest < ActiveSupport :: TestCase
def test_backtrace
puts Minitest . backtrace_filter
end
end
RUBY
end
2013-01-25 18:44:36 +00:00
def create_schema
2016-08-06 17:16:09 +00:00
app_file " db/schema.rb " , " "
2013-01-25 18:44:36 +00:00
end
2017-12-07 01:33:16 +00:00
def create_test_for_env ( env )
app_file " test/models/environment_test.rb " , <<-RUBY
require 'test_helper'
class JSONReporter < Minitest :: AbstractReporter
def record ( result )
puts JSON . dump ( klass : result . class . name ,
name : result . name ,
failures : result . failures ,
assertions : result . assertions ,
time : result . time )
end
end
def Minitest . plugin_json_reporter_init ( opts )
Minitest . reporter . reporters . clear
Minitest . reporter . reporters << JSONReporter . new
end
Minitest . extensions << " rails "
Minitest . extensions << " json_reporter "
# Minitest uses RubyGems to find plugins, and since RubyGems
# doesn't know about the Rails installation we're pointing at,
# Minitest won't require the Rails minitest plugin when we run
# these integration tests. So we have to manually require the
# Minitest plugin here.
require 'minitest/rails_plugin'
class EnvironmentTest < ActiveSupport :: TestCase
def test_environment
2019-09-12 17:10:57 +00:00
test_db = ActiveRecord :: Base . configurations [ #{env.dump}][:database]
2019-12-18 03:34:01 +00:00
db_file = ActiveRecord :: Base . connection_db_config . database
2017-12-08 21:23:31 +00:00
assert_match ( test_db , db_file )
2017-12-07 01:33:16 +00:00
assert_equal #{env.dump}, ENV["RAILS_ENV"]
end
end
RUBY
end
2017-12-20 21:59:41 +00:00
def create_test_file ( path = :unit , name = " test " , pass : true , print : true )
2013-01-25 18:44:36 +00:00
app_file " test/ #{ path } / #{ name } _test.rb " , <<-RUBY
require 'test_helper'
class #{name.camelize}Test < ActiveSupport::TestCase
def test_truth
2017-12-20 21:59:41 +00:00
puts " #{ name . camelize } Test " if #{print}
2015-10-07 20:43:29 +00:00
assert #{pass}, 'wups!'
2013-01-25 18:44:36 +00:00
end
end
RUBY
end
2013-03-09 21:03:09 +00:00
2017-12-20 21:59:41 +00:00
def create_parallel_processes_test_file
app_file " test/models/parallel_test.rb " , <<-RUBY
require 'test_helper'
class ParallelTest < ActiveSupport :: TestCase
RD1 , WR1 = IO . pipe
RD2 , WR2 = IO . pipe
test " one " do
WR1 . close
assert_equal " x " , RD1 . read ( 1 ) # blocks until two runs
RD2 . close
WR2 . write " y " # Allow two to run
WR2 . close
end
test " two " do
RD1 . close
WR1 . write " x " # Allow one to run
WR1 . close
WR2 . close
assert_equal " y " , RD2 . read ( 1 ) # blocks until one runs
end
end
RUBY
end
def create_parallel_threads_test_file
app_file " test/models/parallel_test.rb " , <<-RUBY
require 'test_helper'
class ParallelTest < ActiveSupport :: TestCase
Q1 = Queue . new
Q2 = Queue . new
test " one " do
assert_equal " x " , Q1 . pop # blocks until two runs
Q2 << " y "
end
test " two " do
Q1 << " x "
assert_equal " y " , Q2 . pop # blocks until one runs
end
end
RUBY
end
2018-12-19 16:19:28 +00:00
def exercise_parallelization_regardless_of_machine_core_count ( with : )
2018-12-18 18:25:35 +00:00
app_path ( " test/test_helper.rb " ) do | file_name |
file = File . read ( file_name )
2018-12-19 16:19:28 +00:00
file . sub! ( / parallelize \ (([^ \ )]*) \ ) / , " parallelize(workers: 2, with: : #{ with } ) " )
2018-12-18 18:25:35 +00:00
File . write ( file_name , file )
end
end
2015-03-21 12:15:56 +00:00
def create_env_test
2016-08-06 17:16:09 +00:00
app_file " test/unit/env_test.rb " , <<-RUBY
2015-03-21 12:15:56 +00:00
require 'test_helper'
class EnvTest < ActiveSupport :: TestCase
def test_env
puts " Current Environment: \# {Rails.env} "
end
end
RUBY
end
2013-03-09 21:03:09 +00:00
def create_scaffold
2017-09-03 16:55:26 +00:00
rails " generate " , " scaffold " , " user " , " name:string "
assert File . exist? ( " #{ app_path } /app/models/user.rb " )
2013-03-09 21:03:09 +00:00
run_migration
end
2016-01-06 06:01:14 +00:00
def create_controller
2017-09-03 16:55:26 +00:00
rails " generate " , " controller " , " admin/dashboard " , " index "
2016-01-06 06:01:14 +00:00
end
2013-03-09 21:03:09 +00:00
def run_migration
2017-09-03 16:55:26 +00:00
rails " db:migrate "
2013-03-09 21:03:09 +00:00
end
2013-01-25 18:44:36 +00:00
end
end