2017-08-14 13:08:09 -04:00
# frozen_string_literal: true
2016-08-06 13:16:09 -04:00
require " abstract_unit "
require " rails/test_unit/reporter "
require " minitest/mock "
2015-01-24 05:41:33 -05:00
class TestUnitReporterTest < ActiveSupport :: TestCase
class ExampleTest < Minitest :: Test
def woot ; end
end
setup do
@output = StringIO . new
2015-10-07 16:47:05 -04:00
@reporter = Rails :: TestUnitReporter . new @output , output_inline : true
2015-01-24 05:41:33 -05:00
end
test " prints rerun snippet to run a single failed test " do
2015-02-03 21:54:50 -05:00
@reporter . record ( failed_test )
2015-01-24 05:41:33 -05:00
@reporter . report
2018-06-29 11:51:57 -04:00
assert_match %r{ ^rails test .*test/test_unit/reporter_test \ .rb: \ d+$ } , @output . string
2015-01-24 05:41:33 -05:00
assert_rerun_snippet_count 1
end
test " prints rerun snippet for every failed test " do
2015-02-03 21:54:50 -05:00
@reporter . record ( failed_test )
@reporter . record ( failed_test )
@reporter . record ( failed_test )
2015-01-24 05:41:33 -05:00
@reporter . report
assert_rerun_snippet_count 3
end
test " does not print snippet for successful and skipped tests " do
2015-02-03 21:54:50 -05:00
@reporter . record ( passing_test )
@reporter . record ( skipped_test )
2015-01-24 05:41:33 -05:00
@reporter . report
2016-08-06 13:16:09 -04:00
assert_no_match " Failed tests: " , @output . string
2015-01-24 05:41:33 -05:00
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
2015-02-03 21:54:50 -05:00
verbose . record ( skipped_test )
2015-01-24 05:41:33 -05:00
verbose . report
assert_rerun_snippet_count 1
end
2015-06-13 05:56:32 -04:00
test " allows to customize the executable in the rerun snippet " do
original_executable = Rails :: TestUnitReporter . executable
begin
Rails :: TestUnitReporter . executable = " bin/test "
@reporter . record ( failed_test )
@reporter . report
2017-05-06 15:08:58 -04:00
assert_match %r{ ^bin/test .*test/test_unit/reporter_test \ .rb: \ d+$ } , @output . string
2015-06-13 05:56:32 -04:00
ensure
Rails :: TestUnitReporter . executable = original_executable
end
end
2015-08-30 06:33:37 -04:00
test " outputs failures inline " do
@reporter . record ( failed_test )
@reporter . report
2018-06-29 11:51:57 -04:00
expect = %r{ \ AF \ n \ nFailure: \ nTestUnitReporterTest::ExampleTest # woot \ [[^ \ ]]+ \ ]: \ nboo \ n \ nrails test test/test_unit/reporter_test \ .rb: \ d+ \ n \ n \ z }
2015-12-19 06:04:39 -05:00
assert_match expect , @output . string
2015-08-30 06:33:37 -04:00
end
test " outputs errors inline " do
@reporter . record ( errored_test )
@reporter . report
2018-06-29 11:51:57 -04:00
expect = %r{ \ AE \ n \ nError: \ nTestUnitReporterTest::ExampleTest # woot: \ nArgumentError: wups \ n \ n \ nrails test .*test/test_unit/reporter_test \ .rb: \ d+ \ n \ n \ z }
2015-12-19 06:04:39 -05:00
assert_match expect , @output . string
2015-08-30 06:33:37 -04:00
end
test " outputs skipped tests inline if verbose " do
2015-10-07 16:47:05 -04:00
verbose = Rails :: TestUnitReporter . new @output , verbose : true , output_inline : true
2015-08-30 06:33:37 -04:00
verbose . record ( skipped_test )
verbose . report
2018-06-29 11:51:57 -04:00
expect = %r{ \ ATestUnitReporterTest::ExampleTest # woot = 10 \ .00 s = S \ n \ n \ nSkipped: \ nTestUnitReporterTest::ExampleTest # woot \ [[^ \ ]]+ \ ]: \ nskipchurches, misstemples \ n \ nrails test test/test_unit/reporter_test \ .rb: \ d+ \ n \ n \ z }
2015-12-19 06:04:39 -05:00
assert_match expect , @output . string
2015-08-30 06:33:37 -04:00
end
test " does not output rerun snippets after run " do
@reporter . record ( failed_test )
@reporter . report
2016-08-06 13:16:09 -04:00
assert_no_match " Failed tests: " , @output . string
2015-08-30 06:33:37 -04:00
end
2015-09-28 14:27:30 -04:00
test " fail fast interrupts run on failure " do
fail_fast = Rails :: TestUnitReporter . new @output , fail_fast : true
interrupt_raised = false
# Minitest passes through Interrupt, catch it manually.
begin
fail_fast . record ( failed_test )
rescue Interrupt
interrupt_raised = true
ensure
2016-08-06 13:16:09 -04:00
assert interrupt_raised , " Expected Interrupt to be raised. "
2015-09-28 14:27:30 -04:00
end
end
2016-02-15 03:01:29 -05:00
test " fail fast interrupts run on error " do
2015-09-28 14:27:30 -04:00
fail_fast = Rails :: TestUnitReporter . new @output , fail_fast : true
2016-02-15 03:01:29 -05:00
interrupt_raised = false
2015-09-28 14:27:30 -04:00
2016-02-15 03:01:29 -05:00
# Minitest passes through Interrupt, catch it manually.
begin
fail_fast . record ( errored_test )
rescue Interrupt
interrupt_raised = true
ensure
2016-08-06 13:16:09 -04:00
assert interrupt_raised , " Expected Interrupt to be raised. "
2016-02-15 03:01:29 -05:00
end
end
test " fail fast does not interrupt run skips " do
fail_fast = Rails :: TestUnitReporter . new @output , fail_fast : true
2015-09-28 14:27:30 -04:00
fail_fast . record ( skipped_test )
2016-08-06 13:16:09 -04:00
assert_no_match " Failed tests: " , @output . string
2015-09-28 14:27:30 -04:00
end
2015-12-21 19:13:00 -05:00
test " outputs colored passing results " do
@output . stub ( :tty? , true ) do
colored = Rails :: TestUnitReporter . new @output , color : true , output_inline : true
colored . record ( passing_test )
expect = %r{ \ e \ [32m \ . \ e \ [0m }
assert_match expect , @output . string
end
end
test " outputs colored skipped results " do
@output . stub ( :tty? , true ) do
colored = Rails :: TestUnitReporter . new @output , color : true , output_inline : true
colored . record ( skipped_test )
expect = %r{ \ e \ [33mS \ e \ [0m }
assert_match expect , @output . string
end
end
test " outputs colored failed results " do
@output . stub ( :tty? , true ) do
colored = Rails :: TestUnitReporter . new @output , color : true , output_inline : true
colored . record ( errored_test )
2017-06-25 13:08:26 -04:00
expected = %r{ \ e \ [31mE \ e \ [0m \ n \ n \ e \ [31mError: \ nTestUnitReporterTest::ExampleTest # woot: \ nArgumentError: wups \ n \ n \ e \ [0m }
2015-12-21 19:13:00 -05:00
assert_match expected , @output . string
end
end
2015-01-24 05:41:33 -05:00
private
2016-08-06 13:55:02 -04:00
def assert_rerun_snippet_count ( snippet_count )
2018-06-29 11:51:57 -04:00
assert_equal snippet_count , @output . string . scan ( %r{ ^rails test } ) . size
2016-08-06 13:55:02 -04:00
end
2015-01-24 05:41:33 -05:00
2016-08-06 13:55:02 -04:00
def failed_test
2018-01-02 21:19:22 -05:00
ft = Minitest :: Result . from ( ExampleTest . new ( :woot ) )
2016-08-06 13:55:02 -04:00
ft . failures << begin
raise Minitest :: Assertion , " boo "
rescue Minitest :: Assertion = > e
e
end
ft
end
2015-01-24 05:41:33 -05:00
2016-08-06 13:55:02 -04:00
def errored_test
2017-06-25 13:08:26 -04:00
error = ArgumentError . new ( " wups " )
error . set_backtrace ( [ " some_test.rb:4 " ] )
2018-01-02 21:19:22 -05:00
et = Minitest :: Result . from ( ExampleTest . new ( :woot ) )
2017-06-25 13:08:26 -04:00
et . failures << Minitest :: UnexpectedError . new ( error )
2016-08-06 13:55:02 -04:00
et
end
2015-08-30 06:33:37 -04:00
2016-08-06 13:55:02 -04:00
def passing_test
2018-01-02 21:19:22 -05:00
Minitest :: Result . from ( ExampleTest . new ( :woot ) )
2016-08-06 13:55:02 -04:00
end
2015-01-24 05:41:33 -05:00
2016-08-06 13:55:02 -04:00
def skipped_test
2018-01-02 21:19:22 -05:00
st = Minitest :: Result . from ( ExampleTest . new ( :woot ) )
2016-08-06 13:55:02 -04:00
st . failures << begin
raise Minitest :: Skip , " skipchurches, misstemples "
rescue Minitest :: Assertion = > e
e
end
st . time = 10
st
end
2015-01-24 05:41:33 -05:00
end