Class: Test::Reporters::Html

Inherits:
Abstract show all
Defined in:
lib/rubytest/format/html.rb

Overview

HTML Test Reporter

Writes the run as one self-contained HTML document, once the run has finished: a tally, then every result under the heading of its case. Test output is captured and shown with the test that wrote it.

Constant Summary collapse

LABELS =
{
  :pass  => 'PASS',
  :fail  => 'FAIL',
  :error => 'ERROR',
  :todo  => 'TODO',
  :skip  => 'SKIP'
}
ESCAPES =
{
  '&' => '&amp;', '<' => '&lt;', '>' => '&gt;', '"' => '&quot;', "'" => '&#39;'
}
STYLE =
<<-CSS
  :root { color-scheme: light dark; --pass: #1a7f37; --fail: #cf222e; --todo: #9a6700; --skip: #57606a; --line: #d0d7de; }
  @media (prefers-color-scheme: dark) { :root { --pass: #3fb950; --fail: #f85149; --todo: #d29922; --skip: #8b949e; --line: #30363d; } }
  body { font-family: system-ui, sans-serif; line-height: 1.5; max-width: 60rem; margin: 2rem auto; padding: 0 1rem; }
  h1 { margin-bottom: 0.25rem; }
  .tally { font-weight: bold; }
  .tally.passed { color: var(--pass); }
  .tally.failed { color: var(--fail); }
  section { border-left: 2px solid var(--line); padding-left: 1rem; margin: 1rem 0; }
  .result { margin: 0.25rem 0; }
  .status { display: inline-block; min-width: 3.5rem; font: bold 0.85em ui-monospace, monospace; }
  .pass .status { color: var(--pass); }
  .fail .status, .error .status { color: var(--fail); }
  .todo .status { color: var(--todo); }
  .skip .status { color: var(--skip); }
  .time, .reason, footer { color: var(--skip); font-size: 0.85em; }
  pre { margin: 0.25rem 0 0.5rem 3.5rem; padding: 0.5rem; border: 1px solid var(--line); overflow-x: auto; font-size: 0.85em; }
CSS

Instance Attribute Summary

Attributes inherited from Abstract

#runner

Instance Method Summary collapse

Methods inherited from Abstract

#begin_test, #end_test, inherited, registry

Constructor Details

#initialize(runner, output: $stdout) ⇒ Html

Returns a new instance of Html.



45
46
47
48
49
50
# File 'lib/rubytest/format/html.rb', line 45

def initialize(runner, output: $stdout)
  super(runner)
  @output = output
  @body   = []
  @depth  = 0
end

Instance Method Details

#begin_case(test_case) ⇒ Object



64
65
66
67
68
69
70
71
# File 'lib/rubytest/format/html.rb', line 64

def begin_case(test_case)
  title, *rest = test_case.to_s.strip.split("\n")
  level = [@depth + 2, 6].min
  @body << %[<section class="case">]
  @body << %[<h#{level}>#{h title}</h#{level}>]
  @body << %[<p>#{rest.map{ |line| h(line.strip) }.join('<br>')}</p>] unless rest.empty?
  @depth += 1
end

#begin_suite(suite) ⇒ Object



58
59
60
61
# File 'lib/rubytest/format/html.rb', line 58

def begin_suite(suite)
  @body  = []
  @depth = 0
end

#capture_output? ⇒ Boolean

Test output would otherwise land in the middle of the document.

Returns:

  • (Boolean)


53
54
55
# File 'lib/rubytest/format/html.rb', line 53

def capture_output?
  true
end

#end_case(test_case) ⇒ Object



74
75
76
77
# File 'lib/rubytest/format/html.rb', line 74

def end_case(test_case)
  @depth -= 1
  @body << %[</section>]
end

#finish(summary) ⇒ Object

Write the document.



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
# File 'lib/rubytest/format/html.rb', line 99

def finish(summary)
  outcome = summary.success? ? 'passed' : 'failed'
  tally   = if summary.total.zero?
    'No tests were run.'
  else
    counts = ::Test::Result::STATUSES.map{ |status| "#{summary.counts[status]} #{TITLES[status]}" }
    "#{summary.total} tests: #{counts.join(', ')}."
  end

  @output.puts '<!DOCTYPE html>'
  @output.puts '<html lang="en">'
  @output.puts '<head>'
  @output.puts '<meta charset="utf-8">'
  @output.puts '<meta name="viewport" content="width=device-width, initial-scale=1">'
  @output.puts '<title>Test Report</title>'
  @output.puts "<style>\n#{STYLE.gsub(/^      /, '')}</style>"
  @output.puts '</head>'
  @output.puts '<body>'
  @output.puts '<h1>Test Report</h1>'
  @output.puts %[<p class="tally #{outcome}">#{h tally}</p>]
  @output.puts @body.join("\n") unless @body.empty?
  @output.puts %[<footer>Finished in #{'%.5fs' % summary.elapsed}. Generated by Rubytest #{h ::Test::VERSION} on #{h Time.now.strftime('%Y-%m-%d %H:%M:%S')}.</footer>]
  @output.puts '</body>'
  @output.puts '</html>'
end

#record(result) ⇒ Object

Add a completed result to the document.



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/rubytest/format/html.rb', line 80

def record(result)
  label = result.test.to_s.strip.split("\n").first.to_s
  label = "#{label} (case)" if result.kind == :case

  line  = %[<span class="status">#{LABELS.fetch(result.status)}</span> #{h label}]
  line << %[ <span class="reason">#{h result.reason}</span>] if result.status == :skip && result.reason
  line << %[ <span class="reason">#{h result.exception.message}</span>] if result.status == :todo && result.exception
  line << %[ <span class="time">#{'%.5fs' % result.elapsed}</span>]

  @body << %[<div class="result #{result.status}">#{line}]
  if [:fail, :error].include?(result.status)
    result.exceptions.each{ |exception| @body << %[<pre>#{h detail(exception, result.status == :error)}</pre>] }
  end
  @body << %[<pre class="stdout">#{h result.stdout}</pre>] unless result.stdout.empty?
  @body << %[<pre class="stderr">#{h result.stderr}</pre>] unless result.stderr.empty?
  @body << %[</div>]
end