Test/unit assertions

88 views
Skip to first unread message

jas.s...@hotmail.com

unread,
Dec 10, 2007, 2:54:27 PM12/10/07
to Watir General
All,

I am new to test/unit, and I have a couple of questions that I hope
you can assist me with. First, when an assertion fails, the script
automatically stops running. Is there a way to have an assertion
fail, and continue running the rest of the test? What I really need
is a way to say, if an assertion passes do 'x', and if the assertion
fails do 'y', without the script stopping completely if 'y' is
prompted. What kind of code can I write to interpret the result of
each assertion? Thanks in advance for your assistance.

Shelton

tcfodor

unread,
Dec 10, 2007, 3:25:03 PM12/10/07
to Watir General
I may not understand the test/unit assertions well enough, but I ran
into the same problem. Rather than spending a lot of time trying to
figure it out, I decided to use the watir assertions instead. My
checks look something like this:

verify(($ie.link(:text, "OK").exists?), message="OK link doesn't
exist.")

If the condition fails, the message is displayed (in my case, just to
the command prompt where I'm running my tests, but I think they could
be written to logs as well).

Hope this helps!

-Tiffany

Jason Shelton

unread,
Dec 10, 2007, 4:02:02 PM12/10/07
to watir-...@googlegroups.com
Will the '.selected?' and '.value ==' assertions work with this?  Also, how do you output this to a .txt file?  Thanks again.
 
Shelton
 
 

> Date: Mon, 10 Dec 2007 12:25:03 -0800
> Subject: [wtr-general] Re: Test/unit assertions
> From: tcf...@yahoo.com
> To: watir-...@googlegroups.com

marekj

unread,
Dec 10, 2007, 4:36:02 PM12/10/07
to watir-...@googlegroups.com
Take a look at Watir::TestCase class sublcassing Test::Unit::TestCase
and Watir::Asserttions

You can use 'verify' instead of assert.

here is some chunky bacon....

class TC_xxx < Watir::TestCase
  def test_xxx
    verify_equal(expected,actual)
    verify(expression, 'message')
    verify_match(pattern, string,'message')
  end
end


and here is more chunky bacon grilled with cheese....

require 'watir'
require 'watir/testcase'

class TC_x < Watir::TestCase
 
  def test_01_verify
    verify(1==2,'bla')
    verify(2==4,'bla')
  end
  def test_02_assert
    assert(1==2,'bla')
    assert(2==4, 'bla')
  end
end

run the above and notice two verifies will be executed but only one assert....

marekj

www.testr.us | functional test objects modeling

tcfodor

unread,
Dec 10, 2007, 4:47:27 PM12/10/07
to Watir General
In my experience, the code works like this:

verify((condition), message="text")

and you can use whatever you want for the condition. There is also a
verify_equal method that works pretty much the same way. I see there
is also a verify_match method, but I've never used it. Also, I've
been able to use various string methods in the error message.

Here are some more examples:

verify_equal(transactionReasonId, policy_TransactionReasonId,
message=("Policy_TransactionReasonId is incorrect"))

verify(($ie.text_field(:name, "mailingAddress").value != "123 Bad
Address"), message="Address changes were not reset after canceling a
Change Mailing Address modification for account: " + @accountName +
".")

verify(($ie.checkbox(:name, "sendDocumentation").checked?),
message="The Send Documentation box is not enabled by default for
account: " + $accountName + ".")

I assume that you could use .selected? the same way.

I haven't done much with logging yet, but I've seen in other posts,
that you can redirect the output from your script to a text file like
this:

watir_test.rb > file_name.txt

I think I'm going to give that a try now. I kind-of like seeing the
status real-time, though.

-Tiffany



On Dec 10, 2:02 pm, Jason Shelton <jas.shel...@hotmail.com> wrote:
> Will the '.selected?' and '.value ==' assertions work with this? Also, how do you output this to a .txt file? Thanks again.
>
> Shelton
>
> > Date: Mon, 10 Dec 2007 12:25:03 -0800> Subject: [wtr-general] Re: Test/unit assertions> From: tcfo...@yahoo.com> To: watir-...@googlegroups.com> > > I may not understand the test/unit assertions well enough, but I ran> into the same problem. Rather than spending a lot of time trying to> figure it out, I decided to use the watir assertions instead. My> checks look something like this:> > verify(($ie.link(:text, "OK").exists?), message="OK link doesn't> exist.")> > If the condition fails, the message is displayed (in my case, just to> the command prompt where I'm running my tests, but I think they could> be written to logs as well).> > Hope this helps!> > -Tiffany> > On Dec 10, 12:54 pm, jas.shel...@hotmail.com wrote:> > All,> >> > I am new to test/unit, and I have a couple of questions that I hope> > you can assist me with. First, when an assertion fails, the script> > automatically stops running. Is there a way to have an assertion> > fail, and continue running the rest of the test? What I really need> > is a way to say, if an assertion passes do 'x', and if the assertion> > fails do 'y', without the script stopping completely if 'y' is> > prompted. What kind of code can I write to interpret the result of> > each assertion? Thanks in advance for your assistance.> >> > Shelton> _________________________________________________________________
>
> Connect and share in new ways with Windows Live.http://www.windowslive.com/connect.html?ocid=TXT_TAGLM_Wave2_newways_...

Jason Shelton

unread,
Dec 11, 2007, 9:28:32 AM12/11/07
to watir-...@googlegroups.com
I tried the  verify(($ie.checkbox(:name, "sendDocumentation").checked?),
message="x")  method, and got the error:
 

NoMethodError: undefined method `verify' for #<Testing:0x2e0917c>

xxxx.rb:117:in `test_encDetailScreen'
 
Any ideas what the issue is?  Thanks in advance. (I am using Watir 1.5.3)
 
Shelton

> Date: Mon, 10 Dec 2007 13:47:27 -0800

> Subject: [wtr-general] Re: Test/unit assertions

marekj

unread,
Dec 11, 2007, 10:20:16 AM12/11/07
to watir-...@googlegroups.com
I suspect 2 things.
1. your Testing class needs to subclass Watir::TestCase
2. you need to to require 'watir/testcase' at the top of your file.

marekj
Reply all
Reply to author
Forward
0 new messages