Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Python and database unittests

Reply
Thread Tools

Python and database unittests

 
 
Daniel
Guest
Posts: n/a
 
      08-26-2008
Hello,

I'm writing an application that interacts with a database. As I think
about how to write the unittests, I want them to be able to run
without actually having to access a live database. The pattern that
best describes this is here:

http://martinfowler.com/eaaCatalog/serviceStub.html

I have found http://qualitylabs.org/pdbseed/, which helps with
unittests for a live database. This isn't what I'm after.

Does anyone know about a module that acts as a database stub for
python unittests?

Thanks,
Daniel
 
Reply With Quote
 
 
 
 
gordyt
Guest
Posts: n/a
 
      08-26-2008
Daniel I don't know if it would work for your situation or not, but if
you are using Python 2.5, you could use the now built-in sqlite3
module. If you didn't even want to create a temporary database file
you could use the special memory-only syntax like this:

>>> import sqlite3
>>> conn =sqlite3.connect(":memory:")
>>> # use the connection
>>> conn.close()


--gordy
 
Reply With Quote
 
 
 
 
Daniel
Guest
Posts: n/a
 
      08-26-2008
Hey gordy,

Thanks for the reply. I am actually using sqlite in part of my
application and I don't feel the need to stub that. Even though I
don't use the in memory option, it is still zero configuration so I
build up my database in each test then delete it.

However, the way I interact with the MySQL database is different than
the sqlite portion.

Thanks again,
Daniel


On Aug 26, 4:12*pm, gordyt <gor...@gmail.com> wrote:
> Daniel I don't know if it would work for your situation or not, but if
> you are using Python 2.5, you could use the now built-in sqlite3
> module. *If you didn't even want to create a temporary database file
> you could use the special memory-only syntax like this:
>
> >>> import sqlite3
> >>> conn =sqlite3.connect(":memory:")
> >>> # use the connection
> >>> conn.close()

>
> --gordy


 
Reply With Quote
 
alex23
Guest
Posts: n/a
 
      08-27-2008
Daniel <daniel.watr...@gmail.com> wrote:
> Does anyone know about a module that acts as a database stub for
> python unittests?


It's not database-specific, but the Mock module should help you here:

http://python-mock.sourceforge.net/

There's even an example on that page for mocking a database.
 
Reply With Quote
 
Marco Bizzarri
Guest
Posts: n/a
 
      08-27-2008
On Wed, Aug 27, 2008 at 4:55 AM, alex23 <> wrote:
> Daniel <daniel.watr...@gmail.com> wrote:
>> Does anyone know about a module that acts as a database stub for
>> python unittests?

>
> It's not database-specific, but the Mock module should help you here:
>
> http://python-mock.sourceforge.net/
>
> There's even an example on that page for mocking a database.
> --
> http://mail.python.org/mailman/listinfo/python-list
>




--
Marco Bizzarri
http://iliveinpisa.blogspot.com/
 
Reply With Quote
 
Marco Bizzarri
Guest
Posts: n/a
 
      08-27-2008
On Wed, Aug 27, 2008 at 4:55 AM, alex23 <> wrote:
> Daniel <daniel.watr...@gmail.com> wrote:
>> Does anyone know about a module that acts as a database stub for
>> python unittests?

>
> It's not database-specific, but the Mock module should help you here:
>
> http://python-mock.sourceforge.net/
>
> There's even an example on that page for mocking a database.
> --
> http://mail.python.org/mailman/listinfo/python-list
>


I strongly disagree on using mocks for a database; checking sequences
of SQL statement is fragile, painful, and leads you to frustration
when the actual SQL and the generated SQL do not match.

Regards
Marco

--
Marco Bizzarri
http://iliveinpisa.blogspot.com/
 
Reply With Quote
 
Marco Bizzarri
Guest
Posts: n/a
 
      08-27-2008
On Tue, Aug 26, 2008 at 11:35 PM, Daniel <> wrote:
> Hello,
>
> I'm writing an application that interacts with a database. As I think
> about how to write the unittests, I want them to be able to run
> without actually having to access a live database. The pattern that
> best describes this is here:
>
> http://martinfowler.com/eaaCatalog/serviceStub.html
>
> I have found http://qualitylabs.org/pdbseed/, which helps with
> unittests for a live database. This isn't what I'm after.
>
> Does anyone know about a module that acts as a database stub for
> python unittests?
>
> Thanks,
> Daniel
> --
> http://mail.python.org/mailman/listinfo/python-list
>



