aboutsummaryrefslogtreecommitdiffstats
path: root/railties/test/test_unit/reporter_test.rb
blob: fa6bb71c64ed28a89a547e4c34aef90114f15a8d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
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, output_inline: true
  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_no_match 'Failed tests:', @output.string
    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

  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

      assert_match %r{^bin/test .*test/test_unit/reporter_test.rb:6$}, @output.string
    ensure
      Rails::TestUnitReporter.executable = original_executable
    end
  end

  test "outputs failures inline" do
    @reporter.record(failed_test)
    @reporter.report

    assert_match %r{\A\n\nboo\n\nbin/rails test .*test/test_unit/reporter_test.rb:6\n\n\z}, @output.string
  end

  test "outputs errors inline" do
    @reporter.record(errored_test)
    @reporter.report

    assert_match %r{\A\n\nArgumentError: wups\n    No backtrace\n\nbin/rails test .*test/test_unit/reporter_test.rb:6\n\n\z}, @output.string
  end

  test "outputs skipped tests inline if verbose" do
    verbose = Rails::TestUnitReporter.new @output, verbose: true, output_inline: true
    verbose.record(skipped_test)
    verbose.report

    assert_match %r{\A\n\nskipchurches, misstemples\n\nbin/rails test .*test/test_unit/reporter_test.rb:6\n\n\z}, @output.string
  end

  test "does not output rerun snippets after run" do
    @reporter.record(failed_test)
    @reporter.report

    assert_no_match 'Failed tests:', @output.string
  end

  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
      assert interrupt_raised, 'Expected Interrupt to be raised.'
    end
  end

  test "fail fast does not interrupt run errors or skips" do
    fail_fast = Rails::TestUnitReporter.new @output, fail_fast: true

    fail_fast.record(errored_test)
    assert_no_match 'Failed tests:', @output.string

    fail_fast.record(skipped_test)
    assert_no_match 'Failed tests:', @output.string
  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 errored_test
    et = ExampleTest.new(:woot)
    et.failures << Minitest::UnexpectedError.new(ArgumentError.new("wups"))
    et
  end

  def passing_test
    ExampleTest.new(:woot)
  end

  def skipped_test
    st = ExampleTest.new(:woot)
    st.failures << begin
                     raise Minitest::Skip, "skipchurches, misstemples"
                   rescue Minitest::Assertion => e
                     e
                   end
    st
  end
end