[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

' is not true.' exception from my asserts

aidy

6/12/2006 12:24:00 PM

I am having some serious problems with my unit tests.

below is a simple test

require 'watir'
require 'test/unit'
class TC_1 < Test::Unit::TestCase

def test_1


ie = Watir::IE.new
ie.goto('www.tnt.com')
if assert(ie.contains_text("zzzzzzzzzz")) # this is not going to be
there
p 'pass'
else
p 'fail'
end
rescue => e
puts(e.message + "\n" + e.backtrace.join("\n"))
ensure
ie.close
end

end

And this is the exception I get

'<nil> is not true.'

I really don't why this would be the case. Could anyone help?

Thanks

Aidy

9 Answers

Robert Klemme

6/12/2006 12:56:00 PM

0

aidy wrote:
> I am having some serious problems with my unit tests.
>
> below is a simple test
>
> require 'watir'
> require 'test/unit'
> class TC_1 < Test::Unit::TestCase
>
> def test_1
>
>
> ie = Watir::IE.new
> ie.goto('www.tnt.com')
> if assert(ie.contains_text("zzzzzzzzzz")) # this is not going to be
> there
> p 'pass'
> else
> p 'fail'
> end
> rescue => e
> puts(e.message + "\n" + e.backtrace.join("\n"))
> ensure
> ie.close
> end
>
> end
>
> And this is the exception I get
>
> '<nil> is not true.'
>
> I really don't why this would be the case. Could anyone help?

IIRC assert with throw if the arg is not true. You cannot test this
with if then else. ie.contains_text("zzzzzzzzzz") probably returns nil
and - alas, there you are.

Kind regards

robert

aidy

6/12/2006 3:01:00 PM

0

Robert Wrote
> You cannot test this with if then else.

But for some reason, if the the text is actually found

require 'watir'
require 'test/unit'
class TC_1 < Test::Unit::TestCase
def test_1
ie = Watir::IE.new
ie.goto('www.google.com')
if assert(ie.contains_text("Groups"))
p 'pass'
else
p 'fail'
end
rescue => e
puts(e.message + "\n" + e.backtrace.join("\n"))
ensure
ie.close
end
end

A 'fail' is written to the console.

If I remove assert

if (ie.contains_text("Groups")

a pass it written

would this method in assertions.rb

# Passes if boolean is true.
public
def assert(boolean, message=nil)
_wrap_assertion do
assert_block("assert should not be called with a block.") {
!block_given? }
assert_block(build_message(message, "<?> is not true.",
boolean)) { boolean }
end
end

return true, if the the first formal parameter is true?

Aidy

ts

6/12/2006 3:18:00 PM

0

>>>>> "a" == aidy <aidy.rutter@gmail.com> writes:

a> return true, if the the first formal parameter is true?

No, #assert return nil if the first parameter is true

moulon% cat b.rb
#!/usr/bin/ruby
require 'test/unit'

class TC_1 < Test::Unit::TestCase
def test_1
p "assert return #{assert(true).inspect}"
end
end
moulon%

moulon% ./b.rb
Loaded suite ./b
Started
"assert return nil"
.
Finished in 0.000568 seconds.

1 tests, 1 assertions, 0 failures, 0 errors
moulon%



--

Guy Decoux

Chris Hulan

6/12/2006 3:27:00 PM

0

Hi Aidy

I'd suggest you find some docs/example of unit testing. In my
experience you don't test the calls to 'assert'. You use the asserts
to allow the Test framework to provide you feedback on what is
passing/failing

Cheers

aidy

6/12/2006 4:37:00 PM

0

Chris wrote :
> In my experience you don't test the calls to 'assert'.

good point I have now seperated the test and assert. I need the assert
to make use of the Test::Unit, test_reporter.
require 'all_libs'
require 'test/unit'
class ST_LTD_3 < Test::Unit::TestCase

def test_st_ltd_3

string_to_search = "dddddd"

start_browser('http://gbahevm07l15:9081/wps/portal')
login('aidy', '12345')
goto_employee_search_list
search_employee('GERMANY', 'BERLIN')
click_filter
verify_result(string_to_search)
assert($ie.contains_text(string_to_search))
rescue => e
raise Test::Unit::AssertionFailedError,
"test failed: #{e.message}",
e.backtrace
ensure
log_out
close_window
end

end

Thanks
Aidy

joesb.coe9

6/12/2006 5:47:00 PM

0


aidy wrote:
> Chris wrote :
> > In my experience you don't test the calls to 'assert'.
>
> good point I have now seperated the test and assert. I need the assert
> to make use of the Test::Unit, test_reporter.
> require 'all_libs'
> require 'test/unit'
> class ST_LTD_3 < Test::Unit::TestCase
>
> def test_st_ltd_3
>
> string_to_search = "dddddd"
>
> start_browser('http://gbahevm07l15:9081/wps/portal')
> login('aidy', '12345')
> goto_employee_search_list
> search_employee('GERMANY', 'BERLIN')
> click_filter
> verify_result(string_to_search)
> assert($ie.contains_text(string_to_search))
> rescue => e
> raise Test::Unit::AssertionFailedError,
> "test failed: #{e.message}",
> e.backtrace
> ensure
> log_out
> close_window
> end
>
> end
>
> Thanks
> Aidy

You seems to be using UnitTest in a different way people usually use.
When using RUnit, you don't check the result of assertion yourself, nor
do you report the result of assertion yourself.

Normally your class only contains assertion; no reporting.

class ST_LTD_3 < Test::Unit::TestCase
def test_st_ltd_3
string_to_search = "dddddd"
begin
start_browser('http://gbahevm07l15:9081/wps/portal')
...
click_filter
assert($ie.contains_text(string_to_search))
ensure
log_out
close_window
end
end

Then run testcase with

require 'yourclass'
RUNIT::CUI::TestRunner.run(ST_LTD_3.suite)

Make sure you read the RUnit and understand how to use it first.

The best source is in documentation at the beginning of source code in
lib/ruby/1.8/test/unit.rb.

See testrunner and testsuite section.

aidy

6/13/2006 9:16:00 AM

0

Hi joesb

>You seems to be using UnitTest in a different way people usually use.
>When using RUnit, you don't check the result of assertion yourself, nor
>do you report the result of assertion yourself.

Yes, I run my tests from suites

class TS_Common_Sales_E2
suite = Test::Unit::TestSuite.new

suite << ST_LTD_3.suite
suite << ST_MSR_1.suite

.....
end

However, the problem, I am having is that test code and production code
are not equivalent. An exception thrown for a developer may be fine,
but an automated tester needs to be told where that failure occurs.So I
find myself in the difficult situation of having a verification point,
an assert and exception handling

verify_result(string_to_search)
assert($ie.contains_text(string_to_search))
rescue => e
raise Test::Unit::AssertionFailedError,
"test failed: #{e.message}",
e.backtrace
ensure
log_out
close_window


Thanks

Aidy

joesb.coe9

6/13/2006 9:58:00 AM

0


aidy wrote:
> Hi joesb
>
> >You seems to be using UnitTest in a different way people usually use.
> >When using RUnit, you don't check the result of assertion yourself, nor
> >do you report the result of assertion yourself.
>
> Yes, I run my tests from suites
>
> class TS_Common_Sales_E2
> suite = Test::Unit::TestSuite.new
>
> suite << ST_LTD_3.suite
> suite << ST_MSR_1.suite
>
> .....
> end
>
> However, the problem, I am having is that test code and production code
> are not equivalent. An exception thrown for a developer may be fine,
> but an automated tester needs to be told where that failure occurs.So I
> find myself in the difficult situation of having a verification point,
> an assert and exception handling
>
> verify_result(string_to_search)
> assert($ie.contains_text(string_to_search))
> rescue => e
> raise Test::Unit::AssertionFailedError,
> "test failed: #{e.message}",
> e.backtrace
> ensure
> log_out
> close_window
>
>

Huh? I'm getting confused here. So above is part of your program?

I think it's unusual to have the production code directly using
UnitTest.

UnitTest code is usually separate from production code. Unit tester
uses different code to test/assert/report from the code use in
production. The production code should never do the assert themselves.

And when assert of RUnit fail, doesn't it automatically generate
backtrace in report?

> Thanks
>
> Aidy

aidy

6/13/2006 10:35:00 AM

0


joesb wrote:

> Huh? I'm getting confused here. So above is part of your program?
>
> I think it's unusual to have the production code directly using
> UnitTest.

I am an automated tester and my code tests production code - it is not
production code.
What I think I am trying to convey is that their are standard
guidelines for developer code ie: the seperation of test and
production code; however a tester would not create test classes to test
test classes. This is why I am finding the organisation of the system
test code problematic.

Cheers