# Child \< Parent \< Test::Unit::TestCase

**URL:** <https://www.ruby-forum.com/t/child-parent-test-testcase/117024>\
**Category:** Ruby\
**Created:** [October 12, 2007, 5:30am UTC](https://www.ruby-forum.com/t/child-parent-test-testcase/117024 "2007-10-12T05:30:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![zakifataya](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/c89c15/32.png) [@zakifataya](https://www.ruby-forum.com/u/zakifataya)\
**Post date:** [October 12, 2007, 5:30am UTC](https://www.ruby-forum.com/t/child-parent-test-testcase/117024/1 "2007-10-12T05:30:35Z")

</div>

I just ran into a problem with Test::Unit exhibited by the two files  
below and the output of ‘ruby tc\_child.rb’:

— tc\_child.rb —  
require ‘tc\_parent’

class ChildTest \< ParentTest  
def test\_one  
puts ‘test\_one called’  
assert true  
end  
end  
— tc\_child.rb —

— tc\_parent.rb —  
require “test/unit”

class ParentTest \< Test::Unit::TestCase  
def setup  
puts ‘setup called’  
end

def teardown  
puts ‘teardown called’  
end  
end  
— tc\_parent.rb —

ruby tc\_child.rb  
Loaded suite tc\_child  
Started  
setup called  
test\_one called  
teardown called  
.setup called  
Fteardown called

Finished in 0.004709 seconds.

1. Failure:  
default\_test(ParentTest) [tc\_child.rb:4]:  
No tests were specified.

## 2 tests, 2 assertions, 1 failures, 0 errors

So apparently it’s inadvisable to insert a class between  
Test::Unit::TestCase and a ‘leaf node’ test class. In my case, I ended  
up moving the base functionality into a module that I mixed into  
tc\_child.rb, and this actually worked better for what I’m trying to  
accomplish. The flexibility of modules is a great feature of Ruby.

Is there a way to instruct Test::Unit to not attempt to run tests in  
intermediate classes in the hierarchy?

Brian A.

---

<div class="post-metadata">

**Author:** ![zakifataya](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/c89c15/32.png) [@zakifataya](https://www.ruby-forum.com/u/zakifataya)\
**Post date:** [October 12, 2007, 5:50am UTC](https://www.ruby-forum.com/t/child-parent-test-testcase/117024/2 "2007-10-12T05:50:28Z")

</div>

On 10/12/07, Brian A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Is there a way to instruct Test::Unit to not attempt to run tests in  
> intermediate classes in the hierarchy?

Hi Brian,

I don’t know if it’s advisable or not, but one way is to undefine  
`default\_test’.

require ‘test/unit’

class AbstractTestCase \< Test::Unit::TestCase  
undef\_method :default\_test  
end

class ConcreteTestCase \< AbstractTestCase  
def test\_foo  
assert true  
end  
end

Having said that, unless you really need class inheritance for  
something, it’s probably cleaner just to #include mixins.

Regards,  
George.

---

<div class="post-metadata">

**Author:** ![zakifataya](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/c89c15/32.png) [@zakifataya](https://www.ruby-forum.com/u/zakifataya)\
**Post date:** [October 12, 2007, 8:52am UTC](https://www.ruby-forum.com/t/child-parent-test-testcase/117024/3 "2007-10-12T08:52:17Z")

</div>

On Oct 11, 11:49 pm, George [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> class AbstractTestCase \< Test::Unit::TestCase  
> undef\_method :default\_test  
> end

Thanks! I just looked through testcase.rb, and your suggestion seems  
to be exactly what I was asking for. I’m satisfied with the mixin  
approach for my current scenario, but it’s nice to know I have an  
option for inheritance if the need arises.

Another option is to just hide the class from ObjectSpace 🙂 I did  
some experimenting for the heck of it and came up with the following.  
It’s just a hack for fun, but can anyone come up with a better way of  
passing in the class to be hidden to the redefined each\_object method  
besides the ugly $cloaked global variable? The fact that the  
definition is being evaluated in the scope of the metaclass for  
ObjectSpace kind of complicates things.

require “test/unit”

class Module  
$cloaked = []  
def hide\_class  
$cloaked \<\< self  
end  
end

class \<\< ObjectSpace  
alias orig\_each\_object each\_object  
def each\_object(klass)  
ObjectSpace.orig\_each\_object(klass) do |obj|  
yield obj if !$cloaked.include?(obj)  
end  
end  
end

class ParentTest \< Test::Unit::TestCase  
hide\_class

def setup  
puts ‘setup called’  
end

def teardown  
puts ‘teardown called’  
end  
end

---

<div class="post-metadata">

**Author:** ![zakifataya](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/c89c15/32.png) [@zakifataya](https://www.ruby-forum.com/u/zakifataya)\
**Post date:** [October 12, 2007, 9:25am UTC](https://www.ruby-forum.com/t/child-parent-test-testcase/117024/4 "2007-10-12T09:25:35Z")

</div>

On Oct 12, 2:48 am, Brian A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Another option is to just hide the class from ObjectSpace 🙂 I did  
> some experimenting for the heck of it and came up with the following.  
> It’s just a hack for fun, but can anyone come up with a better way of  
> passing in the class to be hidden to the redefined each\_object method  
> besides the ugly $cloaked global variable? The fact that the  
> definition is being evaluated in the scope of the metaclass for  
> ObjectSpace kind of complicates things.
> 
> require “test/unit”

Using a Module class variable helps a bit, but it still doesn’t feel  
right. Of course, that’s likely because it’s way too late and my brain  
shut off a couple hours ago ☹

class Module  
def hide\_class  
Module.cloaked \<\< self  
end

def Module.cloaked  
@@cloaked ||= []  
end  
end

class \<\< ObjectSpace  
alias orig\_each\_object each\_object  
def each\_object(klass)  
ObjectSpace.orig\_each\_object(klass) do |obj|  
yield obj if !Module.cloaked.include?(obj)  
end  
end  
end
