Permalink
Browse files

adding before / after hooks for setup and teardown

  • Loading branch information...
1 parent 6b09fb1 commit 66c968375c6b84e232d4b495e0131499ed959fbe @tenderlove committed Jan 18, 2012
Showing with 115 additions and 11 deletions.
  1. +8 −0 lib/minitest/spec.rb
  2. +32 −8 lib/minitest/unit.rb
  3. +75 −3 test/test_minitest_unit.rb
@@ -238,6 +238,14 @@ def self.to_s # :nodoc:
end
# :stopdoc:
+ def after_setup
+ run_setup_hooks
+ end
+
+ def before_teardown
+ run_teardown_hooks
+ end
+
class << self
attr_reader :desc
alias :specify :it
View
@@ -1031,8 +1031,9 @@ def run runner
result = ""
begin
@passed = nil
+ self.before_setup
self.setup
- self.run_setup_hooks
+ self.after_setup
self.run_test self.__name__
result = "." unless io?
@passed = true
@@ -1042,13 +1043,14 @@ def run runner
@passed = false
result = runner.puke self.class, self.__name__, e
ensure
- begin
- self.run_teardown_hooks
- self.teardown
- rescue *PASSTHROUGH_EXCEPTIONS
- raise
- rescue Exception => e
- result = runner.puke self.class, self.__name__, e
+ %w{ before_teardown teardown after_teardown }.each do |hook|
+ begin
+ self.send hook
+ rescue *PASSTHROUGH_EXCEPTIONS
+ raise
+ rescue Exception => e
+ result = runner.puke self.class, self.__name__, e
+ end
end
trap 'INFO', 'DEFAULT' if SUPPORTS_INFO_SIGNAL
end
@@ -1141,10 +1143,32 @@ def passed?
def setup; end
##
+ # Runs before every test after setup. Use this to refactor test
+ # initialization.
+
+ def after_setup; end
+
+ ##
+ # Runs before every setup. Use this to refactor test initialization.
+
+ def before_setup; end
+
+ ##
# Runs after every test. Use this to refactor test cleanup.
def teardown; end
+ ##
+ # Runs after every test before teardown. Use this to refactor test
+ # initialization.
+
+ def before_teardown; end
+
+ ##
+ # Runs after every teardown. Use this to refactor test cleanup.
+
+ def after_teardown; end
+
def self.reset_setup_teardown_hooks # :nodoc:
@setup_hooks = []
@teardown_hooks = []
@@ -469,10 +469,82 @@ def test_inherited_hook_plays_nice_with_others
end
end
+ def test_before_setup
+ call_order = []
+ Class.new(MiniTest::Unit::TestCase) do
+ define_method :setup do
+ super()
+ call_order << :setup
+ end
+
+ define_method :before_setup do
+ call_order << :before_setup
+ end
+
+ def test_omg; assert true; end
+ end
+
+ @tu.run %w[--seed 42]
+
+ expected = [:before_setup, :setup]
+ assert_equal expected, call_order
+ end
+
+ def test_after_teardown
+ call_order = []
+ Class.new(MiniTest::Unit::TestCase) do
+ define_method :teardown do
+ super()
+ call_order << :teardown
+ end
+
+ define_method :after_teardown do
+ call_order << :after_teardown
+ end
+
+ def test_omg; assert true; end
+ end
+
+ @tu.run %w[--seed 42]
+
+ expected = [:teardown, :after_teardown]
+ assert_equal expected, call_order
+ end
+
+ def test_all_teardowns_are_guaranteed_to_run
+ call_order = []
+ Class.new(MiniTest::Unit::TestCase) do
+ define_method :after_teardown do
+ super()
+ call_order << :after_teardown
+ raise
+ end
+
+ define_method :teardown do
+ super()
+ call_order << :teardown
+ raise
+ end
+
+ define_method :before_teardown do
+ super()
+ call_order << :before_teardown
+ raise
+ end
+
+ def test_omg; assert true; end
+ end
+
+ @tu.run %w[--seed 42]
+
+ expected = [:before_teardown, :teardown, :after_teardown]
+ assert_equal expected, call_order
+ end
+
def test_setup_hooks
call_order = []
- tc = Class.new(MiniTest::Unit::TestCase) do
+ tc = Class.new(MiniTest::Spec) do
define_method :setup do
super()
call_order << :method
@@ -509,7 +581,7 @@ def test_setup_hooks
def test_teardown_hooks
call_order = []
- tc = Class.new(MiniTest::Unit::TestCase) do
+ tc = Class.new(MiniTest::Spec) do
define_method :teardown do
super()
call_order << :method
@@ -546,7 +618,7 @@ def test_teardown_hooks
def test_setup_and_teardown_hooks_survive_inheritance
call_order = []
- parent = Class.new(MiniTest::Unit::TestCase) do
+ parent = Class.new(MiniTest::Spec) do
define_method :setup do
super()
call_order << :setup_method

0 comments on commit 66c9683

Please sign in to comment.