test-unit 1.2.3 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (60) hide show
  1. data/History.txt +27 -0
  2. data/Manifest.txt +30 -8
  3. data/README.txt +9 -4
  4. data/Rakefile +16 -1
  5. data/bin/testrb +0 -0
  6. data/lib/test/unit/assertions.rb +148 -48
  7. data/lib/test/unit/attribute.rb +125 -0
  8. data/lib/test/unit/autorunner.rb +101 -71
  9. data/lib/test/unit/collector/descendant.rb +23 -0
  10. data/lib/test/unit/collector/dir.rb +1 -1
  11. data/lib/test/unit/collector/load.rb +135 -0
  12. data/lib/test/unit/color.rb +61 -0
  13. data/lib/test/unit/diff.rb +524 -0
  14. data/lib/test/unit/error.rb +70 -2
  15. data/lib/test/unit/exceptionhandler.rb +39 -0
  16. data/lib/test/unit/failure.rb +63 -4
  17. data/lib/test/unit/fixture.rb +185 -0
  18. data/lib/test/unit/notification.rb +125 -0
  19. data/lib/test/unit/omission.rb +143 -0
  20. data/lib/test/unit/pending.rb +146 -0
  21. data/lib/test/unit/priority.rb +146 -0
  22. data/lib/test/unit/runner/console.rb +46 -0
  23. data/lib/test/unit/runner/emacs.rb +8 -0
  24. data/lib/test/unit/testcase.rb +193 -76
  25. data/lib/test/unit/testresult.rb +37 -28
  26. data/lib/test/unit/testsuite.rb +35 -1
  27. data/lib/test/unit/ui/console/outputlevel.rb +14 -0
  28. data/lib/test/unit/ui/console/testrunner.rb +96 -28
  29. data/lib/test/unit/ui/emacs/testrunner.rb +49 -0
  30. data/lib/test/unit/ui/testrunner.rb +20 -0
  31. data/lib/test/unit/ui/testrunnermediator.rb +28 -19
  32. data/lib/test/unit/ui/testrunnerutilities.rb +2 -7
  33. data/lib/test/unit/util/backtracefilter.rb +2 -1
  34. data/lib/test/unit/version.rb +1 -1
  35. data/test/collector/test_descendant.rb +135 -0
  36. data/test/collector/test_load.rb +333 -0
  37. data/test/run-test.rb +13 -0
  38. data/test/test_assertions.rb +221 -56
  39. data/test/test_attribute.rb +86 -0
  40. data/test/test_color.rb +37 -0
  41. data/test/test_diff.rb +477 -0
  42. data/test/test_emacs_runner.rb +60 -0
  43. data/test/test_fixture.rb +275 -0
  44. data/test/test_notification.rb +33 -0
  45. data/test/test_omission.rb +81 -0
  46. data/test/test_pending.rb +70 -0
  47. data/test/test_priority.rb +89 -0
  48. data/test/test_testcase.rb +160 -5
  49. data/test/test_testresult.rb +61 -52
  50. data/test/testunit_test_util.rb +14 -0
  51. data/test/ui/test_testrunmediator.rb +20 -0
  52. metadata +53 -23
  53. data/lib/test/unit/ui/fox/testrunner.rb +0 -268
  54. data/lib/test/unit/ui/gtk/testrunner.rb +0 -416
  55. data/lib/test/unit/ui/gtk2/testrunner.rb +0 -465
  56. data/lib/test/unit/ui/tk/testrunner.rb +0 -260
  57. data/test/runit/test_assert.rb +0 -402
  58. data/test/runit/test_testcase.rb +0 -91
  59. data/test/runit/test_testresult.rb +0 -144
  60. data/test/runit/test_testsuite.rb +0 -49
@@ -0,0 +1,8 @@
1
+ module Test
2
+ module Unit
3
+ AutoRunner.register_runner(:emacs) do |auto_runner|
4
+ require 'test/unit/ui/emacs/testrunner'
5
+ Test::Unit::UI::Emacs::TestRunner
6
+ end
7
+ end
8
+ end
@@ -4,9 +4,16 @@
4
4
  # Copyright:: Copyright (c) 2000-2003 Nathaniel Talbott. All rights reserved.
5
5
  # License:: Ruby license.
6
6
 
7
+ require 'test/unit/attribute'
8
+ require 'test/unit/fixture'
9
+ require 'test/unit/exceptionhandler'
7
10
  require 'test/unit/assertions'
8
11
  require 'test/unit/failure'
9
12
  require 'test/unit/error'
