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

D

Derek Wong

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
=09
def setup
puts "Setting up"
end
=09
def teardown
puts "Tearing down"
end
=09
def createtests(testname)
puts "Method name: #{testname}"
self.class.send:)define_method, testname) {
puts "#{testname}"
assert_equal("a", "a", "")
}
end
=09
def test_d
puts "test_d"
assert_equal("d", "d", "")
test_a()
test_b()
test_c()
end
=09
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"
=09
my_methods_list =3D 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
 
A

ara.t.howard

On Tue, 20 Mar 2007, Derek Wong wrote:

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top