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.

Methods

==   add_assertion   add_error   add_failure   default_test   name   new   passed?   run   setup   size   suite   teardown   to_s  

Included Modules

Assertions Util::BacktraceFilter

Constants

STARTED = name + "::STARTED"
FINISHED = name + "::FINISHED"
PASSTHROUGH_EXCEPTIONS = [NoMemoryError, SignalException, Interrupt, SystemExit]   These exceptions are not caught by run.

Attributes

method_name  [R] 

Public Class methods

Creates a new instance of the fixture for running the test represented by test_method_name.

[Source]

    # 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.

[Source]

    # 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

Public Instance methods

It‘s handy to be able to compare TestCase instances.

[Source]

     # File lib/test/unit/testcase.rb, line 153
153:       def ==(other)
154:         return false unless(other.kind_of?(self.class))
155:         return false unless(@method_name == other.method_name)
156:         self.class == other.class
157:       end

[Source]

     # File lib/test/unit/testcase.rb, line 108
108:       def default_test
109:         flunk("No tests were specified")
110:       end

Returns a human-readable name for the specific test that this instance of TestCase represents.

[Source]

     # File lib/test/unit/testcase.rb, line 143
143:       def name
144:         "#{@method_name}(#{self.class.name})"
145:       end

Runs the individual test method represented by this instance of the fixture, collecting statistics, failures and errors in result.

[Source]

    # 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.

[Source]

     # File lib/test/unit/testcase.rb, line 100
100:       def setup
101:       end

[Source]

     # File lib/test/unit/testcase.rb, line 120
120:       def size
121:         1
122:       end

Called after every test method runs. Can be used to tear down fixture information.

[Source]

     # File lib/test/unit/testcase.rb, line 105
105:       def teardown
106:       end

Overridden to return name.

[Source]

     # File lib/test/unit/testcase.rb, line 148
148:       def to_s
149:         name
150:       end

Private Instance methods

[Source]

     # File lib/test/unit/testcase.rb, line 124
124:       def add_assertion
125:         @_result.add_assertion
126:       end

[Source]

     # 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

[Source]

     # File lib/test/unit/testcase.rb, line 129
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

Returns whether this individual test passed or not. Primarily for use in teardown so that artifacts can be left behind if the test fails.

[Source]

     # File lib/test/unit/testcase.rb, line 115
115:       def passed?
116:         return @test_passed
117:       end

[Validate]