diff options
author | Yves Senn <yves.senn@gmail.com> | 2015-03-19 14:27:01 +0100 |
---|---|---|
committer | Yves Senn <yves.senn@gmail.com> | 2015-03-19 14:27:01 +0100 |
commit | 9959e9525b9bd3324288c87f1c791cbb488c36c8 (patch) | |
tree | 02d13ea832fbd79f451bb9d24de0ba09e30e8894 /railties/test/test_unit | |
parent | bee9434cdf4f56dc51027a8890cf04f506544735 (diff) | |
parent | d9bbafb2e79bbaee93648b7ee5138a0dbd7e8277 (diff) | |
download | rails-9959e9525b9bd3324288c87f1c791cbb488c36c8.tar.gz rails-9959e9525b9bd3324288c87f1c791cbb488c36c8.tar.bz2 rails-9959e9525b9bd3324288c87f1c791cbb488c36c8.zip |
Merge pull request #19216 from senny/bin_test_runner
`bin/rails test` runner (rerun snippets, run tests by line, option documentation)
Diffstat (limited to 'railties/test/test_unit')
-rw-r--r-- | railties/test/test_unit/reporter_test.rb | 74 | ||||
-rw-r--r-- | railties/test/test_unit/runner_test.rb | 110 |
2 files changed, 184 insertions, 0 deletions
diff --git a/railties/test/test_unit/reporter_test.rb b/railties/test/test_unit/reporter_test.rb new file mode 100644 index 0000000000..77883612f5 --- /dev/null +++ b/railties/test/test_unit/reporter_test.rb @@ -0,0 +1,74 @@ +require 'abstract_unit' +require 'rails/test_unit/reporter' + +class TestUnitReporterTest < ActiveSupport::TestCase + class ExampleTest < Minitest::Test + def woot; end + end + + setup do + @output = StringIO.new + @reporter = Rails::TestUnitReporter.new @output + end + + test "prints rerun snippet to run a single failed test" do + @reporter.record(failed_test) + @reporter.report + + assert_match %r{^bin/rails test .*test/test_unit/reporter_test.rb:6$}, @output.string + assert_rerun_snippet_count 1 + end + + test "prints rerun snippet for every failed test" do + @reporter.record(failed_test) + @reporter.record(failed_test) + @reporter.record(failed_test) + @reporter.report + + assert_rerun_snippet_count 3 + end + + test "does not print snippet for successful and skipped tests" do + @reporter.record(passing_test) + @reporter.record(skipped_test) + @reporter.report + assert_rerun_snippet_count 0 + end + + test "prints rerun snippet for skipped tests if run in verbose mode" do + verbose = Rails::TestUnitReporter.new @output, verbose: true + verbose.record(skipped_test) + verbose.report + + assert_rerun_snippet_count 1 + end + + private + def assert_rerun_snippet_count(snippet_count) + assert_equal snippet_count, @output.string.scan(%r{^bin/rails test }).size + end + + def failed_test + ft = ExampleTest.new(:woot) + ft.failures << begin + raise Minitest::Assertion, "boo" + rescue Minitest::Assertion => e + e + end + ft + end + + def passing_test + ExampleTest.new(:woot) + end + + def skipped_test + st = ExampleTest.new(:woot) + st.failures << begin + raise Minitest::Skip + rescue Minitest::Assertion => e + e + end + st + end +end diff --git a/railties/test/test_unit/runner_test.rb b/railties/test/test_unit/runner_test.rb new file mode 100644 index 0000000000..c040c71635 --- /dev/null +++ b/railties/test/test_unit/runner_test.rb @@ -0,0 +1,110 @@ +require 'abstract_unit' +require 'env_helpers' +require 'rails/test_unit/runner' + +class TestUnitTestRunnerTest < ActiveSupport::TestCase + include EnvHelpers + + setup do + @options = Rails::TestRunner::Options + end + + test "shows the filtered backtrace by default" do + options = @options.parse([]) + assert_not options[:backtrace] + end + + test "has --backtrace (-b) option to show the full backtrace" do + options = @options.parse(["-b"]) + assert options[:backtrace] + + options = @options.parse(["--backtrace"]) + assert options[:backtrace] + end + + test "show full backtrace using BACKTRACE environment variable" do + switch_env "BACKTRACE", "true" do + options = @options.parse([]) + assert options[:backtrace] + end + end + + test "tests run in the test environment by default" do + options = @options.parse([]) + assert_equal "test", options[:environment] + end + + test "can run in a specific environment" do + options = @options.parse(["-e development"]) + assert_equal "development", options[:environment] + end + + test "parse the filename and line" do + file = "test/test_unit/runner_test.rb" + absolute_file = __FILE__ + options = @options.parse(["#{file}:20"]) + assert_equal absolute_file, options[:filename] + assert_equal 20, options[:line] + + options = @options.parse(["#{file}:"]) + assert_equal [absolute_file], options[:patterns] + assert_nil options[:line] + + options = @options.parse([file]) + assert_equal [absolute_file], options[:patterns] + assert_nil options[:line] + end + + test "find_method on same file" do + options = @options.parse(["#{__FILE__}:#{__LINE__}"]) + runner = Rails::TestRunner.new(options) + assert_equal "test_find_method_on_same_file", runner.find_method + end + + test "find_method on a different file" do + options = @options.parse(["foobar.rb:#{__LINE__}"]) + runner = Rails::TestRunner.new(options) + assert_nil runner.find_method + end + + test "run all tests in a directory" do + options = @options.parse([__dir__]) + + assert_equal ["#{__dir__}/**/*_test.rb"], options[:patterns] + assert_nil options[:filename] + assert_nil options[:line] + end + + test "run multiple folders" do + application_dir = File.expand_path("#{__dir__}/../application") + + options = @options.parse([__dir__, application_dir]) + + assert_equal ["#{__dir__}/**/*_test.rb", "#{application_dir}/**/*_test.rb"], options[:patterns] + assert_nil options[:filename] + assert_nil options[:line] + + runner = Rails::TestRunner.new(options) + assert runner.test_files.size > 0 + end + + test "run multiple files and run one file by line" do + line = __LINE__ + options = @options.parse([__dir__, "#{__FILE__}:#{line}"]) + + assert_equal ["#{__dir__}/**/*_test.rb"], options[:patterns] + assert_equal __FILE__, options[:filename] + assert_equal line, options[:line] + + runner = Rails::TestRunner.new(options) + assert_equal [__FILE__], runner.test_files, 'Only returns the file that running by line' + end + + test "running multiple files passing line number" do + line = __LINE__ + options = @options.parse(["foobar.rb:8", "#{__FILE__}:#{line}"]) + + assert_equal __FILE__, options[:filename], 'Returns the last file' + assert_equal line, options[:line] + end +end |