Test::Unit can't handle Exceptions ? (neither IRB)

G

gabriele renzi

Hi gurus and nubys,

I have a strange behaviour I can't explain
If I run this code:

require 'test/unit'

class T < Test::Unit::TestCase
def test_f
raise Exception.new
end
end

Test::Unit goes boom:

Loaded suite boom
Started
boom.rb:5:in `test_f': Exception (Exception)
from rubydir/1.8/test/unit/testcase.rb:70:in `__send__'
from rubydir/1.8/test/unit/testcase.rb:70:in `run'
from rubydir/1.8/test/unit/testsuite.rb:32:in `run'
from rubydir/1.8/test/unit/testsuite.rb:31:in `each'
from rubydir/1.8/test/unit/testsuite.rb:31:in `run'
from rubydir/1.8/test/unit/testsuite.rb:32:in `run'
from rubydir/1.8/test/unit/testsuite.rb:31:in `each'
from rubydir/1.8/test/unit/testsuite.rb:31:in `run'
from rubydir/1.8/test/unit/ui/testrunnermediator.rb:44:in `run_suite'
from rubydir/1.8/test/unit/ui/console/testrunner.rb:65:in `start_mediator'
from rubydir/1.8/test/unit/ui/console/testrunner.rb:39:in `start'
from rubydir/1.8/test/unit/ui/testrunnerutilities.rb:27:in `run'
from rubydir/1.8/test/unit/autorunner.rb:185:in `run'
from rubydir/1.8/test/unit/autorunner.rb:13:in `run'
from rubydir/1.8/test/unit.rb:283
from rubydir/1.8/test/unit.rb:283



Why does it happen?
It seem fairly normal to me to create Exceptions inheriting, well, from
Exception, and those are not going to be caught from Test::Unit. What am
I doing wrong?

As a sidenote, should'nt irb handle this?

irb(main):001:0> raise Exception.new
(irb):1:in `irb_binding': Exception (Exception)
from rubydir/1.8/irb/workspace.rb:52:in `irb_binding'

from rubydir/1.8/irb/workspace.rb:52

C:\Documents and Settings\gabriele\>


I'm running the latest praprog installer on windows xp pro.
ruby -v
ruby 1.8.2 (2004-07-29) [i386-mswin32]
 
A

Alexey Verkhovsky

I have a strange behaviour I can't explain

I have the same behavior on Linux. Don't have time too look at the root
cause right now, but it's nothing to do with the platform.

Alex


[alex@dhcp-89-2 alex]$ ruby -v
ruby 1.8.2 (2004-07-29) [i686-linux]
[alex@dhcp-89-2 alex]$ ruby boom.rb
Loaded suite boom
Started
Fboom.rb:6:in `testException': Exception (Exception)
from /usr/local/lib/ruby/1.8/test/unit/testcase.rb:70:in
`__send__'
from /usr/local/lib/ruby/1.8/test/unit/testcase.rb:70:in `run'
from /usr/local/lib/ruby/1.8/test/unit/testsuite.rb:32:in `run'
from /usr/local/lib/ruby/1.8/test/unit/testsuite.rb:31:in `each'
from /usr/local/lib/ruby/1.8/test/unit/testsuite.rb:31:in `run'
from /usr/local/lib/ruby/1.8/test/unit/testsuite.rb:32:in `run'
from /usr/local/lib/ruby/1.8/test/unit/testsuite.rb:31:in `each'
from /usr/local/lib/ruby/1.8/test/unit/testsuite.rb:31:in `run'
from
/usr/local/lib/ruby/1.8/test/unit/ui/testrunnermediator.rb:44:in
`run_suite'
from
/usr/local/lib/ruby/1.8/test/unit/ui/console/testrunner.rb:65:in
`start_mediator'
from
/usr/local/lib/ruby/1.8/test/unit/ui/console/testrunner.rb:39:in `start'
from
/usr/local/lib/ruby/1.8/test/unit/ui/testrunnerutilities.rb:27:in `run'
from /usr/local/lib/ruby/1.8/test/unit/autorunner.rb:185:in
`run'
from /usr/local/lib/ruby/1.8/test/unit/autorunner.rb:13:in `run'
from /usr/local/lib/ruby/1.8/test/unit.rb:283
from /usr/local/lib/ruby/1.8/test/unit.rb:283
 
B

Brian Candler

Test::Unit goes boom:

Loaded suite boom
Started
boom.rb:5:in `test_f': Exception (Exception)
from rubydir/1.8/test/unit/testcase.rb:70:in `__send__'

And if you look at line 70 of that file and its vicinity, you'll see that
Test::Unit only catches
AssertionFailedError
StandardError
ScriptError

However, normally you would never raise a raw Exception. From the
Programming Ruby book (tut_exceptions.html):

When you need to raise an exception, you can use one of the built-in
Exception classes, or you can create one of your own. If you create
your own, you might want to make it a subclass of StandardError or one
of its children. If you don't, your exception won't be caught by
default.

So the solution is to make your custom exception a subclass of StandardError

Regards,

Brian.
 
G

gabriele renzi

Brian Candler ha scritto:

<snip>

Thank you, I supposed Test::Unit worked this way. But I wonder why.
 

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,968
Messages
2,570,154
Members
46,701
Latest member
XavierQ83

Latest Threads

Top