# Inheriting tests in testcases

**URL:** <https://www.ruby-forum.com/t/inheriting-tests-in-testcases/119490>\
**Category:** Rails\
**Created:** [November 7, 2007, 11:38am UTC](https://www.ruby-forum.com/t/inheriting-tests-in-testcases/119490 "2007-11-07T11:38:49Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![deliak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/9e8a1a/32.png) [@deliak](https://www.ruby-forum.com/u/deliak)\
**Post date:** [November 7, 2007, 11:38am UTC](https://www.ruby-forum.com/t/inheriting-tests-in-testcases/119490/1 "2007-11-07T11:38:49Z")

</div>

I’m using Test::Rails from ZenTest.  
I did an abstract(?) class that inherits from Test::Rails::TestCase and  
I wanted to define some general test there to be inherited by my  
testcases.

The problem is that when I run my testcase the tests defined in the  
avstract class are executed twice: first for the class inheriting from  
it and then again for the abstract class.

To be more clear:

MyTestCase \< MyAbstracyClass \< Test::Rails::TestCase

Supposing there is one test defined in MyTestCase and two defined in  
MyAbstractClass the test executed will be 5.

How can I skip the tests in the abstract class?  
Till now I resolved setting the following code at the beginning of each  
test:  
return if self.class == MyAbstracyClass  
…but the test are still be counted…

Any idea?

---

<div class="post-metadata">

**Author:** ![deliak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/9e8a1a/32.png) [@deliak](https://www.ruby-forum.com/u/deliak)\
**Post date:** [November 7, 2007, 11:47am UTC](https://www.ruby-forum.com/t/inheriting-tests-in-testcases/119490/2 "2007-11-07T11:47:17Z")

</div>

On 7 Nov 2007, at 10:38, Emanuele R. wrote:

> return if self.class == MyAbstracyClass  
> …but the test are still be counted…
> 
> Any idea?  
> I don’t know how zen test differs from the Test::Unit stuff that comes  
> as standard with ruby, but  
> we had a similar situation with that and ended up putting the common  
> stuff in a module which we included where appropriate.

Fred

---

<div class="post-metadata">

**Author:** ![deliak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/9e8a1a/32.png) [@deliak](https://www.ruby-forum.com/u/deliak)\
**Post date:** [November 7, 2007, 12:30pm UTC](https://www.ruby-forum.com/t/inheriting-tests-in-testcases/119490/3 "2007-11-07T12:30:01Z")

</div>

On Nov 7, 2007 9:46 PM, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > How can I skip the tests in the abstract class?  
> > stuff in a module which we included where appropriate.  
> > I’d use mixins too, but if it’s too late:

require ‘active\_support’  
require ‘test/unit’

class \<\< Test::Unit::TestCase  
def abstract  
@abstract = true  
end

def suite\_with\_abstract  
if @abstract  
Test::Unit::TestSuite.new  
else  
suite\_without\_abstract  
end  
end  
alias\_method\_chain :suite, :abstract  
end

class AbstractTests \< Test::Unit::TestCase  
abstract

def test\_a  
assert true  
end  
end

class RealTests \< AbstractTests  
end

class MoreRealTests \< AbstractTests  
end

---

<div class="post-metadata">

**Author:** ![deliak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/9e8a1a/32.png) [@deliak](https://www.ruby-forum.com/u/deliak)\
**Post date:** [November 7, 2007, 4:10pm UTC](https://www.ruby-forum.com/t/inheriting-tests-in-testcases/119490/4 "2007-11-07T16:10:43Z")

</div>

It works indeed,  
thank you very much George.

---

<div class="post-metadata">

**Author:** ![deliak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/9e8a1a/32.png) [@deliak](https://www.ruby-forum.com/u/deliak)\
**Post date:** [November 7, 2007, 1:25pm UTC](https://www.ruby-forum.com/t/inheriting-tests-in-testcases/119490/5 "2007-11-07T13:25:33Z")

</div>

Sounds great if it works, but it is not clear to me where to put the  
following code:

require ‘active\_support’  
require ‘test/unit’

class \<\< Test::Unit::TestCase  
def abstract  
@abstract = true  
end

def suite\_with\_abstract  
if @abstract  
Test::Unit::TestSuite.new  
else  
suite\_without\_abstract  
end  
end  
alias\_method\_chain :suite, :abstract  
end
