Test::Unit::TestCase derived classes don't work when test methods are generated dynamically

Hi,

I'm new to the world of Ruby and still getting to grips with the language so please bear with me.

The following script attempts to use the Test::Unit::TestCase framework in conjunction with dynamic test method generation. It does not work as expected.

require 'test/unit'
require 'test/unit/assertions'

class TestGenerator < Test::Unit::TestCase
  
  def setup
    puts "Setting up"
  end
  
  def teardown
    puts "Tearing down"
  end
    
  def createtests(testname)
    puts "Method name: #{testname}"
    self.class.send(:define_method, testname) {
      puts "#{testname}"
      assert_equal("a", "a", "")
    }
  end
  
  def test_d
    puts "test_d"
    assert_equal("d", "d", "")
    test_a()
    test_b()
    test_c()
  end
  
  def initialize(test_method_name)
    createtests("test_a")
    puts "Created test_a"
    createtests("test_b")
    puts "Created test_b"
    createtests("test_c")
    puts "Created test_c"
    
    my_methods_list = self.methods.sort
    my_methods_list.each do |method|
      #puts "Methods supported #{method}"
    end
    super(test_method_name)
  end
end

I was expecting the following output:

Method name: test_a
Created test_a
Method name: test_b
Created test_b
Method name: test_c
Created test_c
Loaded suite TestGenerator
Started
Setting up
test_a
Tearing down
Setting up
test_b
Tearing down
Setting up
test_c
Tearing down
Setting up
test_d
test_a
test_b
test_c
Tearing down
.

But instead I the following output:

Method name: test_a
Created test_a
Method name: test_b
Created test_b
Method name: test_c
Created test_c
Loaded suite TestGenerator
Started
Setting up
test_d
test_a
test_b
test_c
Tearing down
.

This (and the my_methods_list loop) seems to indicate that test_a(), test_b() and test_c() methods are actually added but for some reason the Test::Unit::TestCase framework does not pick them up.

I am using Ruby 184-20.

Thanks for shedding any light on this.

Regards,
Derek Wong.

here's an example of dynamic tests

   http://codeforpeople.com/lib/ruby/xx/xx-2.0.0/test/xx.rb

the point is that you need the tests defined before intialize is called so you
need to dynamically add them as class scope.

harp:~ > cat a.rb
require 'test/unit'
require 'test/unit/assertions'

class TestGenerator < Test::Unit::TestCase

  def setup
    puts "Setting up"
  end

  def teardown
    puts "Tearing down"
  end

  def self.createtests(testname)
    puts "Method name: #{testname}"
    define_method(testname) {
      puts "#{testname}"
      assert_equal("a", "a", "")
    }
  end

  def test_d
    puts "test_d"
    assert_equal("d", "d", "")
    test_a()
    test_b()
    test_c()
  end

    createtests("test_a")
    puts "Created test_a"
    createtests("test_b")
    puts "Created test_b"
    createtests("test_c")
    puts "Created test_c"
  def initialize(test_method_name)

    my_methods_list = self.methods.sort
    my_methods_list.each do |method|
      #puts "Methods supported #{method}"
    end
    super(test_method_name)
  end
end

harp:~ > ruby a.rb
Method name: test_a
Created test_a
Method name: test_b
Created test_b
Method name: test_c
Created test_c
Loaded suite a
Started
Setting up
test_a
Tearing down
.Setting up
test_b
Tearing down
.Setting up
test_c
Tearing down
.Setting up
test_d
test_a
test_b
test_c
Tearing down
.
Finished in 0.001139 seconds.

4 tests, 7 assertions, 0 failures, 0 errors

regards.

-a

···

On Tue, 20 Mar 2007, Derek Wong wrote:

--
be kind whenever possible... it is always possible.
- the dalai lama