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" | ||
PASSTHROUGH_EXCEPTIONS | = | [NoMemoryError, SignalException, Interrupt, SystemExit] | These exceptions are not caught by run. |
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 39 39: 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 45: @method_name = test_method_name 46: @test_passed = true 47: 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 52 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 68: end
# File lib/test/unit/testcase.rb, line 108 108: def default_test 109: flunk("No tests were specified") 110: 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 73 73: def run(result) 74: yield(STARTED, name) 75: @_result = result 76: 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 85: begin 86: teardown 87: rescue AssertionFailedError => e 88: add_failure(e.message, e.backtrace) 89: rescue Exception 90: raise if PASSTHROUGH_EXCEPTIONS.include? $!.class 91: add_error($!) 92: end 93: end 94: result.add_run 95: yield(FINISHED, name) 96: end
Called before every test method runs. Can be used to set up fixture information.
# File lib/test/unit/testcase.rb, line 100 100: def setup 101: end
Called after every test method runs. Can be used to tear down fixture information.
# File lib/test/unit/testcase.rb, line 105 105: def teardown 106: end
# File lib/test/unit/testcase.rb, line 124 124: def add_assertion 125: @_result.add_assertion 126: end
# File lib/test/unit/testcase.rb, line 135 135: def add_error(exception) 136: @test_passed = false 137: @_result.add_error(Error.new(name, exception)) 138: end