user1256045
user1256045

Reputation: 23

running same set of tests for multiple instance using ruby Unit test

I want to run a set of tests with two different arguments in initialization. what I am doing right now is:

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

class My_test < Test::Unit::TestCase
  class << self
    def startup
      $obj = Calc.new("test1")
    end
  end

  def test_1
    #testing $obj method 1
  end

  def test_2
    #testing $obj method 2
  end
  .
  .
  .
end

Now I want to perform all the tests test_1...test_n with different argument say 'test2', Calc.new("test2").

What is the best way to do it? Any suggestion. I am using gem test-unit 2.5.x

Upvotes: 2

Views: 547

Answers (1)

knut
knut

Reputation: 27855

You could put test_1... in modules and create 2 testclasses.

Example (one test is successfull, the 2nd has an error):

require 'rubygems'
gem 'test-unit'
require 'test/unit'
#~ require 'calc'

module My_tests
  def test_1
    assert_equal( 2, 2*@@obj)
  end
end

class My_test1 < Test::Unit::TestCase
  class << self
    def startup
      @@obj = 1 #Calc.new("test1")
    end
  end
  include My_tests
end

class My_test2 < Test::Unit::TestCase
  class << self
    def startup
      @@obj = 2 #Calc.new("test1")
    end
  end
  include My_tests
end

I used no global variable ($obj) but a class attribute (@@obj).

Perhaps you should better use setup:

require 'rubygems'
gem 'test-unit'
require 'test/unit'
#~ require 'calc'

module My_tests
  def test_1
    assert_equal( 2, 2*@obj)
  end
end

class My_test1 < Test::Unit::TestCase
  def setup
    @obj = 1 #Calc.new("test1")
  end
  include My_tests
end

class My_test2 < Test::Unit::TestCase
  def setup
    @obj = 2 #Calc.new("test1")
  end
  include My_tests
end

Upvotes: 1

Related Questions