# DB connection setup for Unit Testing

**URL:** <https://rubytalk.org/t/db-connection-setup-for-unit-testing/40292>\
**Category:** ruby-talk\
**Created:** [31 August 2007 12:12 UTC](https://rubytalk.org/t/db-connection-setup-for-unit-testing/40292 "2007-08-31T12:12:15Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Max\_Russell](https://avatars.discourse-cdn.com/v4/letter/m/e5b9ba/32.png) [@Max\_Russell](https://rubytalk.org/u/Max_Russell)\
**Post date:** [31 August 2007 12:12 UTC](https://rubytalk.org/t/db-connection-setup-for-unit-testing/40292/1 "2007-08-31T12:12:15Z")

</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

&nbsp;&nbsp;def setup  
&nbsp;&nbsp;&nbsp;&nbsp;db = DBI.connect("DBI:ODBC:driver={SQL  
Server};Server=INPSESVER1;Database=RMT;Trusted\_Connection=yes;")  
&nbsp;&nbsp;end

&nbsp;&nbsp;def teardown  
&nbsp;&nbsp;&nbsp;&nbsp;db.disconnect  
&nbsp;&nbsp;end

&nbsp;&nbsp;def test\_connectrmtrack  
&nbsp;&nbsp;&nbsp;&nbsp;pass  
&nbsp;&nbsp;end

&nbsp;&nbsp;def test\_export\_schedule  
&nbsp;&nbsp;&nbsp;&nbsp;pass  
&nbsp;&nbsp;end

&nbsp;&nbsp;def test\_format\_datecols  
&nbsp;&nbsp;&nbsp;&nbsp;pass  
&nbsp;&nbsp;end

&nbsp;&nbsp;def test\_count\_resolutions  
&nbsp;&nbsp;&nbsp;&nbsp;pass  
&nbsp;&nbsp;end

&nbsp;&nbsp;def test\_dumptofile  
&nbsp;&nbsp;&nbsp;&nbsp;pass  
&nbsp;&nbsp;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?

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Jeff\_Burly\_Systems](https://avatars.discourse-cdn.com/v4/letter/j/e19b73/32.png) [@Jeff\_Burly\_Systems](https://rubytalk.org/u/Jeff_Burly_Systems)\
**Post date:** [5 September 2007 17:06 UTC](https://rubytalk.org/t/db-connection-setup-for-unit-testing/40292/2 "2007-09-05T17:06:54Z")

</div>

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

class BurndownTests \< Test::Unit::TestCase

&nbsp;&nbsp;@@db = nil  
&nbsp;&nbsp;@@done\_with\_db = false

&nbsp;&nbsp;def setup  
&nbsp;&nbsp;&nbsp;&nbsp;db =DBI.connect(.......) if not @@db  
&nbsp;&nbsp;end

&nbsp;&nbsp;def teardown  
&nbsp;&nbsp;&nbsp;&nbsp;db.disconnect if @@done\_with\_db  
&nbsp;&nbsp;end

&nbsp;&nbsp;def test\_zzzzzz\_should\_be\_the\_last\_test\_run\_due\_to\_zzzzzz  
&nbsp;&nbsp;&nbsp;&nbsp;@@done\_with\_db = true  
&nbsp;&nbsp;end

&nbsp;&nbsp;# continue on with tests that use @@db ....

Jeff

> **···**
>
> On 8/31/07, Max Russell \<thedossone@gmail.com\> wrote:
> 
> > 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
> > 
> > &nbsp;&nbsp;def setup  
> > &nbsp;&nbsp;&nbsp;&nbsp;db = DBI.connect("DBI:ODBC:driver={SQL  
> > Server};Server=INPSESVER1;Database=RMT;Trusted\_Connection=yes;")  
> > &nbsp;&nbsp;end
> > 
> > &nbsp;&nbsp;def teardown  
> > &nbsp;&nbsp;&nbsp;&nbsp;db.disconnect  
> > &nbsp;&nbsp;end
> > 
> > &nbsp;&nbsp;def test\_connectrmtrack  
> > &nbsp;&nbsp;&nbsp;&nbsp;pass  
> > &nbsp;&nbsp;end
> > 
> > &nbsp;&nbsp;def test\_export\_schedule  
> > &nbsp;&nbsp;&nbsp;&nbsp;pass  
> > &nbsp;&nbsp;end
> > 
> > &nbsp;&nbsp;def test\_format\_datecols  
> > &nbsp;&nbsp;&nbsp;&nbsp;pass  
> > &nbsp;&nbsp;end
> > 
> > &nbsp;&nbsp;def test\_count\_resolutions  
> > &nbsp;&nbsp;&nbsp;&nbsp;pass  
> > &nbsp;&nbsp;end
> > 
> > &nbsp;&nbsp;def test\_dumptofile  
> > &nbsp;&nbsp;&nbsp;&nbsp;pass  
> > &nbsp;&nbsp;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?  
> > --  
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Jeff\_Burly\_Systems](https://avatars.discourse-cdn.com/v4/letter/j/e19b73/32.png) [@Jeff\_Burly\_Systems](https://rubytalk.org/u/Jeff_Burly_Systems)\
**Post date:** [5 September 2007 17:14 UTC](https://rubytalk.org/t/db-connection-setup-for-unit-testing/40292/3 "2007-09-05T17:14:09Z")

</div>

Woops. Typos. Should have been:

class BurndownTests \< Test::Unit::TestCase

&nbsp;&nbsp;@@db = nil  
&nbsp;&nbsp;@@done\_with\_db = false

&nbsp;&nbsp;def setup  
&nbsp;&nbsp;&nbsp;&nbsp;@@db = DBI.connect(.......) if not @@db  
&nbsp;&nbsp;end

&nbsp;&nbsp;def teardown  
&nbsp;&nbsp;&nbsp;&nbsp;@@db.disconnect if @@done\_with\_db  
&nbsp;&nbsp;end

&nbsp;&nbsp;def test\_zzzzzz\_should\_be\_the\_last\_test\_run\_due\_to\_zzzzzz  
&nbsp;&nbsp;&nbsp;&nbsp;@@done\_with\_db = true  
&nbsp;&nbsp;end

&nbsp;&nbsp;# continue on with tests that use @@db ....

Jeff

> **···**
>
> On 9/5/07, Jeff - Burly Systems \<jeff.burly@gmail.com\> wrote:
> 
> > Somewhat lame, but this might do what you're looking for:
> > 
> > class BurndownTests \< Test::Unit::TestCase
> > 
> > &nbsp;&nbsp;@@db = nil  
> > &nbsp;&nbsp;@@done\_with\_db = false
> > 
> > &nbsp;&nbsp;def setup  
> > &nbsp;&nbsp;&nbsp;&nbsp;db =DBI.connect(.......) if not @@db  
> > &nbsp;&nbsp;end
> > 
> > &nbsp;&nbsp;def teardown  
> > &nbsp;&nbsp;&nbsp;&nbsp;db.disconnect if @@done\_with\_db  
> > &nbsp;&nbsp;end
> > 
> > &nbsp;&nbsp;def test\_zzzzzz\_should\_be\_the\_last\_test\_run\_due\_to\_zzzzzz  
> > &nbsp;&nbsp;&nbsp;&nbsp;@@done\_with\_db = true  
> > &nbsp;&nbsp;end
> > 
> > &nbsp;&nbsp;# continue on with tests that use @@db ....
> > 
> > Jeff
> > 
> > On 8/31/07, Max Russell \<thedossone@gmail.com\> wrote:  
> > \> 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?  
> > \> --  
> > \> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).  
> > \>  
> > \>

---

<div class="post-metadata">

**Author:** ![Ryan\_Davis1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/ryan_davis1/32/1848_2.png) [@Ryan\_Davis1](https://rubytalk.org/u/Ryan_Davis1)\
**Post date:** [8 September 2007 06:46 UTC](https://rubytalk.org/t/db-connection-setup-for-unit-testing/40292/4 "2007-09-08T06:46:21Z")

</div>

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

> **···**
>
> On Sep 5, 2007, at 10:06 , Jeff - Burly Systems wrote:
> 
> > class BurndownTests \< Test::Unit::TestCase
> > 
> > &nbsp;&nbsp;@@db = nil  
> > &nbsp;&nbsp;@@done\_with\_db = false
> > 
> > &nbsp;&nbsp;def setup  
> > &nbsp;&nbsp;&nbsp;&nbsp;db =DBI.connect(.......) if not @@db  
> > &nbsp;&nbsp;end
> > 
> > &nbsp;&nbsp;def teardown  
> > &nbsp;&nbsp;&nbsp;&nbsp;db.disconnect if @@done\_with\_db  
> > &nbsp;&nbsp;end
