3 # Author:: Nathaniel Talbott.
4 # Copyright:: Copyright (c) 2000-2003 Nathaniel Talbott. All rights reserved.
5 # License:: Ruby license.
7 require 'test/unit/assertions'
8 require 'test/unit/failure'
9 require 'test/unit/error'
10 require 'test/unit/testsuite'
11 require 'test/unit/assertionfailederror'
12 require 'test/unit/util/backtracefilter'
17 # Ties everything together. If you subclass and add your own
18 # test methods, it takes care of making them into tests and
19 # wrapping those tests into a suite. It also does the
20 # nitty-gritty of actually running an individual test and
21 # collecting its results into a Test::Unit::TestResult object.
24 include Util::BacktraceFilter
26 attr_reader :method_name
28 STARTED = name + "::STARTED"
29 FINISHED = name + "::FINISHED"
32 # These exceptions are not caught by #run.
34 PASSTHROUGH_EXCEPTIONS = [NoMemoryError, SignalException, Interrupt,
37 DECENDANT_CLASSES = []
38 def self.inherited(decendant)
39 DECENDANT_CLASSES << decendant
42 # Creates a new instance of the fixture for running the
43 # test represented by test_method_name.
44 def initialize(test_method_name)
45 unless(respond_to?(test_method_name) && method(test_method_name).arity == 0)
48 @method_name = test_method_name
52 # Rolls up all of the test* methods in the fixture into
53 # one suite, creating a new instance of the fixture for
56 method_names = public_instance_methods(true).map { |m| m.to_s }
57 tests = method_names.delete_if {|method_name| method_name !~ /^test./}
58 suite = TestSuite.new(name)
61 catch(:invalid_test) do
66 catch(:invalid_test) do
67 suite << new(:default_test)
73 # Runs the individual test method represented by this
74 # instance of the fixture, collecting statistics, failures
75 # and errors in result.
81 __send__(@method_name)
82 rescue AssertionFailedError => e
83 add_failure(e.message, e.backtrace)
85 raise if PASSTHROUGH_EXCEPTIONS.include? $!.class
90 rescue AssertionFailedError => e
91 add_failure(e.message, e.backtrace)
93 raise if PASSTHROUGH_EXCEPTIONS.include? $!.class
101 # Called before every test method runs. Can be used
102 # to set up fixture information.
106 # Called after every test method runs. Can be used to tear
107 # down fixture information.
112 flunk("No tests were specified")
115 # Returns whether this individual test passed or
116 # not. Primarily for use in teardown so that artifacts
117 # can be left behind if the test fails.
127 def add_assertion # :nodoc:
128 @_result.add_assertion
130 private :add_assertion
132 def add_failure(message, all_locations=caller()) # :nodoc:
134 @_result.add_failure(Failure.new(name, filter_backtrace(all_locations), message))
138 def add_error(exception) # :nodoc:
140 @_result.add_error(Error.new(name, exception))
144 # Returns a human-readable name for the specific test that
145 # this instance of TestCase represents.
147 "#{@method_name}(#{self.class.name})"
150 # Overridden to return #name.
155 # It's handy to be able to compare TestCase instances.
157 return false unless(other.kind_of?(self.class))
158 return false unless(@method_name == other.method_name)
159 self.class == other.class