13
+ require 'test/unit/pending'
14
+ require 'test/unit/omission'
15
+ require 'test/unit/notification'
16
+ require 'test/unit/priority'
10
17
  require 'test/unit/testsuite'
11
18
  require 'test/unit/assertionfailederror'
12
19
  require 'test/unit/util/backtracefilter'
@@ -19,89 +26,193 @@ module Test
19
26
  # wrapping those tests into a suite. It also does the
20
27
  # nitty-gritty of actually running an individual test and
21
28
  # collecting its results into a Test::Unit::TestResult object.
29
+ #
30
+ # You can run two hooks before/after a TestCase run.
31
+ #
32
+ # Example:
33
+ # class TestMyClass < Test::Unit::TestCase
34
+ # class << self
35
+ # def startup
36
+ # ...
37
+ # end
38
+ #
39
+ # def shutdown
40
+ # ...
41
+ # end
42
+ # end
43
+ #
44
+ # def setup
45
+ # ...
46
+ # end
47
+ #
48
+ # def teardown
49
+ # ...
50
+ # end
51
+ #
52
+ # def test_my_method1
53
+ # ...
54
+ # end
55
+ #
56
+ # def test_my_method2
57
+ # ...
58
+ # end
59
+ # end
60
+ #
61
+ # Here is a call order:
62
+ # * startup
63
+ # * setup
64
+ # * test_my_method1
65
+ # * teardown
66
+ # * setup
67
+ # * test_my_method2
68
+ # * teardown
69
+ # * shutdown
22
70
  class TestCase
71
+ include Attribute
72
+ include Fixture
73
+ include ExceptionHandler
74
+ include ErrorHandler
75
+ include FailureHandler
76
+ include TestCasePendingSupport
77
+ include TestCaseOmissionSupport
78
+ include TestCaseNotificationSupport
79
+ include Priority
23
80
  include Assertions
24
81
  include Util::BacktraceFilter
25
-
26
- attr_reader :method_name
27
-
82
+
28
83
  STARTED = name + "::STARTED"
29
84
  FINISHED = name + "::FINISHED"
30
85
 
31
- ##
32
- # These exceptions are not caught by #run.
86
+ DESCENDANTS = []
33
87
 
34
- PASSTHROUGH_EXCEPTIONS = [NoMemoryError, SignalException, Interrupt,
35
- SystemExit]
88
+ class << self
89
+ def inherited(sub_class)
90
+ DESCENDANTS << sub_class
91
+ end
92
+
93
+ # Rolls up all of the test* methods in the fixture into
94
+ # one suite, creating a new instance of the fixture for
95
+ # each method.
96
+ def suite
97
+ method_names = public_instance_methods(true).collect {|name| name.to_s}
98
+ tests = method_names.delete_if {|method_name| method_name !~ /^test./}
99
+ suite = TestSuite.new(name, self)
100
+ tests.sort.each do |test|
101
+ catch(:invalid_test) do
102
+ suite << new(test)
103
+ end
104
+ end
105
+ if suite.empty?
106
+ catch(:invalid_test) do
107
+ suite << new("default_test")
108
+ end
109
+ end
110
+ suite
111
+ end
112
+ end
113
+
114
+ attr_reader :method_name
36
115
 
37
116
  # Creates a new instance of the fixture for running the
38
117
  # test represented by test_method_name.
39
118
  def initialize(test_method_name)
40
- unless(respond_to?(test_method_name) and
41
- (method(test_method_name).arity == 0 ||
42
- method(test_method_name).arity == -1))
43
- throw :invalid_test
44
- end
119
+ throw :invalid_test unless respond_to?(test_method_name)
120
+ throw :invalid_test if method(test_method_name).arity > 0
45
121
  @method_name = test_method_name
46
122
  @test_passed = true
47
- end
48
-
49
- # Rolls up all of the test* methods in the fixture into
50
- # one suite, creating a new instance of the fixture for
51
- # each method.
52
- def self.suite
53
- method_names = public_instance_methods(true)
54
- tests = method_names.delete_if {|method_name| method_name !~ /^test./}
55
- suite = TestSuite.new(name)
56
- tests.sort.each do
57
- |test|
58
- catch(:invalid_test) do
59
- suite << new(test)
60
- end
61
- end
62
- if (suite.empty?)
63
- catch(:invalid_test) do
64
- suite << new("default_test")
65
- end
66
- end
67
- return suite
123
+ @interrupted = false
68
124
  end
69
125
 
70
126
  # Runs the individual test method represented by this
