rubytest 0.9.0 → 0.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,87 @@
1
+ ## Rake task
2
+
3
+ `rubytest/rake` provides a task for running tests from a Rakefile.
4
+
5
+ require 'rubytest/rake'
6
+ require 'tmpdir'
7
+
8
+ project = Dir.mktmpdir('rubytest-rake')
9
+ Dir.mkdir(File.join(project, 'test'))
10
+
11
+ probe = lambda do |label, body|
12
+ <<-RUBY
13
+ probe = Object.new
14
+ probe.define_singleton_method(:call) { #{body} }
15
+ probe.define_singleton_method(:to_s) { #{label.inspect} }
16
+ $TEST_SUITE << probe
17
+ RUBY
18
+ end
19
+
20
+ File.write(File.join(project, 'test', 'good_test.rb'), probe.call('good', 'true'))
21
+ File.write(File.join(project, 'bad.rb'), probe.call('bad', "raise Assertion, 'wrong value'"))
22
+
23
+ status_of = lambda do |task|
24
+ Dir.chdir(project) do
25
+ begin
26
+ task.run
27
+ 0
28
+ rescue SystemExit => exit
29
+ exit.status
30
+ end
31
+ end
32
+ end
33
+
34
+ The block configures the run, as with `Test.configure`. The task is an
35
+ ordinary Rake task.
36
+
37
+ passing = Test::Rake::TestTask.new('demo:pass') do |run|
38
+ run.files << 'test/good_test.rb'
39
+ run.format = 'test'
40
+ end
41
+
42
+ Rake::Task.task_defined?('demo:pass').assert == true
43
+ status_of.call(passing).assert == 0
44
+
45
+ A run that fails makes the task exit with status 1, so that `rake` fails too.
46
+
47
+ failing = Test::Rake::TestTask.new('demo:fail') do |run|
48
+ run.files << 'bad.rb'
49
+ run.format = 'test'
50
+ end
51
+
52
+ status_of.call(failing).assert == 1
53
+
54
+ Given no files, the task looks under `test/` for files named `test_*.rb`,
55
+ `*_test.rb`, `case_*.rb` or `*_case.rb`, and runs whichever of those it
56
+ finds. It also puts `lib` on the load path.
57
+
58
+ defaults = Test::Rake::TestTask.new('demo:defaults') { |run| run.format = 'test' }
59
+
60
+ status_of.call(defaults).assert == 0
61
+ defaults.config.files.assert == ['test/**/*_test.rb']
62
+ defaults.config.loadpath.assert == ['lib']
63
+
64
+ If it finds none, it says so.
65
+
66
+ empty = Dir.mktmpdir('rubytest-rake-empty')
67
+ nothing = Test::Rake::TestTask.new('demo:nothing') { |run| run.format = 'test' }
68
+ missing = begin
69
+ Dir.chdir(empty) { nothing.run }
70
+ nil
71
+ rescue ArgumentError => error
72
+ error
73
+ end
74
+
75
+ missing.message.assert.start_with?('no test files found')
76
+
77
+ The `TEST` environment variable selects the tests for one invocation, in
78
+ place of the task's own.
79
+
80
+ previous = ENV['TEST']
81
+ begin
82
+ ENV['TEST'] = 'bad.rb'
83
+ status_of.call(passing).assert == 1
84
+ passing.config.files.assert == ['bad.rb']
85
+ ensure
86
+ ENV['TEST'] = previous
87
+ end
data/lib/rubytest/cli.rb CHANGED
@@ -9,7 +9,7 @@ module Test
9
9
  # Test configuration file can be in `etc/test.rb` or `config/test.rb`, or
10
10
  # `Testfile` or '.test` with optional `.rb` extension, in that order of
11
11
  # precedence. To use a different file there is the -c/--config option.
12
- GLOB_CONFIG = '{etc/test.rb,config/test.rb,testfile.rb,testfile,.test.rb,.test}'
12
+ GLOB_CONFIG = '{etc/test.rb,config/test.rb,Testfile.rb,Testfile,testfile.rb,testfile,.test.rb,.test}'
13
13
 
14
14
  # Convenience method for invoking the CLI.
15
15
  #
@@ -95,7 +95,7 @@ module Test
95
95
  conf.requires.concat makelist(file)
96
96
  end
97
97
  opt.on '-c', '--config FILE', "use alternate config file" do |file|
98
- conf.config_files << file
98
+ conf.config_file = file
99
99
  end
100
100
  opt.on '-V' , '--verbose', 'provide extra detail in reports' do
101
101
  conf.verbose = true
@@ -141,6 +141,10 @@ module Test
141
141
  @config_file
142
142
  end
143
143
 
144
+ def config_file=(file)
145
+ @config_file = file
146
+ end
147
+
144
148
  def profile
145
149
  @profile
146
150
  end
@@ -51,7 +51,7 @@ module Test
51
51
 
52
52
  #
53
53
  def self.assertionless=(boolean)
54
- @assertionaless = !!boolean
54
+ @assertionless = !!boolean
55
55
  end
56
56
 
57
57
  # Find and cache project root directory.
@@ -43,34 +43,6 @@ module Test
43
43
  def begin_test(test)
44
44
  end
45
45
 
46
- #
47
- def skip_case(test_case)
48
- end
49
-
50
- #
51
- def skip_test(test)
52
- end
53
-
54
- #
55
- #def test(test)
56
- #end
57
-
58
- #
59
- def pass(test)
60
- end
61
-
62
- #
63
- def fail(test, exception)
64
- end
65
-
66
- # Report a test error.
67
- def error(test, exception)
68
- end
69
-
70
- # Report a pending test.
71
- def todo(test, exception)
72
- end
73
-
74
46
  #
75
47
  def end_test(test)
76
48
  end
@@ -79,21 +51,8 @@ module Test
79
51
  def end_case(test_case)
80
52
  end
81
53
 
82
- #
83
- def end_suite(test_suite)
84
- end
85
-
86
54
  protected
87
55
 
88
- def record
89
- runner.recorder
90
- end
91
-
92
- # Is coverage information requested?
93
- #def cover?
94
- # runner.cover?
95
- #end
96
-
97
56
  # Count up the total number of tests.
98
57
  def total_count(suite)
99
58
  c = 0
@@ -107,73 +66,14 @@ module Test
107
66
  return c
108
67
  end
109
68
 
110
- # Common timestamp any reporter can use.
111
- def timestamp
112
- seconds = Time.now - @start_time
113
-
114
- "Finished in %.5fs, %.2f tests/s." % [seconds, total/seconds]
115
- end
116
-
117
- #
118
- def total
119
- @total ||= subtotal
120
- end
121
-
122
- #
123
- def subtotal
124
- [:todo, :pass, :fail, :error, :omit, :skip].inject(0) do |s,r|
125
- s += record[r.to_sym].size; s
126
- end
127
- end
128
-
129
- # TODO: lump skipped and omitted into one group ?
130
-
131
69
  TITLES = {
132
70
  :pass => 'passing',
133
71
  :fail => 'failures',
134
72
  :error => 'errors',
135
73
  :todo => 'pending',
136
- :omit => 'omissions',
137
74
  :skip => 'skipped'
138
75
  }
139
76
 
140
- # TODO: Add assertion counts (if reasonably possible).
141
-
142
- # Common tally stamp any reporter can use.
143
- #
144
- # @return [String] tally stamp
145
- def tally
146
- sizes = {}
147
- names = %w{pass error fail todo omit skip}.map{ |n| n.to_sym }
148
- names.each do |r|
149
- sizes[r] = record[r].size
150
- end
151
-
152
- #names.unshift(:tests)
153
- #sizes[:tests] = total
154
-
155
- s = []
156
- names.each do |n|
157
- next unless sizes[n] > 0
158
- s << tally_item(n, sizes)
159
- end
160
-
161
- 'Executed ' + "#{total}".ansi(:bold) + ' tests with ' + s.join(', ') + '.'
162
- end
163
-
164
- #
165
- def tally_item(name, sizes)
166
- x = []
167
- x << "%s" % sizes[name].to_s.ansi(:bold)
168
- x << " %s" % TITLES[name].downcase
169
- x << " (%.1f%%)" % ((sizes[name].to_f/total*100)) if runner.verbose?
170
- x.join('')
171
- end
172
-
173
- #--
174
- # TODO: Matching `bin/ruby-test` is not robust.
175
- #++
176
-
177
77
  # Remove reference to lemon library from backtrace.
178
78
  #
179
79
  # @param [Exception] exception
@@ -2,11 +2,34 @@
2
2
 
3
3
  module Test::Reporters
4
4
 
5
- # Hash Abstract is a base class for the TAP-Y
6
- # and TAP-J reporters.
5
+ # Structured records used by the internal test reporter.
7
6
  #
8
7
  class AbstractHash < Abstract
9
8
 
9
+ def capture_output?
10
+ true
11
+ end
12
+
13
+ # Return a structured record of the completed result.
14
+ def record(result)
15
+ @current_result = result
16
+ if result.status == :skip
17
+ method = result.kind == :case ? :skip_case : :skip_test
18
+ public_send(method, result.test, result.reason)
19
+ elsif result.status == :pass
20
+ pass(result.test)
21
+ else
22
+ public_send(result.status, result.test, result.exception)
23
+ end
24
+ ensure
25
+ @current_result = nil if result
26
+ end
27
+
28
+ def finish(summary)
29
+ @summary = summary
30
+ end_suite(summary.suite)
31
+ end
32
+
10
33
  #
11
34
  # @return [Hash]
12
35
  #
@@ -51,9 +74,6 @@ module Test::Reporters
51
74
  #
52
75
  def begin_test(test)
53
76
  @test_index += 1
54
-
55
- @stdout, @stderr = $stdout, $stderr
56
- $stdout, $stderr = StringIO.new, StringIO.new
57
77
  end
58
78
 
59
79
  # Ruby Test use the term "skip", where as TAP-Y/J uses "omit".
@@ -62,22 +82,16 @@ module Test::Reporters
62
82
  #
63
83
  # @return [Hash]
64
84
  #
65
- def skip_test(test)
66
- h = {}
67
- h['type' ] = 'test'
68
- h['status'] = 'omit'
69
-
70
- merge_subtype h, test
71
- merge_setup h, test
72
- merge_label h, test
73
- #merge_comparison h, test, exception
74
- #merge_coverage h, test
75
- merge_source h, test
76
- #merge_exception h, test, exception
77
- merge_output h
78
- merge_time h
85
+ def skip_test(test, reason=nil)
86
+ skip_record(test, reason)
87
+ end
79
88
 
80
- return h
89
+ # A skipped case is reported like a skipped test, as a case.
90
+ #
91
+ # @return [Hash]
92
+ #
93
+ def skip_case(test_case, reason=nil)
94
+ skip_record(test_case, reason).merge('type' => 'case')
81
95
  end
82
96
 
83
97
  #
@@ -169,7 +183,6 @@ module Test::Reporters
169
183
  #
170
184
  def end_test(test)
171
185
  super(test)
172
- $stdout, $stderr = @stdout, @stderr
173
186
  end
174
187
 
175
188
  #
@@ -183,14 +196,14 @@ module Test::Reporters
183
196
  def end_suite(suite)
184
197
  h = {
185
198
  'type' => 'final',
186
- 'time' => Time.now - @start_time,
199
+ 'time' => @summary.elapsed,
187
200
  'counts' => {
188
- 'total' => total,
189
- 'pass' => record[:pass].size,
190
- 'fail' => record[:fail].size,
191
- 'error' => record[:error].size,
192
- 'omit' => record[:omit].size,
193
- 'todo' => record[:todo].size
201
+ 'total' => @summary.total,
202
+ 'pass' => @summary.counts[:pass],
203
+ 'fail' => @summary.counts[:fail],
204
+ 'error' => @summary.counts[:error],
205
+ 'omit' => @summary.counts[:skip],
206
+ 'todo' => @summary.counts[:todo]
194
207
  }
195
208
  }
196
209
  return h
@@ -198,6 +211,23 @@ module Test::Reporters
198
211
 
199
212
  private
200
213
 
214
+ # The record of a skipped test or case.
215
+ def skip_record(test, reason)
216
+ h = {}
217
+ h['type' ] = 'test'
218
+ h['status'] = 'omit'
219
+ h['reason'] = reason if reason
220
+
221
+ merge_subtype h, test
222
+ merge_setup h, test
223
+ merge_label h, test
224
+ merge_source h, test
225
+ merge_output h
226
+ merge_time h
227
+
228
+ return h
229
+ end
230
+
201
231
  # For todo entries in particulr, i.e. NotImplementedError
202
232
  # exception, the return value represents the "todo" priority
203
233
  # level. The `Exception#priority` method returns an Integer
@@ -206,7 +236,7 @@ module Test::Reporters
206
236
  #
207
237
  def merge_priority(hash, test, exception)
208
238
  level = exception.priority
209
- h['priority'] = level.to_i
239
+ hash['priority'] = level.to_i
210
240
  end
211
241
 
212
242
  #
@@ -233,8 +263,8 @@ module Test::Reporters
233
263
 
234
264
  # Add source location information to hash.
235
265
  def merge_source(hash, test)
236
- if test.respond_to?('source_location')
237
- file, line = source_location
266
+ if test.respond_to?(:source_location)
267
+ file, line = test.source_location
238
268
  hash['file' ] = file
239
269
  hash['line' ] = line
240
270
  hash['source' ] = code(file, line).to_str
@@ -251,6 +281,12 @@ module Test::Reporters
251
281
  hash['exception']['snippet' ] = code(exception).to_omap
252
282
  hash['exception']['message' ] = exception.message
253
283
  hash['exception']['backtrace'] = clean_backtrace(exception) if bt
284
+ if @current_result && @current_result.exceptions.size > 1
285
+ hash['exceptions'] = @current_result.exceptions.map do |error|
286
+ {'class' => error.class.to_s, 'message' => error.message,
287
+ 'backtrace' => clean_backtrace(error)}
288
+ end
289
+ end
254
290
  end
255
291
 
256
292
  # TODO: This is still an "idea in progress" for both RybyTest and Tap-Y/J.
@@ -280,13 +316,13 @@ module Test::Reporters
280
316
 
281
317
  #
282
318
  def merge_output(hash)
283
- hash['stdout'] = $stdout.string
284
- hash['stderr'] = $stderr.string
319
+ hash['stdout'] = @current_result.stdout
320
+ hash['stderr'] = @current_result.stderr
285
321
  end
286
322
 
287
323
  #
288
324
  def merge_time(hash)
289
- hash['time'] = Time.now - @start_time
325
+ hash['time'] = @current_result.elapsed
290
326
  end
291
327
 
292
328
  end
@@ -5,82 +5,108 @@ module Test::Reporters
5
5
  # Simple Dot-Progress Reporter
6
6
  class Dotprogress < Abstract
7
7
 
8
- def skip_test(unit, reason)
9
- print "S".ansi(:cyan) if runner.verbose?
8
+ def initialize(runner, output: $stdout)
9
+ super(runner)
10
+ @output = output
11
+ @verbose = runner.verbose?
10
12
  end
11
13
 
12
- def pass(unit)
13
- print "."
14
- $stdout.flush
14
+ def record(result)
15
+ if result.status == :skip
16
+ method = result.kind == :case ? :skip_case : :skip_test
17
+ public_send(method, result.test, result.reason)
18
+ elsif result.status == :pass
19
+ pass(result.test)
20
+ else
21
+ public_send(result.status, result.test, result.exception)
22
+ end
23
+ end
24
+
25
+ def finish(summary)
26
+ @summary = summary
27
+ end_suite(summary.suite)
28
+ end
29
+
30
+ def skip_test(test, reason)
31
+ emit('S'.ansi(:cyan)) if @verbose
15
32
  end
16
33
 
17
- def fail(unit, exception)
18
- print "F".ansi(:red)
19
- $stdout.flush
34
+ def skip_case(test_case, reason)
35
+ emit('S'.ansi(:cyan)) if @verbose
20
36
  end
21
37
 
22
- def error(unit, exception)
23
- print "E".ansi(:red, :bold)
24
- $stdout.flush
38
+ def pass(test)
39
+ emit('.')
25
40
  end
26
41
 
27
- def todo(unit, exception)
28
- print "P".ansi(:yellow)
29
- $stdout.flush
42
+ def fail(test, exception)
43
+ emit('F'.ansi(:red))
44
+ end
45
+
46
+ def error(test, exception)
47
+ emit('E'.ansi(:red, :bold))
48
+ end
49
+
50
+ def todo(test, exception)
51
+ emit('P'.ansi(:yellow))
30
52
  end
31
53
 
32
54
  def end_suite(suite)
33
- puts; puts
34
- puts timestamp
35
- puts
36
-
37
- if runner.verbose?
38
- unless record[:omit].empty?
39
- puts "SKIPPED\n\n"
40
- record[:skip].each do |test, reason|
41
- puts " #{test}".ansi(:bold)
42
- puts " #{reason}" if String===reason
43
- puts
44
- end
45
- end
46
- end
55
+ summary = @summary
56
+ @output.puts
57
+ @output.puts
58
+ rate = summary.elapsed.zero? ? 0.0 : summary.total / summary.elapsed
59
+ @output.puts "Finished in %.5fs, %.2f tests/s." % [summary.elapsed, rate]
60
+ @output.puts
47
61
 
48
- unless record[:todo].empty?
49
- puts "PENDING\n\n"
50
- record[:todo].each do |test, exception|
51
- puts " #{test}".ansi(:bold) unless test.to_s.empty?
52
- puts " #{exception}"
53
- puts " #{file_and_line(exception)}"
54
- puts code(exception)
55
- puts
62
+ if @verbose && summary.counts[:skip].positive?
63
+ @output.puts "SKIPPED\n\n"
64
+ summary.results_for(:skip).each do |result|
65
+ @output.puts " #{result.test}".ansi(:bold)
66
+ @output.puts " #{result.reason}" if String === result.reason
67
+ @output.puts
56
68
  end
57
69
  end
58
70
 
59
- unless record[:fail].empty?
60
- puts "FAILURES\n\n"
61
- record[:fail].each do |test_unit, exception|
62
- puts " #{test_unit}".ansi(:bold)
63
- puts " #{exception}"
64
- puts " #{file_and_line(exception)}"
65
- puts code(exception)
66
- puts " " + clean_backtrace(exception).join("\n ")
67
- puts
71
+ {todo: 'PENDING', fail: 'FAILURES', error: 'ERRORS'}.each do |status, title|
72
+ next if summary.counts[status].zero?
73
+ @output.puts "#{title}\n\n"
74
+ summary.results_for(status).each do |result|
75
+ @output.puts " #{result.test}".ansi(:bold) unless status == :todo && result.test.to_s.empty?
76
+ result.exceptions.each_with_index do |exception, index|
77
+ prefix = result.exceptions.size > 1 ? "#{index + 1}. " : ''
78
+ @output.puts " #{prefix}#{exception.class}: #{exception.message}"
79
+ @output.puts " #{file_and_line(exception)}"
80
+ @output.puts code(exception)
81
+ @output.puts " " + clean_backtrace(exception).join("\n ") unless status == :todo
82
+ end
83
+ @output.puts
68
84
  end
69
85
  end
70
86
 
71
- unless record[:error].empty?
72
- puts "ERRORS\n\n"
73
- record[:error].each do |test_unit, exception|
74
- puts " #{test_unit}".ansi(:bold)
75
- puts " #{exception}"
76
- puts " #{file_and_line(exception)}"
77
- puts code(exception)
78
- puts " " + clean_backtrace(exception).join("\n ")
79
- puts
80
- end
87
+ if summary.total.zero?
88
+ @output.puts 'No tests were run.'
89
+ else
90
+ @output.puts tally_for(summary)
81
91
  end
92
+ end
82
93
 
83
- puts tally
94
+ private
95
+
96
+ def emit(symbol)
97
+ @output.print(symbol)
98
+ @output.flush
99
+ end
100
+
101
+ def tally_for(summary)
102
+ items = [:pass, :error, :fail, :todo, :skip].filter_map do |status|
103
+ count = summary.counts[status]
104
+ next if count.zero?
105
+ title = TITLES[status].downcase
106
+ percentage = @verbose ? " (%.1f%%)" % (count.to_f / summary.total * 100) : ''
107
+ "#{count.to_s.ansi(:bold)} #{title}#{percentage}"
108
+ end
109
+ "Executed #{summary.total.to_s.ansi(:bold)} tests with #{items.join(', ')}."
84
110
  end
85
111
 
86
112
  end