Since you are using Ruby 1.9.3, the line
require 'test/unit'
Will require the Minitest gem. In contrast, in Ruby 1.8.7, this would
include the Test::Unit gem.
If you do a rescue without a type specified, the default type is
StandardError, which is a subclass of Exception.
In Minitest, when an assertion fails, a MiniTest::Assertion is thrown. This
is a subclass of Exception rather than StandardError. Hence it will not be
caught by the rescue.
You need to add the exception type:
require 'test/unit'
class TC_Sample < Test::Unit::TestCase
def test_01
begin
assert(false)
rescue MiniTest::Assertion
p "Batdog to the Rescue!"
end
end
end
- Justin Ko
On Wednesday, December 19, 2012 10:35:26 AM UTC-5, captin wrote:
>
> Thank you for your reply and for taking the time to assist me. As I stated
> in my OP, I'm new to watir. I've come across RSpec and Cucumber many times
> as I've been learning but have yet to dig into them. I will most likely be
> evaluating those in the near future, but I wanted to understand the
> fundamentals first and maybe build my own basic test framework.
>
> I wasn't able to get your simplified example to work for me. Here's my
> ruby version:
>
> ruby 1.9.3p327 (2012-11-10) [i386-mingw32]
>
> When I run your unaltered example, it gives the following output:
>
> Run options:
> # Running tests:
> E
> Finished tests in 0.002000s, 500.0000 tests/s, 500.0000 assertions/s.
> 1) Error:
> test_01(TC_Sample):
> NameError: uninitialized constant Test::Unit::AssertionFailedError
> test1.rb:22:in `rescue in test_01'
> test1.rb:20:in `test_01'
> 1 tests, 1 assertions, 0 failures, 1 errors, 0 skips
>
> When I comment out the Test::Unit::AssertionFailedError so the rescue is
> simplified, it seems to skip the rescue just like my original example did:
>
> Run options:
> # Running tests:
> F
> Finished tests in 0.002000s, 500.0000 tests/s, 500.0000 assertions/s.
> 1) Failure:
> test_01(TC_Sample) [test1.rb:21]:
> Failed assertion, no message given.
> 1 tests, 1 assertions, 1 failures, 0 errors, 0 skips
>
> Do you think the difference in behavior is due to the Ruby versions? Or
> maybe due to differences in our gem versions? Here's my gem list:
>
> *** LOCAL GEMS ***
> addressable (2.3.2)
> bigdecimal (1.1.0)
> builder (3.1.4)
> childprocess (0.3.6)
> commonwatir (4.0.0)
> ffi (1.2.0 x86-mingw32)
> firewatir (1.9.4)
> hoe (3.3.1)
> io-console (0.3)
> json (1.5.4)
> libwebsocket (0.1.6.1)
> logger (1.2.8)
> mini_magick (3.2.1)
> minitest (2.5.1)
> multi_json (1.3.7)
> nokogiri (1.5.5 x86-mingw32)
> rake (0.9.2.2)
> rautomation (0.7.3)
> rdoc (3.9.4)
> rubyzip (0.9.9)
> s4t-utils (1.0.4)
> selenium-webdriver (2.26.0)
> subexec (0.0.4)
> test-unit (2.5.3)
> user-choices (1.1.6.1)
> watir (4.0.2 x86-mingw32)
> watir-classic (3.3.0)
> watir-webdriver (0.6.1)
> websocket (1.0.4)
> win32-api (1.4.8 x86-mingw32)
> win32-process (0.7.0)
> win32screenshot (1.0.7)
> windows-api (0.4.2)
> windows-pr (1.2.2)
> xml-simple (1.1.2)
>
> Thanks again for your help.
>
> Captin
>
> On Wednesday, December 19, 2012 5:15:07 AM UTC-6, Željko Filipin wrote:
>
>
>> Rescuing exceptions so you can log the error message or take a screen
>> shot is the wrong way to do it. My test/unit-fu is pretty rusty, but you
>> should be able to create custom formatter (at least that is how I would do
>> it in RSpec or Cucumber). Is there a reason you are using test/unit instead
>> of RSpec or Cucumber?
>>
>> That said, I have simplified the code that you have posted a bit, and it
>> rescues the exception on my machine.
>>
>> require 'test/unit'
>> class TC_Sample < Test::Unit::TestCase
>> def test_01
>> begin
>> assert(false)
>> rescue Test::Unit::AssertionFailedError
>> puts "Batdog to the Rescue!"
>> end
>> end
>> end
>>
>> The script rescues the exception if I use just "rescue" or "rescue
>> Test::Unit::AssertionFailedError".
>>
>> $ ruby test.rb
>> Loaded suite test
>> Started
>> Batdog to the Rescue!
>> .
>> Finished in 0.017473 seconds.
>>
>> 1 tests, 1 assertions, 0 failures, 0 errors
>>
>> $ ruby -v
>> ruby 1.8.7 (2012-02-08 patchlevel 358) [universal-darwin11.0]
>>
>> What version of Ruby are you using?
>>
>> Željko
>> --
>> https://leanpub.com/watirbook
>>
>
--
Before posting, please read http://watir.com/support. In short: search before
you ask, be nice.
[email protected]
http://groups.google.com/group/watir-general
[email protected]