# DB connection setup for Unit Testing

**URL:** <https://www.ruby-forum.com/t/db-connection-setup-for-unit-testing/113478>\
**Category:** Ruby\
**Created:** [August 31, 2007, 2:12pm UTC](https://www.ruby-forum.com/t/db-connection-setup-for-unit-testing/113478 "2007-08-31T14:12:10Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![shinji](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c6cbf5/32.png) [@shinji](https://www.ruby-forum.com/u/shinji)\
**Post date:** [August 31, 2007, 2:12pm UTC](https://www.ruby-forum.com/t/db-connection-setup-for-unit-testing/113478/1 "2007-08-31T14:12:10Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![shinji](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c6cbf5/32.png) [@shinji](https://www.ruby-forum.com/u/shinji)\
**Post date:** [September 5, 2007, 7:07pm UTC](https://www.ruby-forum.com/t/db-connection-setup-for-unit-testing/113478/2 "2007-09-05T19:07:41Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![shinji](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c6cbf5/32.png) [@shinji](https://www.ruby-forum.com/u/shinji)\
**Post date:** [September 5, 2007, 7:14pm UTC](https://www.ruby-forum.com/t/db-connection-setup-for-unit-testing/113478/3 "2007-09-05T19:14:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![shinji](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c6cbf5/32.png) [@shinji](https://www.ruby-forum.com/u/shinji)\
**Post date:** [September 8, 2007, 8:47am UTC](https://www.ruby-forum.com/t/db-connection-setup-for-unit-testing/113478/4 "2007-09-08T08:47:29Z")

</div>

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…
