Test Harness?

T

Troy Denkinger

------=_Part_4965_12563328.1141923398430
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

I'm starting to feel my way around in Ruby after having been a Perler for a
very long time. I'm trying to figure out how to write a proper test harnes=
s
in Ruby and so far I'm running up against a brick wall. All my classes hav=
e
good unit tests using Test::Unit. However, I want to create a harness that
I can use to run all of the unit tests at a go and give me a success/failur=
e
report across all tests. Every time I make a significant change I have to
remember to run all my unit tests, which is error prone.

I have written test harnesses in Perl using Test::Harness::Straps that does
exactly what I want - for Perl. Is there a similar library for Ruby? If
so, I have yet to find it.

Thanks,

Troy

------=_Part_4965_12563328.1141923398430--
 
J

James Edward Gray II

I'm starting to feel my way around in Ruby after having been a
Perler for a
very long time. I'm trying to figure out how to write a proper
test harness
in Ruby and so far I'm running up against a brick wall. All my
classes have
good unit tests using Test::Unit. However, I want to create a
harness that
I can use to run all of the unit tests at a go and give me a
success/failure
report across all tests. Every time I make a significant change I
have to
remember to run all my unit tests, which is error prone.

I have written test harnesses in Perl using Test::Harness::Straps
that does
exactly what I want - for Perl. Is there a similar library for
Ruby? If
so, I have yet to find it.

Making a test suite is trivial. Just add a final like "ts_all.rb" to
your test folder that looks something like this:

#!/usr/local/bin/ruby -w

require "test/unit"

require "tc_file_one.rb"
require "tc_file_two.rb"
# ...

__END__

Execute that file to run all of the tests. Hope that helps.

James Edward Gray II
 
J

Jim Weirich

Troy said:
I'm starting to feel my way around in Ruby after having been a Perler
for a
very long time. I'm trying to figure out how to write a proper test
harness
in Ruby and so far I'm running up against a brick wall. All my classes
have
good unit tests using Test::Unit. However, I want to create a harness
that
I can use to run all of the unit tests at a go and give me a
success/failure
report across all tests. Every time I make a significant change I have
to
remember to run all my unit tests, which is error prone.

I use rake. Create a Rakefile with the following:

---------
require 'rake/testtask'

task :default => :test

Rake::TestTask.new:)test) do |t|
t.test_files = FileList['test/test_*.rb']
t.verbose = true
end
---------

The above assumes all your tests are in the test directory and begin
with 'test_'. Change as needed for your situation.

To invoke, just type

rake

or

rake test

Add other tasks as needed.

(See http://docs.rubyrake.org for more details on rake)
 
P

pat eyler

or, use Rake and a task like:

Rake::TestTask.new:)unittests) do |t|
t.test_files =3D FileList['test/test*.rb']
t.warning =3D true
t.verbose =3D false
end


then when you want to run your tests do:
$ rake unittests
(in /home/eylerpm/notes/ruby/provisioning)
Loaded suite /usr/lib/ruby/gems/1.8/gems/rake-0.7.0/lib/rake/rake_test_load=
er
Started
.................
Finished in 2.307442 seconds.

17 tests, 55 assertions, 0 failures, 0 errors
$
 
M

Mark Volkmann

I'm starting to feel my way around in Ruby after having been a Perler for= a
very long time. I'm trying to figure out how to write a proper test harn= ess
in Ruby and so far I'm running up against a brick wall. All my classes h= ave
good unit tests using Test::Unit. However, I want to create a harness th= at
I can use to run all of the unit tests at a go and give me a success/fail= ure
report across all tests. Every time I make a significant change I have t= o
remember to run all my unit tests, which is error prone.

I have written test harnesses in Perl using Test::Harness::Straps that do= es
exactly what I want - for Perl. Is there a similar library for Ruby? If
so, I have yet to find it.

In addition to the other suggestions you've received, there's always
this simple approach.

Put your code in a lib directory below your project directory.
Put your test code in a test directory.
cd to to your lib directory
run "testrb ../test/*.rb"

I should probably learn how to use Rake though.
 
R

Ross Bamford

I'm starting to feel my way around in Ruby after having been a Perler for a
very long time. I'm trying to figure out how to write a proper test harness
in Ruby and so far I'm running up against a brick wall. All my classes have
good unit tests using Test::Unit. However, I want to create a harness that
I can use to run all of the unit tests at a go and give me a success/failure
report across all tests. Every time I make a significant change I have to
remember to run all my unit tests, which is error prone.

I see you've had plenty of suggestions, but heres yet another variation:

$ ruby -e "Dir['test/tc*.rb'].each { |tc| require tc }"
Loaded suite -e
Started
.......................................
Finished in 0.25893 seconds.

39 tests, 101 assertions, 0 failures, 0 errors

That 'Loaded suite -e' thing is a shame though...
 
E

Eric Hodel

I'm starting to feel my way around in Ruby after having been a
Perler for a
very long time. I'm trying to figure out how to write a proper
test harness
in Ruby and so far I'm running up against a brick wall. All my
classes have
good unit tests using Test::Unit. However, I want to create a
harness that
I can use to run all of the unit tests at a go and give me a
success/failure
report across all tests. Every time I make a significant change I
have to
remember to run all my unit tests, which is error prone.

