# Working with Test::Unit

**URL:** <https://www.ruby-forum.com/t/working-with-test-unit/225985>\
**Category:** Ruby\
**Created:** [January 2, 2008, 4:01pm UTC](https://www.ruby-forum.com/t/working-with-test-unit/225985 "2008-01-02T16:01:08Z")\
**Posts on this page:** 6\
**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:** [January 2, 2008, 4:01pm UTC](https://www.ruby-forum.com/t/working-with-test-unit/225985/1 "2008-01-02T16:01:08Z")

</div>

Hi,

I use Watir and Test::Unit.

There are a number of sites I test that are very similar.

I have a base test unit script e.g.

class Main\_Site\_Test \< Test::Unit::TestCase  
include Watir::Assertions

def setup  
@browser = Project::Browser.new  
@register = Project::Register.new  
@home = Project::HomeExceptions.new  
@login = {:url=\> ‘main\_site.com’}  
end

def test\_0002\_login  
@browser.goto(@login[:url])  
@home.log\_in\_link.click  
@home.username\_edit.set(‘removed\_email\_address@domain.invalid’)  
@home.password\_edit.set(‘pass123’)  
@register.go\_button.click  
verify(@browser.include(‘Welcome’), ‘not logged in successfully’)

rescue =\> e  
@browser.close  
end

So for the next site I will do this

class Similar\_Test \< Main\_Site\_Test  
def setup  
super  
@login = {:url=\> ‘similar\_test.com’}  
end  
end

However, I can’t run the Similar\_Test class without running the  
Main\_Site\_Test class

require ‘main\_site\_test’  
require ‘similar\_test’

Also, I have written ‘test\_0002\_login’, can I not just use that  
specific method in different test classes?

Any suggestions?

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:** [January 2, 2008, 5:48pm UTC](https://www.ruby-forum.com/t/working-with-test-unit/225985/2 "2008-01-02T17:48:37Z")

</div>

aidy wrote:

> However, I can’t run the Similar\_Test class without running the  
> Main\_Site\_Test class
> 
> require ‘main\_site\_test’  
> require ‘similar\_test’
> 
> Also, I have written ‘test\_0002\_login’, can I not just use that  
> specific method in different test classes?
> 
> Any suggestions?

Would using mixin instead inheritance work in your case?

Best regards,

Jari W.

---

<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:** [January 2, 2008, 5:45pm UTC](https://www.ruby-forum.com/t/working-with-test-unit/225985/3 "2008-01-02T17:45:41Z")

</div>

On Jan 2, 9:55 am, aidy [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> @home.username\_edit.set(‘removed\_email\_address@domain.invalid’)  
> class Similar\_Test \< Main\_Site\_Test  
> require ‘similar\_test’
> 
> Also, I have written ‘test\_0002\_login’, can I not just use that  
> specific method in different test classes?
> 
> Any suggestions?
> 
> Aidy

I think this only partially applies to your situation, but the  
following thread has info regarding inheritance issues with Test::Unit

[http://groups.google.com/group/comp.lang.ruby/browse\_frm/thread/a2ee41414dc9376d/eea4ead591c1da1f](http://groups.google.com/group/comp.lang.ruby/browse_frm/thread/a2ee41414dc9376d/eea4ead591c1da1f)

If it’s not helpful, maybe you could clarify what you want to do. It  
seems from your post that you don’t want to be required to execute  
Main\_Site\_Test to execute Similar\_Test, but you also want to reuse  
test\_0002\_login which is in Main\_Site\_Test.

---

<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:** [January 3, 2008, 10:57am UTC](https://www.ruby-forum.com/t/working-with-test-unit/225985/4 "2008-01-03T10:57:13Z")

</div>

Hi Jari,

> Would using mixin instead inheritance work in your case?

Could you give me an example of where the module would be used and  
what it would contain?

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:** [January 3, 2008, 11:10am UTC](https://www.ruby-forum.com/t/working-with-test-unit/225985/5 "2008-01-03T11:10:40Z")

</div>

On 2 Jan, 16:41, Brian A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On Jan 2, 9:55 am,aidy[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> If it’s not helpful, maybe you could clarify what you want to do. It  
> seems from your post that you don’t want to be required to execute  
> Main\_Site\_Test to execute Similar\_Test, but you also want to reuse  
> test\_0002\_login which is in Main\_Site\_Test.- Hide quoted text -

class Common \< Test::Unit::TestCase  
include Watir::Assertions

def setup  
@browser = Project::Browser.new  
@home = Project::HomeExceptions.new  
@register = Project::Register.new  
@login = {:url=\> ‘[baseclass.com](http://baseclass.com)’}  
end

def test\_login  
@browser.goto(@login[:url])  
@home.log\_in\_link.click  
@home.username\_edit.set(‘removed\_email\_address@domain.invalid’)  
@home.password\_edit.set(‘pass123’)  
@register.go\_button.click  
end

end

class Sub\_Class \< Common

def setup  
@login = {:url=\> ‘[subclass.com](http://subclass.com)’}  
end

```
def test_login
    super
end

```

end

I am looking to use the methods in the Common class only in the sub  
class. I do not want the the Common  
class to run as well.

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:** [January 3, 2008, 10:48pm UTC](https://www.ruby-forum.com/t/working-with-test-unit/225985/6 "2008-01-03T22:48:30Z")

</div>

aidy wrote:

> Hi Jari,
> 
> > Would using mixin instead inheritance work in your case?
> 
> Could you give me an example of where the module would be used and  
> what it would contain?
> 
> Aidy

We encountered a similar condition with our test framework. While the  
Test::Unit framework provides the mechanism to run tests, we found that  
there was a lot of duplication of code…particularly in the  
setup/teardown methods.

Our approach used something like this (names changed to protect the  
innocent)

common\_testcase.rb

module MyNamespace  
module TestCase

def setup  
# Enter common setup code here  
puts “Common setup code”  
end

def teardown  
# Enter common teardown code here  
puts “Common teardown code”  
end

def doWork  
# Enter common worker code here  
puts “Common worker method”  
end

end # TestCase  
end # MyNamespace

my\_testcase.rb

class TC\_MyTestCase \< Test::Unit::TestCase

require ‘common\_testcase.rb’  
include MyNameSpace::TestCase

# Test Case - Do the test

def test\_doTest  
doWork  
end  
end # TC\_MyTestCase

By implementing the common functionality in a separate module, it can be  
included in each of your test cases. Place the common code in the  
module, and mixin with the test cases. Just don’t place any ‘test\_\*’  
methods in the common code.

Hope this helps.
