Class: Minitest::Reporters::HtmlReporter

Inherits:
BaseReporter
  • Object
show all
Defined in:
lib/minitest/reporters/html_reporter.rb

Overview

A reporter for generating HTML test reports This is recommended to be used with a CI server, where the report is kept as an artifact and is accessible via a shared link

The reporter sorts the results alphabetically and then by results so that failing and skipped tests are at the top.

When using Minitest Specs, the number prefix is dropped from the name of the test so that it reads well

On each test run all files in the reports directory are deleted, this prevents a build up of old reports

The report is generated using ERB. A custom ERB template can be provided but it is not required The default ERB template uses JQuery and Bootstrap, both of these are included by referencing the CDN sites

Instance Attribute Summary collapse

Attributes inherited from BaseReporter

#tests

Instance Method Summary collapse

Methods inherited from BaseReporter

#add_defaults, #after_test, #before_test, #record

Constructor Details

#initialize(args = {}) ⇒ HtmlReporter

The constructor takes a hash, and uses the following keys: :title - the title that will be used in the report, defaults to 'Test Results' :reports_dir - the directory the reports should be written to, defaults to 'test/html_reports' :erb_template - the path to a custom ERB template, defaults to the supplied ERB template :mode - Useful for debugging, :terse suppresses errors and is the default, :verbose lets errors bubble up :output_filename - the report's filename, defaults to 'index.html'



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
# File 'lib/minitest/reporters/html_reporter.rb', line 56

def initialize(args = {})
  super({})

  defaults = {
      :title           => 'Test Results',
      :erb_template    => "#{File.dirname(__FILE__)}/../templates/index.html.erb",
      :reports_dir     => 'test/html_reports',
      :mode            => :safe,
      :output_filename => 'index.html'
  }

  settings = defaults.merge(args)

  @mode = settings[:mode]
  @title = settings[:title]
  @erb_template = settings[:erb_template]
  @output_filename = settings[:output_filename]
  reports_dir = settings[:reports_dir]

  @reports_path = File.absolute_path(reports_dir)

  puts "Emptying #{@reports_path}"
  FileUtils.mkdir_p(@reports_path)
  File.delete(html_file) if File.exist?(html_file)
end

Instance Attribute Details

#title (readonly)

The title of the report



21
22
23
# File 'lib/minitest/reporters/html_reporter.rb', line 21

def title
  @title
end

Instance Method Details

#friendly_name(test)

Trims off the number prefix on test names when using Minitest Specs



44
45
46
47
48
# File 'lib/minitest/reporters/html_reporter.rb', line 44

def friendly_name(test)
  groups = test.name.scan(/(test_\d+_)(.*)/i)
  return test.name if groups.empty?
  "it #{groups[0][1]}"
end

#passes

The number of tests that passed



24
25
26
# File 'lib/minitest/reporters/html_reporter.rb', line 24

def passes
  count - failures - errors - skips
end

#percent_errors_failures

The percentage of tests that failed



39
40
41
# File 'lib/minitest/reporters/html_reporter.rb', line 39

def percent_errors_failures
  ((errors+failures)/count.to_f * 100).to_i
end

#percent_passes

The percentage of tests that passed, calculated in a way that avoids rounding errors



29
30
31
# File 'lib/minitest/reporters/html_reporter.rb', line 29

def percent_passes
  100 - percent_skipps - percent_errors_failures
end

#percent_skipps

The percentage of tests that were skipped



34
35
36
# File 'lib/minitest/reporters/html_reporter.rb', line 34

def percent_skipps
  (skips/count.to_f * 100).to_i
end

#report

Called by the framework to generate the report



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
# File 'lib/minitest/reporters/html_reporter.rb', line 83

def report
  super

  begin
    puts "Writing HTML reports to #{@reports_path}"
    erb_str = File.read(@erb_template)
    renderer = ERB.new(erb_str)

    tests_by_suites = tests.group_by(&:class) # taken from the JUnit reporter

    suites = tests_by_suites.map do |suite, tests|
      suite_summary = summarize_suite(suite, tests)
      suite_summary[:tests] = tests.sort { |a, b| compare_tests(a, b) }
      suite_summary
    end

    suites.sort! { |a, b| compare_suites(a, b) }

    result = renderer.result(binding)
    File.open(html_file, 'w') do |f|
      f.write(result)
    end

  rescue Exception => e
    puts 'There was an error writing the HTML report'
    puts 'This may have been caused by cancelling the test run'
    puts 'Use mode => :verbose in the HTML reporters constructor to see more detail' if @mode == :terse
    puts 'Use mode => :terse in the HTML reporters constructor to see less detail' if @mode != :terse
    raise e if @mode != :terse
  end

end