[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

DB connection setup for Unit Testing

Max Russell

8/31/2007 12:12:00 PM

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

3 Answers

Jeff - Burly Systems

9/5/2007 5:07:00 PM

0

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

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

Jeff - Burly Systems

9/5/2007 5:14:00 PM

0

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

Ryan Davis

9/8/2007 6:46:00 AM

0


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

> 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

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