| Class | Test::Unit::TestCase |
| In: |
lib/test/unit/testcase.rb
|
| Parent: | Object |
Ties everything together. If you subclass and add your own test methods, it takes care of making them into tests and wrapping those tests into a suite. It also does the nitty-gritty of actually running an individual test and collecting its results into a Test::Unit::TestResult object.
| STARTED | = | name + "::STARTED" |
| FINISHED | = | name + "::FINISHED" |
| method_name | [R] |
Creates a new instance of the fixture for running the test represented by test_method_name.
# File lib/test/unit/testcase.rb, line 33
33: def initialize(test_method_name)
34: unless(respond_to?(test_method_name) and
35: (method(test_method_name).arity == 0 ||
36: method(test_method_name).arity == -1))
37: throw :invalid_test
38: end
39: @method_name = test_method_name
40: @test_passed = true
41: end
Rolls up all of the test* methods in the fixture into one suite, creating a new instance of the fixture for each method.
# File lib/test/unit/testcase.rb, line 46
46: def self.suite
47: method_names = public_instance_methods(true)
48: tests = method_names.delete_if {|method_name| method_name !~ /^test./}
49: suite = TestSuite.new(name)
50: tests.sort.each do
51: |test|
52: catch(:invalid_test) do
53: suite << new(test)
54: end
55: end
56: if (suite.empty?)
57: catch(:invalid_test) do
58: suite << new(:default_test)
59: end
60: end
61: return suite
62: end
# File lib/test/unit/testcase.rb, line 100
100: def default_test
101: flunk("No tests were specified")
102: end
Runs the individual test method represented by this instance of the fixture, collecting statistics, failures and errors in result.
# File lib/test/unit/testcase.rb, line 67
67: def run(result)
68: yield(STARTED, name)
69: @_result = result
70: begin
71: setup
72: __send__(@method_name)
73: rescue AssertionFailedError => e
74: add_failure(e.message, e.backtrace)
75: rescue StandardError, ScriptError
76: add_error($!)
77: ensure
78: begin
79: teardown
80: rescue AssertionFailedError => e
81: add_failure(e.message, e.backtrace)
82: rescue StandardError, ScriptError
83: add_error($!)
84: end
85: end
86: result.add_run
87: yield(FINISHED, name)
88: end
Called before every test method runs. Can be used to set up fixture information.
# File lib/test/unit/testcase.rb, line 92
92: def setup
93: end
Called after every test method runs. Can be used to tear down fixture information.
# File lib/test/unit/testcase.rb, line 97
97: def teardown
98: end
# File lib/test/unit/testcase.rb, line 116
116: def add_assertion
117: @_result.add_assertion
118: end
# File lib/test/unit/testcase.rb, line 127
127: def add_error(exception)
128: @test_passed = false
129: @_result.add_error(Error.new(name, exception))
130: end