'<nil> is not true.' exception from my asserts

A

aidy

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
 
R

Robert Klemme

aidy said:
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
 
A

aidy

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
 
T

ts

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%
 
C

ChrisH

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
 
A

aidy

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
 
J

joesb

aidy said:
Chris wrote :

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.
 
A

aidy

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
 
J

joesb

aidy said:
Hi joesb


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?
 
A

aidy

joesb said:
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
 

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,982
Messages
2,570,186
Members
46,739
Latest member
Clint8040

Latest Threads

Top