Class: Test::Unit::TestCase

Inherits:
Object show all
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

Instance Method Summary collapse

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

Define an action to be run after teardown. Subsequent calls result in multiple actions. The block will be given a reference to the test being executed.

Example:

Test::Unit::TestCase.after_teardown do |test|
  test.verify_mocks
end


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

Define an action to be run before setup. Subsequent calls result in multiple actions, EACH BEING PREPENDED TO THE PREVIOUS.

The block will be given a reference to the test being executed.

Example:

Test::Unit::TestCase.before_setup do |test|
  test.prepare_hardmock_control
end


71
72
73
# File 'lib/test_unit_before_after.rb', line 71

def before_setup(&block)
  pre_setup_actions.unshift block
end

.post_teardown_actionsObject

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_actionsObject

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(result) {|STARTED, name| ... } ⇒ Object

OVERRIDE: This is a reimplementation of the default “run”, updated to execute actions after teardown.

Yields:

  • (STARTED, name)


84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/test_unit_before_after.rb', line 84

def run(result)
  yield(STARTED, name)
  @_result = result
  begin
    execute_pre_setup_actions(self)
    setup
    __send__(@method_name)
  rescue Test::Unit::AssertionFailedError => e
    add_failure(e.message, 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.message, 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)
    end
  end
  result.add_run
  yield(FINISHED, name)
end