-
Notifications
You must be signed in to change notification settings - Fork 104
/
testrunner.rb
224 lines (203 loc) · 6.98 KB
/
testrunner.rb
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#--
#
# Author:: Kouhei Sutou
# Copyright::
# * Copyright (c) 2011 Kouhei Sutou <kou@clear-code.com>
# License:: Ruby license.
require 'erb'
require 'time'
require 'test/unit/ui/testrunner'
require 'test/unit/ui/testrunnermediator'
module Test
module Unit
module UI
module XML
# Runs a Test::Unit::TestSuite and outputs XML.
class TestRunner < UI::TestRunner
include ERB::Util
# Creates a new TestRunner for running the passed
# suite. :output option specifies where runner
# output should go to; defaults to STDOUT.
def initialize(suite, options={})
super
@output = @options[:output] || STDOUT
if @options[:output_file_descriptor]
@output = IO.new(@options[:output_file_descriptor], "w")
end
@already_outputted = false
@indent = 0
@top_level = true
@current_test = nil
@current_test_suite = nil
@already_outputted = false
end
private
def attach_to_mediator
@mediator.add_listener(TestResult::PASS_ASSERTION,
&method(:result_pass_assertion))
@mediator.add_listener(TestResult::FAULT,
&method(:result_fault))
@mediator.add_listener(TestRunnerMediator::STARTED,
&method(:started))
@mediator.add_listener(TestRunnerMediator::FINISHED,
&method(:finished))
@mediator.add_listener(TestCase::STARTED_OBJECT,
&method(:test_started))
@mediator.add_listener(TestCase::FINISHED_OBJECT,
&method(:test_finished))
@mediator.add_listener(TestSuite::STARTED_OBJECT,
&method(:test_suite_started))
@mediator.add_listener(TestSuite::FINISHED_OBJECT,
&method(:test_suite_finished))
end
def result_pass_assertion(result)
open_tag("pass-assertion") do
output_test(@current_test)
end
end
def result_fault(fault)
open_tag("test-result") do
open_tag("result") do
output_test_suite(@current_test_suite)
output_test(@current_test)
open_tag("backtrace") do
fault.location.each do |entry|
file, line, info = entry.split(/:/, 3)
open_tag("entry") do
add_content("file", file)
add_content("line", line)
add_content("info", info)
end
end
end
if fault.respond_to?(:expected)
add_content("expected", fault.expected)
end
if fault.respond_to?(:actual)
add_content("actual", fault.actual)
end
add_content("detail", fault.message)
add_content("status", fault.label.downcase)
end
end
@already_outputted = true if fault.critical?
end
def started(result)
@result = result
output_started
end
def output_started
open_tag("stream")
end
def finished(elapsed_time)
add_content("success", @result.passed?)
close_tag("stream")
end
def test_started(test)
@already_outputted = false
@current_test = test
open_tag("start-test") do
output_test(test)
end
end
def test_finished(test)
unless @already_outputted
open_tag("test-result") do
output_test(test)
open_tag("result") do
output_test_suite(@current_test_suite)
output_test(test)
add_content("status", "success")
end
end
end
open_tag("complete-test") do
output_test(test)
add_content("success", test.passed?)
end
@current_test = nil
end
def test_suite_started(suite)
@current_test_suite = suite
if suite.test_case.nil?
open_tag("ready-test-suite") do
add_content("n-tests", suite.size)
end
open_tag("start-test-suite") do
output_test_suite(suite)
end
else
open_tag("ready-test-case") do
output_test_suite(suite)
add_content("n-tests", suite.size)
end
open_tag("start-test-case") do
output_test_suite(suite)
end
end
end
def test_suite_finished(suite)
if suite.test_case.nil?
open_tag("complete-test-suite") do
output_test_suite(suite)
add_content("success", suite.passed?)
end
else
open_tag("complete-test-case") do
output_test_suite(suite)
add_content("success", suite.passed?)
end
end
@current_test_suite = nil
end
def indent
" " * @indent
end
def open_tag(name)
@output.puts("#{indent}<#{name}>")
@indent += 2
if block_given?
yield
close_tag(name)
end
end
def add_content(name, content)
return if content.nil?
case content
when Time
content = content.iso8601
end
@output.puts("#{indent}<#{name}>#{h(content)}</#{name}>")
end
def close_tag(name)
@indent -= 2
@output.puts("#{indent}</#{name}>")
end
def output_test(test)
open_tag("test") do
add_content("name", test.method_name)
add_content("start-time", test.start_time)
add_content("elapsed", test.elapsed_time)
end
end
def output_test_suite(test_suite)
test_case = test_suite.test_case
if test_case.nil?
open_tag("test-suite") do
add_content("name", test_suite.name)
add_content("start-time", test_suite.start_time)
add_content("elapsed", test_suite.elapsed_time)
end
else
open_tag("test-case") do
add_content("name", test_suite.name)
add_content("start-time", test_suite.start_time)
add_content("elapsed", test_suite.elapsed_time)
end
end
end
end
end
end
end
end