Testcase Setup And Teardown With Blocks plugin

Plugin details

The current implementation of setup and teardown by Test::Unit and Rails only allow the setup method to be overridden once. This implementation of setup and teardown makes use of blocks, rather than full methods.

Repositoryhttp://svn.viney.net.nz/things/rails/plugins/testcase_setup_and_teardown_with_blocks/ Author Jonathan Viney Tags setup, Test LicenseMIT

Documentation

Install the plugin:
ruby script/plugin install http://svn.viney.net.nz/things/rails/plugins/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
    
    # This is overridden by the subclass and will not be called
    def teardown
    end
  end
  
  class PersonTest < Test::Unit::TestCase
    # This overrides the method in the superclass
    def setup
    end
    
    # This overrides the method in the superclass
    def teardown
    end
    
    # Only the setup and teardown methods in this class will be called
    def test_name
    end
  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
    
    teardown do
    end
  end
  
  class PersonTest < Test::Unit::TestCase
    setup do
    end
    
    teardown do
    end
    
    def test_name
    end
  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.

Further Documentation

There is currently no advanced documentation for this plugin.

New documentation

Edit plugin | (0 older versions) | Last edited by: hardway, about 1 year ago