Test Unit setup teardown problem

Hi All,

I am using Test::Unit framework for website testing. It has setup and
teardown methods. The thing is it is called before and after each and
every test methods.

is there any way we can call setup method before first test gets execute
and teardown method after last test executes rather than before and
after each and every test???

···

--
Posted via http://www.ruby-forum.com/.

I'm not sure whether it's possible to do what you're suggesting. It's open
source, so I'm sure that if you're properly motivated to do so you could
monkeypatch this into Test::Unit. The point, though, is that you want to be
testing your objects' behavior against a known state, not just whatever
happens to be hanging around from any other tests you've already performed.
If you're relying on the results or state set by one test in order to pass
another, I'd call that a code smell, and I'd start looking for ways to
rethink or refactor.

···

On Thu, Jul 28, 2011 at 10:39 AM, Gaurang Shah <shahgomji@gmail.com> wrote:

Hi All,

I am using Test::Unit framework for website testing. It has setup and
teardown methods. The thing is it is called before and after each and
every test methods.

is there any way we can call setup method before first test gets execute
and teardown method after last test executes rather than before and
after each and every test???

--
Posted via http://www.ruby-forum.com/\.

Hi,

In <e26f7206bba0946cf6b01ddca66d10d9@ruby-forum.com>
  "Test Unit setup teardown problem" on Thu, 28 Jul 2011 17:39:30 +0900,

···

Gaurang Shah <shahgomji@gmail.com> wrote:

I am using Test::Unit framework for website testing. It has setup and
teardown methods. The thing is it is called before and after each and
every test methods.

is there any way we can call setup method before first test gets execute
and teardown method after last test executes rather than before and
after each and every test???

You can use startup and shutdown class methods for it:
  http://test-unit.rubyforge.org/test-unit/Test/Unit/TestCase.html

Thanks,
--
kou

Kouhei Sutou wrote in post #1013565:

Hi,

In <e26f7206bba0946cf6b01ddca66d10d9@ruby-forum.com>
  "Test Unit setup teardown problem" on Thu, 28 Jul 2011 17:39:30 +0900,

I am using Test::Unit framework for website testing. It has setup and
teardown methods. The thing is it is called before and after each and
every test methods.

is there any way we can call setup method before first test gets execute
and teardown method after last test executes rather than before and
after each and every test???

You can use startup and shutdown class methods for it:
  http://test-unit.rubyforge.org/test-unit/Test/Unit/TestCase.html

Thanks,

hey sutou,

Thanks for the help. However i am facing a silly problem. What I did is
i put this methods in BaseClass class and extended all my test classes
with this BaseClass, however i face the problem as this

So what i did is i make that class as module. now the problem is setup
and teardown gets invoked but startup and shutdown method doesn't.
following is the code I am using.
require 'test/unit'
require 'rubygems'
require 'watir'

module BaseClass
  class << self
    def startup
      p "startup"
      @browser = Watir::Browser.new
      @browser.goto("http://gmail.com")
      @browser.maximize
    end

    def shutdown
      p "shutdown"
      @browser.close()
    end
  end

  def setup
    puts "setup called"
  end

  def teardown
    puts "teardown called"
  end
end

DemoTest.rb
require 'Test/BaseClass'
require 'test/unit'
require 'rubygems'
require 'watir'

class DemoTest < Test::Unit::TestCase
  include BaseClass

  def test_first
    puts "first"
  end
end

···

  Gaurang Shah <shahgomji@gmail.com> wrote:

--
Posted via http://www.ruby-forum.com/\.

Hi,

In <a63b1a4a8d55adfcebf38941a6abdce8@ruby-forum.com>
  "Re: Test Unit setup teardown problem" on Fri, 29 Jul 2011 20:26:44 +0900,

So what i did is i make that class as module. now the problem is setup
and teardown gets invoked but startup and shutdown method doesn't.
following is the code I am using.
require 'test/unit'
require 'rubygems'
require 'watir'

module BaseClass
  class << self
    def startup
      p "startup"
      @browser = Watir::Browser.new
      @browser.goto("http://gmail.com")
      @browser.maximize
    end

    def shutdown
      p "shutdown"
      @browser.close()
    end
  end

  def setup
    puts "setup called"
  end

  def teardown
    puts "teardown called"
  end
end

DemoTest.rb
require 'Test/BaseClass'
require 'test/unit'
require 'rubygems'
require 'watir'

class DemoTest < Test::Unit::TestCase
  include BaseClass

  def test_first
    puts "first"
  end
end

Try this:

require 'rubygems'
gem 'test-unit'
require 'test/unit'
require 'waitr'

module BaseClass
  module ClassMethods
    def startup
      p "startup"
      @browser = Watir::Browser.new
      @browser.goto("http://gmail.com")
      @browser.maximize
    end

    def shutdown
      p "shutdown"
      @browser.close()
    end
  end

  class << self
    def included(base)
      base.extend(ClassMethods)
    end
  end

  def setup
    puts "setup called"
  end

  def teardown
    puts "teardown called"
  end
end

class DemoTest < Test::Unit::TestCase
  include BaseClass

  def test_first
    puts "first"
  end
end

Thanks,

···

Gaurang Shah <shahgomji@gmail.com> wrote:
--
kou