Thursday, July 28, 2011

Watir - Setup and TearDown

In Test/Unit framework setup and teardown method invokes before and after each and every tests but sometimes we require to call setup method before test method of the class and teardown method after the last test method of the class.

After searching so much on the internet i found this

In the following example setup and teardown method will be called before and after each and every test.
beforeClass method will be called before the first test method executes.
afterClass method will be called after the last test method executes.

require 'test/unit'

class TestSuite < Test::Unit::TestCase

  def setup
    p 'setup'
  end

  def teardown
    p 'teardown'
  end

  def test1
    p 'test1'
  end

  def test2
    p 'test2'
  end

  def self.suite
    s = super
    def s.beforeClass
      p 'suite_setup'
    end

    def s.afterClass
      p 'suite_teardown'
    end

    def s.run(*args)
      beforeClass
      super
      afterClass
    end
    s
  end
end

0 comments:

Post a Comment