I have written test harnesses in Perl using Test::Harness::Straps
that does
exactly what I want - for Perl. Is there a similar library for
Ruby? If
so, I have yet to find it.

testrb (which comes with Ruby and Test::Unit) does this for you. You
only need to specify a directory and have your test files named
properly (which is test_*.rb).

$ ls
test/
$ ls test
test_one.rb test_two.rb
$ testrb test
Loaded suite test
Started
..
Finished in 0.003477 seconds.

2 tests, 2 assertions, 0 failures, 0 errors
$

If you named your tests wrong you can use the -p argument:

$ ls test
one_test.rb two_test.rb
$ testrb test -p /_test.rb/
Loaded suite test
Started
..
Finished in 0.002296 seconds.

2 tests, 2 assertions, 0 failures, 0 errors
 
D

Daniel Nugent

Is there a way to pass Rake tasks arguments?

I'd like to do something where I break my unit tests up into test
suites by just dropping them into folders and then being able to
invoke a Rake task like rake suite "sweet" or rake suite
"sweet/saccharine", if I wanted a test suite inside of a test suite.

Or, alternatively, I'd like to be able to invoke test cases named
"sucrose" by rake test_case "sucrose"

Disclaimer: I've read Rake tutorials several times in the past but
have yet to arse myself to start using it (which I should, because I
think I'm starting to exert more effor than is worthwhile keeping
tests organzied without rake), so I might've forgotten the part where
it says you can do exactly what I'm asking.

I'm starting to feel my way around in Ruby after having been a Perler f= or a
very long time. I'm trying to figure out how to write a proper test ha= rness
in Ruby and so far I'm running up against a brick wall. All my classes= have
good unit tests using Test::Unit. However, I want to create a harness = that
I can use to run all of the unit tests at a go and give me a success/fa= ilure
report across all tests. Every time I make a significant change I have= to
remember to run all my unit tests, which is error prone.

I see you've had plenty of suggestions, but heres yet another variation:

$ ruby -e "Dir['test/tc*.rb'].each { |tc| require tc }"
Loaded suite -e
Started
.......................................
Finished in 0.25893 seconds.

39 tests, 101 assertions, 0 failures, 0 errors

That 'Loaded suite -e' thing is a shame though...
 
R

Ross Bamford

Is there a way to pass Rake tasks arguments?

You can do the easy way:

$ cat Rakefile
task :default do
puts ENV['SOMEVAR']
end

$ rake
(in /home/rosco/dev/ruby/ngtest)
nil

$ rake SOMEVAR=321
(in /home/rosco/dev/ruby/ngtest)
321

Another sneaky thing I've done before when embedding rake in other stuff
is to have a task that creates a Rake rule that matches everything, so
that all arguments following that task are grabbed by that rule.

task :create do
rule '' do |t|
argument_passed = t.name

# do stuff with argument...
end
end

This is only good when you don't want to have further tasks on the same
commandline, but works pretty nice in some cases e.g.

rake create myproject
I'd like to do something where I break my unit tests up into test
suites by just dropping them into folders and then being able to
invoke a Rake task like rake suite "sweet" or rake suite
"sweet/saccharine", if I wanted a test suite inside of a test suite.

Or, alternatively, I'd like to be able to invoke test cases named
"sucrose" by rake test_case "sucrose"

The Rake test-task lets you specify the name for the task, so it's
pretty easy to split up your tests into groups in the Rakefile by simply
defining separate tasks for e.g. functional and unit tests. I often have
a couple of separate tasks, with a single task bringing them together
via task dependencies as an :alltest task.

Also, as others have mentioned, testrb is pretty handy for this stuff:

$ testrb tests/tc*.rb -n '/test_ruby_xml.*/'
47 tests, 339 assertions, 0 failures, 0 errors

$ testrb tests/tc*.rb -n '/test_ruby_xml_document.*/'
15 tests, 129 assertions, 0 failures, 0 errors

(output trimmed for brevity)
 
J

Jim Weirich

Daniel said:
Is there a way to pass Rake tasks arguments?

I'd like to do something where I break my unit tests up into test
suites by just dropping them into folders and then being able to
invoke a Rake task like rake suite "sweet" or rake suite
"sweet/saccharine", if I wanted a test suite inside of a test suite.

Or, alternatively, I'd like to be able to invoke test cases named
"sucrose" by rake test_case "sucrose"

Disclaimer: I've read Rake tutorials several times in the past but
have yet to arse myself to start using it (which I should, because I
think I'm starting to exert more effor than is worthwhile keeping
tests organzied without rake), so I might've forgotten the part where
it says you can do exactly what I'm asking.

This page: http://rake.rubyforge.org/classes/Rake/TestTask.html pretty
well describes how the built-in test task takes arguments.
 
T

Troy Denkinger

------=_Part_6926_9434393.1141968860679
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

Thanks to everyone for the ideas on this. I've implemented the rake
suggestion, and it mostly does what I'm looking for.

Thanks,

Troy

This page: http://rake.rubyforge.org/classes/Rake/TestTask.html pretty
well describes how the built-in test task takes arguments.

------=_Part_6926_9434393.1141968860679--
 

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

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top