71
127
  # instance of the fixture, collecting statistics, failures
72
128
  # and errors in result.
73
129
  def run(result)
74
- yield(STARTED, name)
75
- @_result = result
76
130
  begin
77
- setup
78
- __send__(@method_name)
79
- rescue AssertionFailedError => e
80
- add_failure(e.message, e.backtrace)
81
- rescue Exception
82
- raise if PASSTHROUGH_EXCEPTIONS.include? $!.class
83
- add_error($!)
84
- ensure
131
+ @_result = result
132
+ yield(STARTED, name)
85
133
  begin
86
- teardown
87
- rescue AssertionFailedError => e
88
- add_failure(e.message, e.backtrace)
134
+ run_setup
135
+ __send__(@method_name)
89
136
  rescue Exception
90
- raise if PASSTHROUGH_EXCEPTIONS.include? $!.class
91
- add_error($!)
137
+ @interrupted = true
138
+ raise unless handle_exception($!)
139
+ ensure
140
+ begin
141
+ run_teardown
142
+ rescue Exception
143
+ raise unless handle_exception($!)
144
+ end
92
145
  end
146
+ result.add_run
147
+ yield(FINISHED, name)
148
+ ensure
149
+ @_result = nil
93
150
  end
94
- result.add_run
95
- yield(FINISHED, name)
96
151
  end
97
152
 
98
153
  # Called before every test method runs. Can be used
99
154
  # to set up fixture information.
155
+ #
156
+ # You can add additional setup tasks by the following
157
+ # code:
158
+ # class TestMyClass < Test::Unit::TestCase
159
+ # def setup
160
+ # ...
161
+ # end
162
+ #
163
+ # setup
164
+ # def my_setup1
165
+ # ...
166
+ # end
167
+ #
168
+ # setup
169
+ # def my_setup2
170
+ # ...
171
+ # end
172
+ #
173
+ # def test_my_class
174
+ # ...
175
+ # end
176
+ # end
177
+ #
178
+ # Here is a call order:
179
+ # * setup
180
+ # * my_setup1
181
+ # * my_setup2
182
+ # * test_my_class
100
183
  def setup
101
184
  end
102
185
 
103
186
  # Called after every test method runs. Can be used to tear
104
187
  # down fixture information.
188
+ #
189
+ # You can add additional teardown tasks by the following
190
+ # code:
191
+ # class TestMyClass < Test::Unit::TestCase
192
+ # def teardown
193
+ # ...
194
+ # end
195
+ #
196
+ # teardown
197
+ # def my_teardown1
198
+ # ...
199
+ # end
200
+ #
201
+ # teardown
202
+ # def my_teardown2
203
+ # ...
204
+ # end
205
+ #
206
+ # def test_my_class
207
+ # ...
208
+ # end
209
+ # end
210
+ #
211
+ # Here is a call order:
212
+ # * test_my_class
213
+ # * my_teardown2
214
+ # * my_teardown1
215
+ # * teardown
105
216
  def teardown
106
217
  end
107
218
 
@@ -109,35 +220,10 @@ module Test
109
220
  flunk("No tests were specified")
110
221
  end
111
222
 
112
- # Returns whether this individual test passed or
113
- # not. Primarily for use in teardown so that artifacts
114
- # can be left behind if the test fails.
115
- def passed?
116
- return @test_passed
117
- end
118
- private :passed?
119
-
120
223
  def size
121
224
  1
122
225
  end
123
226
 
124
- def add_assertion
125
- @_result.add_assertion
126
- end
127
- private :add_assertion
128
-
129
- def add_failure(message, all_locations=caller())
130
- @test_passed = false
131
- @_result.add_failure(Failure.new(name, filter_backtrace(all_locations), message))
132
- end
133
- private :add_failure
134
-
135
- def add_error(exception)
136
- @test_passed = false
137
- @_result.add_error(Error.new(name, exception))
138
- end
139
- private :add_error
140
-
141
227
  # Returns a human-readable name for the specific test that
142
228
  # this instance of TestCase represents.
143
229
  def name
@@ -148,13 +234,44 @@ module Test
148
234
  def to_s
149
235
  name
150
236
  end
151
-
237
+
152
238
  # It's handy to be able to compare TestCase instances.
153
239
  def ==(other)
154
240
  return false unless(other.kind_of?(self.class))
155
241
  return false unless(@method_name == other.method_name)
156
242
  self.class == other.class
157
243
  end
