# Module and Test::Unit

**URL:** <https://www.ruby-forum.com/t/module-and-test-unit/70190>\
**Category:** Ruby\
**Created:** [September 27, 2006, 12:42pm UTC](https://www.ruby-forum.com/t/module-and-test-unit/70190 "2006-09-27T12:42:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![aidy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/eada6e/32.png) [@aidy](https://www.ruby-forum.com/u/aidy)\
**Post date:** [September 27, 2006, 12:42pm UTC](https://www.ruby-forum.com/t/module-and-test-unit/70190/1 "2006-09-27T12:42:35Z")

</div>

Hi,

Is there any way we can use test assertations in a module, or do we  
always need to create a test class and inherit test unit?

I am using assertations for GUI tests and have modules with re-usable  
methods.

thank you

aidy

---

<div class="post-metadata">

**Author:** ![aidy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/eada6e/32.png) [@aidy](https://www.ruby-forum.com/u/aidy)\
**Post date:** [September 27, 2006, 2:02pm UTC](https://www.ruby-forum.com/t/module-and-test-unit/70190/2 "2006-09-27T14:02:44Z")

</div>

aidy wrote:

> Hi,
> 
> Is there any way we can use test assertations in a module, or do we  
> always need to create a test class and inherit test unit?
> 
> I am using assertations for GUI tests and have modules with re-usable  
> methods.

I’m not sure if this is what you’re asking, but I do this sort of thing  
quite a lot:

module ServiceTests  
def test\_foo  
assert @thing.foo  
end  
def test\_bar  
assert @thing.bar  
end  
end

class RawTests \< Test::Unit::TestCase  
include ServiceTests  
def setup  
@thing = Thing.new  
end  
end

class XMLRPCTest \< Test::Unit::TestCase  
include ServiceTests  
def setup  
client = XMLRPC::Client.new(‘localhost’, ‘/’, $port)  
@thing = client.proxy(‘thing’)  
end  
end

That ensures that return values and the like are correct across both  
native and RPC calls, without overly duplicating code. Is that what you  
were after?

---

<div class="post-metadata">

**Author:** ![aidy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/eada6e/32.png) [@aidy](https://www.ruby-forum.com/u/aidy)\
**Post date:** [September 27, 2006, 5:57pm UTC](https://www.ruby-forum.com/t/module-and-test-unit/70190/3 "2006-09-27T17:57:22Z")

</div>

You can mix in the assertion module even if you are not using  
Test::Unit::TestCase. Thus:

module mine  
include Test::Unit::Assertions  
def my\_func  
do\_something  
assert\_equal 2, x  
end  
end

The only thing “wrong” with this is that your assertions won’t be  
counted if this module is actually, later, used by a test case. To fix  
that problem, see [404 Error - Page Not Found...](http://www.io.com/~wazmo/blog/archives/2006_05.html)

Bret

---

<div class="post-metadata">

**Author:** ![aidy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/eada6e/32.png) [@aidy](https://www.ruby-forum.com/u/aidy)\
**Post date:** [September 28, 2006, 11:45am UTC](https://www.ruby-forum.com/t/module-and-test-unit/70190/4 "2006-09-28T11:45:31Z")

</div>

removed\_email\_address@domain.invalid wrote:

> [404 Error - Page Not Found...](http://www.io.com/~wazmo/blog/archives/2006_05.html)

Very useful blog. I’ll link it from mine.

cheers

aidy
