2013-01-25 13:44:36 -05:00
require 'isolation/abstract_unit'
2013-02-06 01:03:17 -05:00
require 'active_support/core_ext/string/strip'
2015-03-21 08:15:56 -04:00
require 'env_helpers'
2013-01-25 13:44:36 -05:00
module ApplicationTests
class TestRunnerTest < ActiveSupport :: TestCase
2015-03-21 08:15:56 -04:00
include ActiveSupport :: Testing :: Isolation , EnvHelpers
2013-01-25 13:44:36 -05:00
def setup
build_app
create_schema
end
def teardown
teardown_app
end
def test_run_single_file
create_test_file :models , 'foo'
2013-03-29 22:22:02 -04:00
create_test_file :models , 'bar'
2013-05-06 20:38:45 -04:00
assert_match " 1 runs, 1 assertions, 0 failures " , run_test_command ( " test/models/foo_test.rb " )
2013-01-25 13:44:36 -05:00
end
def test_run_multiple_files
create_test_file :models , 'foo'
create_test_file :models , 'bar'
2013-05-06 20:38:45 -04: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 13:44:36 -05:00
end
def test_run_file_with_syntax_error
app_file 'test/models/error_test.rb' , <<-RUBY
require 'test_helper'
def ; end
RUBY
2015-01-30 06:04:57 -05:00
error = capture ( :stderr ) { run_test_command ( 'test/models/error_test.rb' ) }
assert_match " syntax error " , error
2013-01-25 13:44:36 -05:00
end
def test_run_models
create_test_file :models , 'foo'
create_test_file :models , 'bar'
create_test_file :controllers , 'foobar_controller'
2015-01-30 08:54:41 -05:00
run_test_command ( " test/models " ) . tap do | output |
2013-03-11 14:48:23 -04:00
assert_match " FooTest " , output
assert_match " BarTest " , output
2013-05-06 20:38:45 -04:00
assert_match " 2 runs, 2 assertions, 0 failures " , output
2013-01-25 13:44:36 -05:00
end
end
def test_run_helpers
create_test_file :helpers , 'foo_helper'
create_test_file :helpers , 'bar_helper'
create_test_file :controllers , 'foobar_controller'
2015-01-30 08:54:41 -05:00
run_test_command ( " test/helpers " ) . tap do | output |
2013-03-11 14:48:23 -04:00
assert_match " FooHelperTest " , output
assert_match " BarHelperTest " , output
2013-05-06 20:38:45 -04:00
assert_match " 2 runs, 2 assertions, 0 failures " , output
2013-01-25 13:44:36 -05:00
end
end
def test_run_units
2015-01-30 08:54:41 -05:00
skip " we no longer have the concept of unit tests. Just different directories... "
2013-01-25 13:44:36 -05:00
create_test_file :models , 'foo'
create_test_file :helpers , 'bar_helper'
create_test_file :unit , 'baz_unit'
create_test_file :controllers , 'foobar_controller'
2013-03-29 22:22:02 -04:00
run_test_units_command . tap do | output |
2013-03-11 14:48:23 -04:00
assert_match " FooTest " , output
assert_match " BarHelperTest " , output
assert_match " BazUnitTest " , output
2013-05-06 20:38:45 -04:00
assert_match " 3 runs, 3 assertions, 0 failures " , output
2013-01-25 13:44:36 -05:00
end
end
def test_run_controllers
create_test_file :controllers , 'foo_controller'
create_test_file :controllers , 'bar_controller'
create_test_file :models , 'foo'
2015-01-30 08:54:41 -05:00
run_test_command ( " test/controllers " ) . tap do | output |
2013-03-11 14:48:23 -04:00
assert_match " FooControllerTest " , output
assert_match " BarControllerTest " , output
2013-05-06 20:38:45 -04:00
assert_match " 2 runs, 2 assertions, 0 failures " , output
2013-01-25 13:44:36 -05:00
end
end
def test_run_mailers
create_test_file :mailers , 'foo_mailer'
create_test_file :mailers , 'bar_mailer'
create_test_file :models , 'foo'
2015-01-30 08:54:41 -05:00
run_test_command ( " test/mailers " ) . tap do | output |
2013-03-11 14:48:23 -04:00
assert_match " FooMailerTest " , output
assert_match " BarMailerTest " , output
2013-05-06 20:38:45 -04:00
assert_match " 2 runs, 2 assertions, 0 failures " , output
2013-01-25 13:44:36 -05:00
end
end
2014-09-14 19:23:14 -04:00
def test_run_jobs
create_test_file :jobs , 'foo_job'
create_test_file :jobs , 'bar_job'
create_test_file :models , 'foo'
2015-01-30 08:54:41 -05:00
run_test_command ( " test/jobs " ) . tap do | output |
2014-09-14 19:23:14 -04:00
assert_match " FooJobTest " , output
assert_match " BarJobTest " , output
assert_match " 2 runs, 2 assertions, 0 failures " , output
end
end
2013-01-25 13:44:36 -05:00
def test_run_functionals
2015-01-30 08:54:41 -05:00
skip " we no longer have the concept of functional tests. Just different directories... "
2013-01-25 13:44:36 -05:00
create_test_file :mailers , 'foo_mailer'
create_test_file :controllers , 'bar_controller'
create_test_file :functional , 'baz_functional'
create_test_file :models , 'foo'
2013-03-29 22:22:02 -04:00
run_test_functionals_command . tap do | output |
2013-03-11 14:48:23 -04:00
assert_match " FooMailerTest " , output
assert_match " BarControllerTest " , output
assert_match " BazFunctionalTest " , output
2013-05-06 20:38:45 -04:00
assert_match " 3 runs, 3 assertions, 0 failures " , output
2013-01-25 13:44:36 -05:00
end
end
def test_run_integration
create_test_file :integration , 'foo_integration'
create_test_file :models , 'foo'
2015-01-30 08:54:41 -05:00
run_test_command ( " test/integration " ) . tap do | output |
2013-03-11 14:48:23 -04:00
assert_match " FooIntegration " , output
2013-05-06 20:38:45 -04:00
assert_match " 1 runs, 1 assertions, 0 failures " , output
2013-01-25 13:44:36 -05:00
end
end
2013-02-07 18:53:11 -05:00
def test_run_all_suites
2014-09-14 19:23:14 -04:00
suites = [ :models , :helpers , :unit , :controllers , :mailers , :functional , :integration , :jobs ]
2013-02-07 18:53:11 -05:00
suites . each { | suite | create_test_file suite , " foo_ #{ suite } " }
2013-01-25 13:44:36 -05:00
run_test_command ( '' ) . tap do | output |
2013-03-11 14:48:23 -04:00
suites . each { | suite | assert_match " Foo #{ suite . to_s . camelize } Test " , output }
2014-09-14 19:23:14 -04:00
assert_match " 8 runs, 8 assertions, 0 failures " , output
2013-01-25 13:44:36 -05:00
end
end
2013-02-01 17:21:09 -05:00
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
2015-01-30 08:54:41 -05:00
run_test_command ( '-n test_rikka test/unit/chu_2_koi_test.rb' ) . tap do | output |
2013-04-05 19:38:37 -04:00
assert_match " Rikka " , output
assert_no_match " Sanae " , output
end
end
def test_run_matched_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
2015-03-21 08:15:56 -04:00
run_test_command ( '-n /rikka/ test/unit/chu_2_koi_test.rb' ) . tap do | output |
2013-03-11 14:48:23 -04:00
assert_match " Rikka " , output
assert_no_match " Sanae " , output
2013-02-01 17:21:09 -05:00
end
end
2013-02-06 01:03:17 -05:00
def test_load_fixtures_when_running_test_suites
create_model_with_fixture
2015-01-30 08:54:41 -05:00
suites = [ :models , :helpers , :controllers , :mailers , :integration ]
2013-02-06 01:03:17 -05:00
2013-02-07 18:53:11 -05:00
suites . each do | suite , directory |
directory || = suite
2013-02-06 01:03:17 -05:00
create_fixture_test directory
2015-01-30 08:54:41 -05:00
assert_match " 3 users " , run_test_command ( " test/ #{ suite } " )
2013-02-06 01:03:17 -05:00
Dir . chdir ( app_path ) { FileUtils . rm_f " test/ #{ directory } " }
end
end
2013-04-05 20:12:23 -04:00
def test_run_with_model
2015-01-30 08:54:41 -05:00
skip " These feel a bit odd. Not sure we should keep supporting them. "
2013-04-05 20:12:23 -04:00
create_model_with_fixture
create_fixture_test 'models' , 'user'
assert_match " 3 users " , run_task ( [ " test models/user " ] )
assert_match " 3 users " , run_task ( [ " test app/models/user.rb " ] )
end
2013-03-09 16:03:09 -05:00
def test_run_different_environment_using_env_var
2015-01-30 08:54:41 -05:00
skip " no longer possible. Running tests in a different environment should be explicit "
2013-03-09 16:03:09 -05:00
app_file 'test/unit/env_test.rb' , <<-RUBY
require 'test_helper'
class EnvTest < ActiveSupport :: TestCase
def test_env
puts Rails . env
end
end
RUBY
2013-03-11 14:28:43 -04:00
ENV [ 'RAILS_ENV' ] = 'development'
2013-03-11 14:48:23 -04:00
assert_match " development " , run_test_command ( 'test/unit/env_test.rb' )
2013-03-09 16:03:09 -05:00
end
2015-03-21 08:15:56 -04:00
def test_run_in_test_environment_by_default
create_env_test
assert_match " Current Environment: test " , run_test_command ( 'test/unit/env_test.rb' )
end
2015-01-30 08:54:41 -05:00
def test_run_different_environment
2015-03-21 08:15:56 -04: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
assert_match " 0 failures, 0 errors, 0 skips " , run_test_command ( '' )
end
2016-01-06 01:01:14 -05:00
def test_generated_controller_works_with_rails_test
create_controller
assert_match " 0 failures, 0 errors, 0 skips " , run_test_command ( '' )
end
2015-03-21 08:15:56 -04:00
def test_run_multiple_folders
create_test_file :models , 'account'
create_test_file :controllers , 'accounts_controller'
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
end
end
def test_run_with_ruby_command
app_file 'test/models/post_test.rb' , <<-RUBY
2013-03-09 16:03:09 -05:00
require 'test_helper'
2015-03-21 08:15:56 -04:00
class PostTest < ActiveSupport :: TestCase
test 'declarative syntax works' do
puts 'PostTest'
assert true
2013-03-09 16:03:09 -05:00
end
end
RUBY
2015-03-21 08:15:56 -04:00
Dir . chdir ( app_path ) do
` ruby -Itest test/models/post_test.rb ` . tap do | output |
assert_match 'PostTest' , output
assert_no_match 'is already defined in' , output
end
end
2013-03-09 16:03:09 -05:00
end
2015-03-21 08:15:56 -04:00
def test_mix_files_and_line_filters
create_test_file :models , 'account'
app_file 'test/models/post_test.rb' , <<-RUBY
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
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
end
end
2015-12-29 12:27:27 -05:00
def test_more_than_one_line_filter
app_file 'test/models/post_test.rb' , <<-RUBY
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 " test line filter does not run this " do
assert true
end
end
RUBY
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
end
end
def test_more_than_one_line_filter_with_multiple_files
app_file 'test/models/account_test.rb' , <<-RUBY
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
app_file 'test/models/post_test.rb' , <<-RUBY
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-02-03 16:10:02 -05:00
run_test_command ( 'test/models/account_test.rb:4:9 test/models/post_test.rb:4:9' ) . tap do | output |
2015-12-29 12:27:27 -05:00
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
end
end
2015-03-21 08:15:56 -04:00
def test_multiple_line_filters
create_test_file :models , 'account'
create_test_file :models , 'post'
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
end
end
2016-02-03 15:58:55 -05:00
def test_line_filters_trigger_only_one_runnable
app_file 'test/models/post_test.rb' , <<-RUBY
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.
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
end
end
2016-02-04 17:06:33 -05:00
def test_line_filter_with_minitest_string_filter
app_file 'test/models/post_test.rb' , <<-RUBY
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
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
end
end
2015-03-21 08:15:56 -04:00
def test_shows_filtered_backtrace_by_default
create_backtrace_test
assert_match 'Rails::BacktraceCleaner' , run_test_command ( 'test/unit/backtrace_test.rb' )
end
def test_backtrace_option
create_backtrace_test
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' )
end
def test_show_full_backtrace_using_backtrace_environment_variable
create_backtrace_test
switch_env 'BACKTRACE' , 'true' do
assert_match 'Minitest::BacktraceFilter' , run_test_command ( 'test/unit/backtrace_test.rb' )
end
end
def test_run_app_without_rails_loaded
# Simulate a real Rails app boot.
app_file 'config/boot.rb' , <<-RUBY
ENV [ 'BUNDLE_GEMFILE' ] || = File . expand_path ( '../../Gemfile' , __FILE__ )
require 'bundler/setup' # Set up gems listed in the Gemfile.
RUBY
assert_match '0 runs, 0 assertions' , run_test_command ( '' )
2013-03-09 16:03:09 -05:00
end
2015-08-30 06:33:37 -04:00
def test_output_inline_by_default
2015-10-07 16:43:29 -04:00
create_test_file :models , 'post' , pass : false
2015-08-30 06:33:37 -04:00
output = run_test_command ( 'test/models/post_test.rb' )
2016-02-15 09:09:00 -05:00
expect = %r{ Running: \ n \ nPostTest \ nF \ n \ nFailure: \ nPostTest # test_truth \ [[^ \ ]]+test/models/post_test.rb:6 \ ]: \ nwups! \ n \ nbin/rails test test/models/post_test.rb:4 \ n \ n \ n \ n }
2015-12-19 06:04:39 -05:00
assert_match expect , output
2015-08-30 06:33:37 -04:00
end
2015-10-07 16:43:29 -04:00
def test_only_inline_failure_output
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 16:23:26 -04:00
output = run_test_command ( 'test/models/post_test.rb' )
assert_match %r{ Finished in.* \ n \ n1 runs, 1 assertions } , output
end
2015-09-28 14:27:30 -04:00
def test_fail_fast
2015-10-07 16:43:29 -04:00
create_test_file :models , 'post' , pass : false
2015-09-28 14:27:30 -04:00
assert_match ( / Interrupt / ,
capture ( :stderr ) { run_test_command ( 'test/models/post_test.rb --fail-fast' ) } )
end
2015-09-06 08:21:18 -04:00
def test_raise_error_when_specified_file_does_not_exist
error = capture ( :stderr ) { run_test_command ( 'test/not_exists.rb' ) }
assert_match ( %r{ cannot load such file.+test/not_exists \ .rb } , error )
end
2016-01-12 13:24:40 -05:00
def test_pass_TEST_env_on_rake_test
create_test_file :models , 'account'
create_test_file :models , 'post' , pass : false
2016-01-23 14:25:02 -05:00
# This specifically verifies TEST for backwards compatibility with rake test
# as bin/rails test already supports running tests from a single file more cleanly.
2016-01-12 13:24:40 -05:00
output = Dir . chdir ( app_path ) { ` bin/rake test TEST=test/models/post_test.rb ` }
assert_match " PostTest " , output , " passing TEST= should run selected test "
assert_no_match " AccountTest " , output , " passing TEST= should only run selected test "
assert_match '1 runs, 1 assertions' , output
end
2016-02-23 11:12:54 -05:00
def test_rails_db_create_all_restores_db_connection
create_test_file :models , 'account'
output = Dir . chdir ( app_path ) { ` bin/rails db:create:all db:migrate && echo ".tables" | rails dbconsole ` }
assert_match " ar_internal_metadata " , output , " tables should be dumped "
end
def test_rails_db_create_all_restores_db_connection_after_drop
create_test_file :models , 'account'
Dir . chdir ( app_path ) { ` bin/rails db:create:all ` } # create all to avoid warnings
output = Dir . chdir ( app_path ) { ` bin/rails db:drop:all db:create:all db:migrate && echo ".tables" | rails dbconsole ` }
assert_match " ar_internal_metadata " , output , " tables should be dumped "
end
2016-02-22 19:14:14 -05:00
def test_rake_passes_TESTOPTS_to_minitest
create_test_file :models , 'account'
output = Dir . chdir ( app_path ) { ` bin/rake test TESTOPTS=-v ` }
assert_match " AccountTest # test_truth " , output , " passing TEST= should run selected test "
end
def test_rake_passes_multiple_TESTOPTS_to_minitest
create_test_file :models , 'account'
output = Dir . chdir ( app_path ) { ` bin/rake test TESTOPTS='-v --seed=1234' ` }
assert_match " AccountTest # test_truth " , output , " passing TEST= should run selected test "
assert_match " seed=1234 " , output , " passing TEST= should run selected test "
end
2013-01-25 13:44:36 -05:00
private
def run_test_command ( arguments = 'test/unit/test_test.rb' )
2015-01-30 08:54:41 -05:00
Dir . chdir ( app_path ) { ` bin/rails t #{ arguments } ` }
2013-01-25 13:44:36 -05:00
end
2013-02-06 01:03:17 -05:00
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
2013-03-09 16:03:09 -05:00
run_migration
2013-02-06 01:03:17 -05:00
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
2015-03-21 08:15:56 -04:00
def create_backtrace_test
app_file 'test/unit/backtrace_test.rb' , <<-RUBY
require 'test_helper'
class BacktraceTest < ActiveSupport :: TestCase
def test_backtrace
puts Minitest . backtrace_filter
end
end
RUBY
end
2013-01-25 13:44:36 -05:00
def create_schema
app_file 'db/schema.rb' , ''
end
2015-10-07 16:43:29 -04:00
def create_test_file ( path = :unit , name = 'test' , pass : true )
2013-01-25 13:44:36 -05:00
app_file " test/ #{ path } / #{ name } _test.rb " , <<-RUBY
require 'test_helper'
class #{name.camelize}Test < ActiveSupport::TestCase
def test_truth
puts " #{ name . camelize } Test "
2015-10-07 16:43:29 -04:00
assert #{pass}, 'wups!'
2013-01-25 13:44:36 -05:00
end
end
RUBY
end
2013-03-09 16:03:09 -05:00
2015-03-21 08:15:56 -04:00
def create_env_test
app_file 'test/unit/env_test.rb' , <<-RUBY
require 'test_helper'
class EnvTest < ActiveSupport :: TestCase
def test_env
puts " Current Environment: \# {Rails.env} "
end
end
RUBY
end
2013-03-09 16:03:09 -05:00
def create_scaffold
script 'generate scaffold user name:string'
Dir . chdir ( app_path ) { File . exist? ( 'app/models/user.rb' ) }
run_migration
end
2016-01-06 01:01:14 -05:00
def create_controller
script 'generate controller admin/dashboard index'
end
2013-03-09 16:03:09 -05:00
def run_migration
2016-01-23 14:25:02 -05:00
Dir . chdir ( app_path ) { ` bin/rails db:migrate ` }
2013-03-09 16:03:09 -05:00
end
2013-01-25 13:44:36 -05:00
end
end