[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

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

Derek Wong

3/20/2007 4:51:00 AM

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
1 Answer

Ara.T.Howard

3/20/2007 5:23:00 AM

0