# Test::Unit::TestCase derived classes don't work when test me

**URL:** <https://www.ruby-forum.com/t/test-testcase-derived-classes-dont-work-when-test-me/89400>\
**Category:** Ruby\
**Created:** [March 20, 2007, 5:51am UTC](https://www.ruby-forum.com/t/test-testcase-derived-classes-dont-work-when-test-me/89400 "2007-03-20T05:51:52Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Derek\_Wong](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/d2c977/32.png) [@Derek\_Wong](https://www.ruby-forum.com/u/Derek_Wong)\
**Post date:** [March 20, 2007, 5:51am UTC](https://www.ruby-forum.com/t/test-testcase-derived-classes-dont-work-when-test-me/89400/1 "2007-03-20T05:51:52Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Derek\_Wong](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/d2c977/32.png) [@Derek\_Wong](https://www.ruby-forum.com/u/Derek_Wong)\
**Post date:** [March 20, 2007, 6:23am UTC](https://www.ruby-forum.com/t/test-testcase-derived-classes-dont-work-when-test-me/89400/2 "2007-03-20T06:23:55Z")

</div>

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](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
