# Test::Unit running all tests with automated suite

**URL:** <https://www.ruby-forum.com/t/test-unit-running-all-tests-with-automated-suite/90936>\
**Category:** Ruby\
**Created:** [April 3, 2007, 3:35am UTC](https://www.ruby-forum.com/t/test-unit-running-all-tests-with-automated-suite/90936 "2007-04-03T03:35:28Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![brettschuchert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/278dde/32.png) [@brettschuchert](https://www.ruby-forum.com/u/brettschuchert)\
**Post date:** [April 3, 2007, 3:35am UTC](https://www.ruby-forum.com/t/test-unit-running-all-tests-with-automated-suite/90936/1 "2007-04-03T03:35:28Z")

</div>

Am I missing something when using Test::Unit in Eclipse? I am used to  
using JUnit and/or NUnit and being able to select a project and run all  
of the tests. I don’t see this feature and I suspect I’m just blind.

I’ve come up with the following “automated” suite script that seems to  
get the job done. I had to do this in JUnit 5 years ago when the JUnit  
eclipse 2.x plugin didn’t support this:

require ‘test/unit’  
require ‘find’

def allFiles  
files = Array.new  
Find.find(’./’) { |f|  
if(f =~ /\_test/)  
f = f.sub(/.//, “”).sub(/.rb$/,"")  
files.push(f)  
end  
}  
files  
end

allFiles().each {|f| require f}

A comment:  
This is simple, I know. I only recursively find files that match  
\*\_test.rb from the current directory then I require the files and  
Test::Unit automatically generates the suite method for me. For now this  
is adequate.

So two questions:

1. Is the necessary or what am I missing (I’ve just started using Ruby a  
few days ago).
2. I assume someone can suggest to me ways to shorten this up. I’m all  
ears.

Thanks!

Brett

---

<div class="post-metadata">

**Author:** ![brettschuchert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/278dde/32.png) [@brettschuchert](https://www.ruby-forum.com/u/brettschuchert)\
**Post date:** [April 3, 2007, 7:18am UTC](https://www.ruby-forum.com/t/test-unit-running-all-tests-with-automated-suite/90936/2 "2007-04-03T07:18:20Z")

</div>

There’s an excellent article at the following URL on IBM’s site  
introducing the Ruby mode for Eclipse. Tthey run you through how to  
set up Test::Unit run configurations. Hope this helps.  
Andy Morrow

[http://www-128.ibm.com/developerworks/opensource/library/os-](http://www-128.ibm.com/developerworks/opensource/library/os-)  
rubyeclipse/#N101D7

---

<div class="post-metadata">

**Author:** ![brettschuchert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/278dde/32.png) [@brettschuchert](https://www.ruby-forum.com/u/brettschuchert)\
**Post date:** [April 3, 2007, 3:40pm UTC](https://www.ruby-forum.com/t/test-unit-running-all-tests-with-automated-suite/90936/3 "2007-04-03T15:40:52Z")

</div>

Andy Morrow wrote:

> There’s an excellent article at the following URL on IBM’s site
> 
> [http://www-128.ibm.com/developerworks/opensource/library/os-](http://www-128.ibm.com/developerworks/opensource/library/os-)  
> rubyeclipse/#N101D7  
> Andy,

Thanks, that is an excellent article for getting started and it’s what I  
started with. Unfortunately it only discusses running individual tests,  
not suites of tests.

---

<div class="post-metadata">

**Author:** ![brettschuchert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/278dde/32.png) [@brettschuchert](https://www.ruby-forum.com/u/brettschuchert)\
**Post date:** [April 3, 2007, 10:44am UTC](https://www.ruby-forum.com/t/test-unit-running-all-tests-with-automated-suite/90936/4 "2007-04-03T10:44:47Z")

</div>

On Apr 2, 2007, at 18:35 , Brett S. wrote:

> require ‘find’  
> end  
> As far as eclipse goes, I cannot help you. Generically, there are  
> many ways to go about it.

- Use testrb:

% testrb test/\*\*/\*\_test.rb  
[…]  
Finished in 5.562656 seconds.

297 tests, 886 assertions, 0 failures, 0 errors

- Here is a much more concise version of what you wrote:

(%w(test/unit) + Dir[’\*\*/\*\_test.rb’]).each { |f| require f }

- And then there is autotest:

% autotest

# and happily leave it running all day

- And probably many more.

---

<div class="post-metadata">

**Author:** ![brettschuchert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/278dde/32.png) [@brettschuchert](https://www.ruby-forum.com/u/brettschuchert)\
**Post date:** [April 3, 2007, 3:55pm UTC](https://www.ruby-forum.com/t/test-unit-running-all-tests-with-automated-suite/90936/5 "2007-04-03T15:55:13Z")

</div>

Ryan D. wrote:

> On Apr 2, 2007, at 18:35 , Brett S. wrote:
> 
> > require ‘find’  
> > end  
> > As far as eclipse goes, I cannot help you. Generically, there are  
> > many ways to go about it.

Ya, maybe I’m just an old Eclipse hack (I like using it). I even use a  
VI plugin in eclipse.

> - Use testrb:
> 
> % testrb test/\*\*/\*\_test.rb

Maybe instead of using Test::Unit I should be using other testing  
frameworks. Not sure. When I started using Java years ago (and C++  
before that) I did pretty much work at the command line.

> - Here is a much more concise version of what you wrote:
> 
> (%w(test/unit) + Dir[’\*\*/\*\_test.rb’]).each { |f| require f }

Thanks, that’s better. I need to update this to skip subversion meta  
directories (.svn) but I’m sure I can figure that out.

> - And then there is autotest:
> 
> % autotest
> 
> # and happily leave it running all day
> 
> - And probably many more.

So is there a direction in which the “ruby community” is going with  
respect to unit testing and continuous integration?

---

<div class="post-metadata">

**Author:** ![brettschuchert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/278dde/32.png) [@brettschuchert](https://www.ruby-forum.com/u/brettschuchert)\
**Post date:** [April 3, 2007, 7:03pm UTC](https://www.ruby-forum.com/t/test-unit-running-all-tests-with-automated-suite/90936/6 "2007-04-03T19:03:52Z")

</div>

Ryan L. wrote:

> dc = Test::Unit::Collector::Dir.new  
> dc.pattern \<\< /\b.\*.rb\Z/m  
> dc.collect
> 
> Someone really needs to properly document all the cool stuff included  
> with Test::Unit. That someone may be me eventually 🙂
> 
> Ryan

Even better!

Thanks Ryan, I figured I was missing something obvious. Now if someone  
would update the plugin to do this!

Brett

---

<div class="post-metadata">

**Author:** ![brettschuchert](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/278dde/32.png) [@brettschuchert](https://www.ruby-forum.com/u/brettschuchert)\
**Post date:** [April 3, 2007, 4:46pm UTC](https://www.ruby-forum.com/t/test-unit-running-all-tests-with-automated-suite/90936/7 "2007-04-03T16:46:29Z")

</div>

On 4/2/07, Brett S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> So two questions:
> 
> 1. Is the necessary or what am I missing (I’ve just started using Ruby a  
> few days ago).
> 2. I assume someone can suggest to me ways to shorten this up. I’m all  
> ears.

To answer both questions:

require ‘test/unit’  
require ‘test/unit/collector/dir’

Test::Unit::Collector::Dir.new.collect

By default this recursively collects and runs all test case classes in  
the current directory in files that match the pattern test\_\*.rb. It is  
very customizable though. For example to examine all .rb files for  
tests:

dc = Test::Unit::Collector::Dir.new  
dc.pattern \<\< /\b.\*.rb\Z/m  
dc.collect

Someone really needs to properly document all the cool stuff included  
with Test::Unit. That someone may be me eventually 🙂

Ryan
