-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrun_tests
executable file
·305 lines (256 loc) · 7.53 KB
/
run_tests
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#!/usr/bin/env ruby
require 'erb'
require 'lib/log'
require 'lib/report-generators/report_templates'
require 'lib/report-generators/reports'
require 'lib/test-outcome'
require 'pathname'
require 'test/unit/collector/objectspace'
require 'test/unit/testsuite'
require 'test/unit/ui/testrunnermediator'
require 'test/unit/ui/testrunnerutilities'
require 'yaml'
#----------------------------------------------------------------
# based on the console test runner
module Test
module Unit
module UI
class ThinTestRunner
extend Test::Unit::UI::TestRunnerUtilities
attr_reader :suites
# Creates a new TestRunner for running the passed
# suite. If quiet_mode is true, the output while
# running is limited to progress dots, errors and
# failures, and the final result. io specifies
# where runner output should go to; defaults to
# STDOUT.
def initialize(suite, output_level=NORMAL, io=STDOUT)
@suite = suite
@output_level = output_level
@io = io
@already_outputted = false
@faults = []
@total_passed = 0
@total_failed = 0
@suites = Hash.new {|hash, key| hash[key] = Array.new}
end
# Begins the test run.
def start
setup_mediator
attach_to_mediator
start_mediator
end
def get_binding
binding
end
private
def setup_mediator
@mediator = create_mediator(@suite)
suite_name = @suite.to_s
if @suite.kind_of?(Module)
suite_name = @suite.name
end
output("Loaded suite #{suite_name}")
end
def create_mediator(suite)
return TestRunnerMediator.new(suite)
end
def attach_to_mediator
@mediator.add_listener(TestResult::FAULT, &method(:add_fault))
@mediator.add_listener(TestRunnerMediator::STARTED, &method(:started))
@mediator.add_listener(TestRunnerMediator::FINISHED, &method(:finished))
@mediator.add_listener(TestCase::STARTED, &method(:test_started))
@mediator.add_listener(TestCase::FINISHED, &method(:test_finished))
end
def start_mediator
@mediator.run_suite
end
def add_fault(fault)
error(fault.long_display)
@current_test.add_fault(fault)
@faults << fault
output_single(fault.single_character_display, PROGRESS_ONLY)
@already_outputted = true
end
def started(result)
@result = result
output("Started")
end
def finished(elapsed_time)
nl
output("Finished in #{elapsed_time} seconds.")
@faults.each_with_index do |fault, index|
nl
output("%3d) %s" % [index + 1, fault.long_display])
end
nl
output(@result)
end
def decompose_name(name)
m = name.match(/test_(.*)[(](.*)[)]/)
if m
[m[2], m[1]]
else
['anonymous', name]
end
end
def result_file(name)
"./reports/#{mangle(name)}.result"
end
def log_file(name)
"./reports/#{mangle(name)}.result"
end
def yaml_file(name)
s, n = decompose_name(name)
"./reports/#{mangle(s)}_#{mangle(n)}.yaml"
end
def test_started(name)
suite, n = decompose_name(name)
t = TestOutcome.new(suite, n)
@current_log = File.open(t.log_path, 'w')
set_log(@current_log)
@current_test = t
@suites[suite] << t
output_single(name + ": ", VERBOSE)
end
def test_finished(name)
output_single(".", PROGRESS_ONLY) unless @already_outputted
nl(VERBOSE)
@already_outputted = false
set_log(STDERR)
@current_log.close
File.open(yaml_file(name), 'w') do |file|
file.puts @current_test.to_yaml
end
end
def total_tests
sum = 0
@suites.values.each {|s| sum += s.size}
sum
end
def total_passed
sum = 0
@suites.values.each do |s|
s.each do |t|
sum = sum + 1 if t.pass?
end
end
sum
end
def total_failed
total_tests - total_passed
end
def nl(level=NORMAL)
output("", level)
end
def output(something, level=NORMAL)
@io.puts(something) if output?(level)
@io.flush
end
def output_single(something, level=NORMAL)
@io.write(something) if output?(level)
@io.flush
end
def output?(level)
level <= @output_level
end
end
end
end
end
#----------------------------------------------------------------
def options
@options ||= OptionParser.new do |o|
o.banner = "Thin Provisioning unit test runner."
o.banner << "\nUsage: #{$0} [options] [-- untouched arguments]"
o.on
o.on('-n', '--name=NAME', String,
"Runs tests matching NAME (patterns may be used).") do |n|
n = (%r{\A/(.*)/\Z} =~ n ? Regexp.new($1) : n)
case n
when Regexp
$filters << proc{|t| n =~ t.method_name ? true : nil}
else
$filters << proc{|t| n == t.method_name ? true : nil}
end
end
o.on('-p', '--profile=NAME', String,
"Selects a config profile.") do |p|
$profile = p
end
o.on('-s', '--suite=TEST_SUITE', String,
"Selects a test suite.") do |s|
$suite = s
end
o.on('-t', '--testcase=TESTCASE', String,
"Runs tests in TestCases matching TESTCASE (patterns may be used).") do |n|
n = (%r{\A/(.*)/\Z} =~ n ? Regexp.new($1) : n)
case n
when Regexp
$filters << proc {|t| n =~ t.class.name ? true : nil}
else
$filters << proc {|t| n == t.class.name ? true : nil}
end
end
o.on('-T', '--tags=TAG', String,
"Runs tests tagged with TAG (patterns may be used).") do |n|
n = (%r{\A/(.*)/\Z} =~ n ? Regexp.new($1) : n)
case n
when Regexp
$filters << lambda do |t|
begin
tags = t.class.get_tags(t.method_name.to_sym)
tags.any? do |tag|
n =~ tag.to_s
end ? true : nil
rescue
nil
end
end
else
$filters << lambda do |t|
begin
tags = t.class.get_tags(t.method_name.to_sym)
tags.any? do |tag|
n == tag.to_s
end ? true : nil
rescue
nil
end
end
end
end
end
end
#----------------------------------------------------------------
$filters = []
$profile = nil
$suite = nil
def process_args(args = ARGV)
begin
options.order!(args) {|arg|}
rescue OptionParser::ParseError => e
puts e
puts options
$! = nil
abort
else
$filters << proc {false} unless($filters.empty?)
end
end
process_args
if $suite.nil?
STDERR.puts "please specify a test suite, eg, '--suite suites/infrastructure'"
exit(1)
end
unless $suite =~ /^suites\//
$suite = "suites/#{$suite}"
end
require $suite
c = Test::Unit::Collector::ObjectSpace.new
c.filter = $filters
suite = c.collect($0.sub(/\.rb\Z/, ''))
runner = Test::Unit::UI::ThinTestRunner.new(suite, Test::Unit::UI::VERBOSE)
runner.start
`./generate_reports`
#----------------------------------------------------------------