I think you're pointing to the wrong direction, if you want to make a
servicestub; the service should encapsulate your access to the
database (or whatever external resource you want to access), and,
after that, it should be transparent for you, more or less.

Regards
Marco


--
Marco Bizzarri
http://iliveinpisa.blogspot.com/
 
Reply With Quote
 
Simon Brunning
Guest
Posts: n/a
 
      08-27-2008
2008/8/27 alex23 <>:
> Daniel <daniel.watr...@gmail.com> wrote:
> It's not database-specific, but the Mock module should help you here:
>
> http://python-mock.sourceforge.net/
>
> There's even an example on that page for mocking a database.


There's a number of mocking modules for Python - my current favorite
is Mox. See <http://tinyurl.com/57hzr4>.

--
Cheers,
Simon B.

http://www.brunningonline.net/simon/blog/
 
Reply With Quote
 
Simon Brunning
Guest
Posts: n/a
 
      08-27-2008
2008/8/27 Marco Bizzarri <>:
> I strongly disagree on using mocks for a database; checking sequences
> of SQL statement is fragile, painful, and leads you to frustration
> when the actual SQL and the generated SQL do not match.


Clearly you need integration tests as well as unit tests, but the unit
tests ought to isolate the code under test, so stubbing out external
dependencies is the norm.

--
Cheers,
Simon B.

http://www.brunningonline.net/simon/blog/
GTalk: simon.brunning | MSN: small_values | Yahoo: smallvalues | Twitter: brunns
 
Reply With Quote
 
M.-A. Lemburg
Guest
Posts: n/a
 
      08-27-2008
On 2008-08-26 23:35, Daniel wrote:
> Hello,
>
> I'm writing an application that interacts with a database. As I think
> about how to write the unittests, I want them to be able to run
> without actually having to access a live database. The pattern that
> best describes this is here:
>
> http://martinfowler.com/eaaCatalog/serviceStub.html
>
> I have found http://qualitylabs.org/pdbseed/, which helps with
> unittests for a live database. This isn't what I'm after.
>
> Does anyone know about a module that acts as a database stub for
> python unittests?


I'm not sure how such a stub would help in testing. After all, you
want the unit tests to work against the real database in order to
find possible bugs in the way you use that database. A stub would
only hide possible bugs due to incorrect usage of database APIs
(or possibly even cause fake ones due to bugs in the stub).

When testing database applications, it's usually best to have
a separate test installation of the whole system which then
creates a test dataset at testing setup time and eventually
cleans up the database again after all tests have run.

For small tests and depending on the database backend, you can
often put all the setup, testing of the database into
a single transaction, so you don't even have to bother with
the removal step (just roll back your changes at the end of the
test).

--
Marc-Andre Lemburg
eGenix.com

Professional Python Services directly from the Source (#1, Aug 27 200
>>> Python/Zope Consulting and Support ... http://www.egenix.com/
>>> mxODBC.Zope.Database.Adapter ... http://zope.egenix.com/
>>> mxODBC, mxDateTime, mxTextTools ... http://python.egenix.com/

__________________________________________________ ______________________

:::: Try mxODBC.Zope.DA for Windows,Linux,Solaris,MacOSX for free ! ::::


eGenix.com Software, Skills and Services GmbH Pastor-Loeh-Str.48
D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
Registered at Amtsgericht Duesseldorf: HRB 46611
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
unittests with different parameters Ulrich Eckhardt Python 9 11-24-2010 02:10 PM
question about an exciting gotcha for unittests (and elsewhere) ... Cameron Simpson Python 3 04-23-2010 07:05 PM
Error on using require 'unittests/setup' Puneet Pattar Ruby 1 12-14-2009 09:46 AM
Global.asax.cs and unittests Peter Larsen [CPH] ASP .Net 3 09-10-2008 08:11 AM
Bugtracking & UnitTests == good? martinankerl at eml dot cc Ruby 13 07-11-2004 11:53 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57