Python and database unittests

D

Daniel

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
 
G

gordyt

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:

--gordy
 
D

Daniel

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
 
M

Marco Bizzarri

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.

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
 
M

Marco Bizzarri

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


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
 
S

Simon Brunning

2008/8/27 Marco Bizzarri said:
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.
(e-mail address removed)
http://www.brunningonline.net/simon/blog/
GTalk: simon.brunning | MSN: small_values | Yahoo: smallvalues | Twitter: brunns
 
M

M.-A. Lemburg

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 2008)________________________________________________________________________

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top