Testcase - Setup and Teardown with Blocks

The current implementation of setup and teardown by Test::Unit and Rails only allow the setup method to be overridden once. In the following example, only the setup and teardown methods in PersonTest will be executed:

class Test::Unit::TestCase
  # This is overridden by the subclass and will not be called
  def setup
  end
end
  1. This is overridden by the subclass and will not be called def teardown end
class PersonTest < Test::Unit::TestCase
  # This overrides the method in the superclass
  def setup
  end
end
  1. This overrides the method in the superclass def teardown end
  1. Only the setup and teardown methods in this class will be called def test_name end

This behaviour is quite standard in object oriented programming, but in the case of Test::Unit it would be nice to be able to define multiple setup and teardown methods.

This implementation of setup and teardown makes use of blocks, rather than full methods. The above example becomes:

class Test::Unit::TestCase
  setup do
  end
end
teardown do
end
class PersonTest < Test::Unit::TestCase
  setup do
  end
end
teardown do
end
def test_name
end

Both setup and teardown can be called in class definitions as many times as required and, during testing, are executed in the order they are defined.

Existing code will continue to work as before. Code containing setup and teardown methods do not have to be converted to block format although it is recommended for consistency.

== Resources

Installation

  • script/plugin install http://svn.viney.net.nz/things/rails/plugins/testcase_setup_and_teardown_with_blocks

Tags

You need to Login to tag this item.