1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/railties/lib/rails/test_unit/reporter.rb
Kasper Timm Hansen 0db310586a Prefer Minitest's location for test failures.
When running tests, the Rails test runner would report the start of the test method as the test failure.

For this test:

```ruby
1 require 'test_helper
2
3 class BunnyTest < ActiveSupport::TestCase
4   test "something failing" do
5     assert false, 'This failed'
6   end
7 end
```

The runner outputs 5 instead of 4:

```
............................................F

This failed

bin/rails test test/models/bunny_test.rb:5

........
```
2015-11-12 21:39:58 +01:00

70 lines
1.6 KiB
Ruby

require "active_support/core_ext/class/attribute"
require "minitest"
module Rails
class TestUnitReporter < Minitest::StatisticsReporter
class_attribute :executable
self.executable = "bin/rails test"
def record(result)
super
if output_inline? && result.failure && (!result.skipped? || options[:verbose])
io.puts
io.puts
io.puts result.failures.map(&:message)
io.puts
io.puts format_rerun_snippet(result)
io.puts
end
if fail_fast? && result.failure && !result.error? && !result.skipped?
raise Interrupt
end
end
def report
return if output_inline? || filtered_results.empty?
io.puts
io.puts "Failed tests:"
io.puts
io.puts aggregated_results
end
def aggregated_results # :nodoc:
filtered_results.map { |result| format_rerun_snippet(result) }.join "\n"
end
def filtered_results
if options[:verbose]
results
else
results.reject(&:skipped?)
end
end
def relative_path_for(file)
file.sub(/^#{Rails.root}\/?/, '')
end
private
def output_inline?
options[:output_inline]
end
def fail_fast?
options[:fail_fast]
end
def format_rerun_snippet(result)
# Try to extract path to assertion from backtrace.
if result.location =~ /\[(.*)\]\z/
assertion_path = $1
else
assertion_path = result.method(result.name).source_location.join(':')
end
"#{self.executable} #{relative_path_for(assertion_path)}"
end
end
end