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"

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

[Source]

    # 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

Public Instance methods

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

[Source]

     # File lib/test/unit/testcase.rb, line 145
145:       def ==(other)
146:         return false unless(other.kind_of?(self.class))
147:         return false unless(@method_name == other.method_name)
148:         self.class == other.class
149:       end

[Source]

     # File lib/test/unit/testcase.rb, line 100
100:       def default_test
101:         flunk("No tests were specified")
102:       end

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

[Source]

     # File lib/test/unit/testcase.rb, line 135
135:       def name
136:         "#{@method_name}(#{self.class.name})"
137:       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 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.

[Source]

    # File lib/test/unit/testcase.rb, line 92
92:       def setup
93:       end

[Source]

     # File lib/test/unit/testcase.rb, line 112
112:       def size
113:         1
114:       end

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

[Source]

    # File lib/test/unit/testcase.rb, line 97
97:       def teardown
98:       end

Overridden to return name.

[Source]

     # File lib/test/unit/testcase.rb, line 140
140:       def to_s
141:         name
142:       end

Private Instance methods

[Source]

     # File lib/test/unit/testcase.rb, line 116
116:       def add_assertion
117:         @_result.add_assertion
118:       end

[Source]

     # 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

[Source]

     # File lib/test/unit/testcase.rb, line 121
121:       def add_failure(message, all_locations=caller())
122:         @test_passed = false
123:         @_result.add_failure(Failure.new(name, filter_backtrace(all_locations), message))
124:       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 107
107:       def passed?
108:         return @test_passed
109:       end

[Validate]