Class: Test::Unit::TestCase
- Includes:
- Hardmock
- Defined in:
- lib/test_unit_before_after.rb,
lib/extend_test_unit.rb
Overview
TestCase Modifications
Monkey-patch to provide a formal mechanism for appending actions to be executed after teardown. Use after_teardown to define one or more actions to be executed after teardown for ALL tests.
COMING SOON?
-
(maybe?) Hooks for before_teardown, after_setup, on_error
-
(maybe?) Options for positional control, eg, after_teardown :before_other_actions
-
(maybe?) Provide tagging/filtering so action execution can be controlled specifically?
Usage
Invoke TestCase.after_teardown with optional parameter, which will be invoked with a reference to the test instance that has just been torn down.
Example:
Test::Unit::TestCase.after_teardown do |test|
test.verify_mocks
end
Justification
There are a number of tools and libraries that play fast-n-loose with setup and teardown by wrapping them, and by overriding method_added as a means of upholding special setup/teardown behavior, usually by re-wrapping newly defined user-level setup/teardown methods. mocha and active_record/fixtures (and previously, hardmock) will fight for this territory with often unpredictable results.
We wouldn’t have to battle if Test::Unit provided a formal pre- and post- hook mechanism.
Class Method Summary collapse
-
.after_teardown(&block) ⇒ Object
Define an action to be run after teardown.
-
.before_setup(&block) ⇒ Object
Define an action to be run before setup.
-
.post_teardown_actions ⇒ Object
Used internally.
-
.pre_setup_actions ⇒ Object
Used internally.
Instance Method Summary collapse
-
#run(runner) ⇒ Object
OVERRIDE FOR RUBY 1.9.x: This is a reimplementation of the default “run”, updated to support execution of actions before setup after teardown.
Methods included from Hardmock
all_replaced_methods, #clear_expectations, #create_mocks, has_replaced_method?, #prepare_hardmock_control, #reset_stubs, restore_all_replaced_methods, track_replaced_method, #verify_mocks
Class Method Details
.after_teardown(&block) ⇒ Object
52 53 54 |
# File 'lib/test_unit_before_after.rb', line 52 def after_teardown(&block) post_teardown_actions << block end |
.before_setup(&block) ⇒ Object
71 72 73 |
# File 'lib/test_unit_before_after.rb', line 71 def before_setup(&block) pre_setup_actions.unshift block end |
.post_teardown_actions ⇒ Object
Used internally. Access the list of post teardown actions for to be used by all tests.
58 59 60 |
# File 'lib/test_unit_before_after.rb', line 58 def post_teardown_actions @@post_teardown_actions ||= [] end |
.pre_setup_actions ⇒ Object
Used internally. Access the list of post teardown actions for to be used by all tests.
77 78 79 |
# File 'lib/test_unit_before_after.rb', line 77 def pre_setup_actions @@pre_setup_actions ||= [] end |
Instance Method Details
#run(runner) ⇒ Object
OVERRIDE FOR RUBY 1.9.x: This is a reimplementation of the default “run”, updated to support execution of actions before setup after teardown. Only the lines marked with ** are different from the MiniTest implementation of TestCase#run. (Ruby 1.9 ships with MiniTest which is similar to TestUnit in structure but with a different implementation of TestCase#run)
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/test_unit_before_after.rb', line 87 def run(result) yield(STARTED, name) @_result = result begin execute_pre_setup_actions(self) # ** Added to support TestCaseBeforeAfter setup __send__(@method_name) rescue Test::Unit::AssertionFailedError => e add_failure(e., auxiliary_backtrace_filter(e.backtrace)) rescue Exception raise if should_passthru_exception($!) # See implementation; this is for pre-1.8.6 compat add_error($!) ensure begin teardown rescue Test::Unit::AssertionFailedError => e add_failure(e., auxiliary_backtrace_filter(e.backtrace)) rescue Exception raise if should_passthru_exception($!) # See implementation; this is for pre-1.8.6 compat add_error($!) ensure execute_post_teardown_actions(self) # ** Added to support TestCaseBeforeAfter end end result.add_run yield(FINISHED, name) end |