Test::Unit running all tests with automated suite

  • Thread starter Brett Schuchert
  • Start date
B

Brett Schuchert

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
 
R

Ryan Davis

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

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.
 
B

Brett Schuchert

Ryan said:
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?
 
R

Ryan Leavengood

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
 
B

Brett Schuchert

Ryan said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top