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.