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???

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.

Hi,

In [email protected]
“Test Unit setup teardown problem” on Thu, 28 Jul 2011 17:39:30 +0900,
Gaurang S. [email protected] 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,

Kouhei S. wrote in post #1013565:

Hi,

In [email protected]
“Test Unit setup teardown problem” on Thu, 28 Jul 2011 17:39:30 +0900,
Gaurang S. [email protected] 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,

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
http://www.ruby-forum.com/topic/2242330#1013688

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

Hi,

In [email protected]
“Re: Test Unit setup teardown problem” on Fri, 29 Jul 2011 20:26:44
+0900,
Gaurang S. [email protected] wrote:

  p "startup"

require ‘Test/BaseClass’
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,