Accessing global variable in Unit Tests

Hi,

I’m having problems using a global variable defined in the my test
class, which then is referenced in the libraries file. I’m using Ruby
1.9.3p392 and test-unit 2.5.4

Here is the code that runs the tests:

require ‘rubygems’
gem ‘test-unit’
require ‘test/unit’
require ‘ci/reporter/rake/test_unit_loader’
load ‘…/lib/functions.rb’
require ‘watir’

class Test_002 < Test::Unit::TestCase

include Functions

class << self
def startup
@browser = Watir::Browser.new :ie
@browser.speed = :fast
end

def shutdown
  @browser.close
end

end

def test_001_login
login(‘some_url’, ‘some_user’, ‘some_passw’)
end
end

And this is part of the library that contains the login function:

require ‘rubygems’

module Functions

def login(url, user, passw)
@browser.goto(url)

end
end

This is the output:
Started
E

Error: test_001_login(Test_002)
NoMethodError: undefined method `goto’ for nil:NilClass
(…)
23: end
24:
25: def test_001_login
=> 26: login(‘some_url’, ‘some_user’, ‘some_passw’)
27: end
28:
29: end

Finished in 3.0043 seconds.

1 tests, 0 assertions, 0 failures, 1 errors, 0 pendings, 0 omissions, 0
notifications
0% passed

0.33 tests/s, 0.00 assertions/s

Any ideas?

On Mon, Apr 8, 2013 at 6:33 PM, T- Di [email protected] wrote:

require ‘test/unit’

I think this should be called “setup” and be an instance method and not
a
class method.

  @browser = Watir::Browser.new :ie
  @browser.speed = :fast
end

def shutdown

This should be called “teardown”.

And this is part of the library that contains the login function:

 24:

1 tests, 0 assertions, 0 failures, 1 errors, 0 pendings, 0 omissions, 0
notifications
0% passed

0.33 tests/s, 0.00 assertions/s

Any ideas?

@browser is not initialized. See

http://en.wikibooks.org/wiki/Ruby_Programming/Unit_testing

Kind regards

robert

On Tue, Apr 9, 2013 at 10:13 AM, T- Di [email protected] wrote:

My understanding was that the new test-unit uses the startup
initialization and shutdown methods.

I don’t think so. Also, the “new” test-unit seems tobe the old
Test::Unit
refurbished and Ruby 1.9* now comes with minitest:
http://rubygems.org/gems/test-unit
http://rubydoc.info/gems/test-unit/2.5.4/frames

I used the standard Test::Unit (minitest) and it worked with #setup and
#teardown as indicated above.

In any case I’m starting to think that the approach I’m using is not the

best way to handle this. What I’m trying to do is to setup the execution
of several Watir test cases, using each unit test as a contained for a
small subset of steps & validations. I know this is not the objective of
unit tests, but couldn’t find a better way to handle this in a
continuous integration environment such as Hudson.

Well, if it’s a test then why not?

Kind regards

robert

On Apr 9, 2013, at 01:48 , Robert K. [email protected]
wrote:

I used the standard Test::Unit (minitest) and it worked with #setup and
#teardown as indicated above.

1.9+ ships with both minitest and a test/unit shim on top of minitest.
They’re (very much) not the same tho.

My understanding was that the new test-unit uses the startup
initialization and shutdown methods.

In any case I’m starting to think that the approach I’m using is not the
best way to handle this. What I’m trying to do is to setup the execution
of several Watir test cases, using each unit test as a contained for a
small subset of steps & validations. I know this is not the objective of
unit tests, but couldn’t find a better way to handle this in a
continuous integration environment such as Hudson.