DB connection setup for Unit Testing

I am trying to take a test-driven approach to a script I am writing.

The script needs to connect to SQLServer using DBI.
Currently it looks like this:

class BurndownTests < Test::Unit::TestCase

def setup
db = DBI.connect(“DBI:ODBC:driver={SQL
Server};Server=INPSESVER1;Database=RMT;Trusted_Connection=yes;”)
end

def teardown
db.disconnect
end

def test_connectrmtrack
pass
end

def test_export_schedule
pass
end

def test_format_datecols
pass
end

def test_count_resolutions
pass
end

def test_dumptofile
pass
end

end

The issue with this is that using setup like this, from what I
understand is used for each new test method I add. As I want to connect,
test the connection and then run tests which have a reliance on the
connection being established, how can I do a onetime setup and then test
this?

Somewhat lame, but this might do what you’re looking for:

class BurndownTests < Test::Unit::TestCase

@@db = nil
@@done_with_db = false

def setup
db =DBI.connect(…) if not @@db
end

def teardown
db.disconnect if @@done_with_db
end

def test_zzzzzz_should_be_the_last_test_run_due_to_zzzzzz
@@done_with_db = true
end

continue on with tests that use @@db

Jeff

Woops. Typos. Should have been:

class BurndownTests < Test::Unit::TestCase

@@db = nil
@@done_with_db = false

def setup
@@db = DBI.connect(…) if not @@db
end

def teardown
@@db.disconnect if @@done_with_db
end

def test_zzzzzz_should_be_the_last_test_run_due_to_zzzzzz
@@done_with_db = true
end

continue on with tests that use @@db

Jeff

On Sep 5, 2007, at 10:06 , Jeff - Burly S. wrote:

db.disconnect if @@done_with_db

end

This is what I use for such types of things:

$db = DBI…
at_exit { at_exit { $db.disconnect } } # guaranteed to run AFTER test/
unit is done

…stuff…