244
+
245
+ def interrupted?
246
+ @interrupted
247
+ end
248
+
249
+ private
250
+ def current_result
251
+ @_result
252
+ end
253
+
254
+ def handle_exception(exception)
255
+ self.class.exception_handlers.each do |handler|
256
+ return true if send(handler, exception)
257
+ end
258
+ false
259
+ end
260
+
261
+ # Returns whether this individual test passed or
262
+ # not. Primarily for use in teardown so that artifacts
263
+ # can be left behind if the test fails.
264
+ def passed?
265
+ @test_passed
266
+ end
267
+
268
+ def problem_occurred
269
+ @test_passed = false
270
+ end
271
+
272
+ def add_assertion
273
+ current_result.add_assertion
274
+ end
158
275
  end
159
276
  end
160
277
  end
@@ -4,9 +4,19 @@
4
4
  # License:: Ruby license.
5
5
 
6
6
  require 'test/unit/util/observable'
7
+ require 'test/unit/failure'
8
+ require 'test/unit/error'
9
+ require 'test/unit/omission'
10
+ require 'test/unit/pending'
11
+ require 'test/unit/notification'
7
12
 
8
13
  module Test
9
14
  module Unit
15
+ module NullResultContainerInitializer
16
+ private
17
+ def initialize_containers
18
+ end
19
+ end
10
20
 
11
21
  # Collects Test::Unit::Failure and Test::Unit::Error so that
12
22
  # they can be displayed to the user. To this end, observers
@@ -14,66 +24,65 @@ module Test
14
24
  # UI.
15
25
  class TestResult
16
26
  include Util::Observable
27
+ include NullResultContainerInitializer
28
+ include TestResultFailureSupport
29
+ include TestResultErrorSupport
30
+ include TestResultPendingSupport
31
+ include TestResultOmissionSupport
32
+ include TestResultNotificationSupport
17
33
 
18
34
  CHANGED = "CHANGED"
19
35
  FAULT = "FAULT"
20
36
 
21
- attr_reader(:run_count, :assertion_count)
37
+ attr_reader :run_count, :assertion_count, :faults
22
38
 
23
39
  # Constructs a new, empty TestResult.
24
40
  def initialize
25
41
  @run_count, @assertion_count = 0, 0
26
- @failures, @errors = Array.new, Array.new
42
+ @summary_generators = []
43
+ @problem_checkers = []
44
+ @faults = []
45
+ initialize_containers
27
46
  end
28
47
 
29
48
  # Records a test run.
30
49
  def add_run
31
50
  @run_count += 1
32
- notify_listeners(CHANGED, self)
33
- end
34
-
35
- # Records a Test::Unit::Failure.
36
- def add_failure(failure)
37
- @failures << failure
38
- notify_listeners(FAULT, failure)
39
- notify_listeners(CHANGED, self)
40
- end
41
-
42
- # Records a Test::Unit::Error.
43
- def add_error(error)
44
- @errors << error
45
- notify_listeners(FAULT, error)
46
- notify_listeners(CHANGED, self)
51
+ notify_changed
47
52
  end
48
53
 
49
54
  # Records an individual assertion.
50
55
  def add_assertion
51
56
  @assertion_count += 1
52
- notify_listeners(CHANGED, self)
57
+ notify_changed
53
58
  end
54
59
 
55
60
  # Returns a string contain the recorded runs, assertions,
56
61
  # failures and errors in this TestResult.
62
+ def summary
63
+ ["#{run_count} tests",
64
+ "#{assertion_count} assertions",
65
+ *@summary_generators.collect {|generator| send(generator)}].join(", ")
66
+ end
67
+
57
68
  def to_s
58
- "#{run_count} tests, #{assertion_count} assertions, #{failure_count} failures, #{error_count} errors"
69
+ summary
59
70
  end
60
71
 
61
72
  # Returns whether or not this TestResult represents
62
73
  # successful completion.
63
74
  def passed?
64
- return @failures.empty? && @errors.empty?
75
+ @problem_checkers.all? {|checker| not send(checker)}
65
76
  end
66
77
 
67
- # Returns the number of failures this TestResult has
68
- # recorded.
69
- def failure_count
70
- return @failures.size
78
+ private
79
+ def notify_changed
80
+ notify_listeners(CHANGED, self)
71
81
  end
72
82
 
73
- # Returns the number of errors this TestResult has
74
- # recorded.
75
- def error_count
76
- return @errors.size
83
+ def notify_fault(fault)
84
+ @faults << fault
85
+ notify_listeners(FAULT, fault)
77
86
  end
78
87
  end
79
88
  end