[TIP] Fixtures

Kumar McMillan kumar.mcmillan at gmail.com
Tue Apr 7 08:41:48 PDT 2009

On Tue, Apr 7, 2009 at 10:08 AM, David Christian
<david.christian at gmail.com> wrote:
> Does anyone know of a fixture library that makes it easy to dump
> database(s) and files and restore them, based on an assigned name?
> I'd like to be able to say, here are my (postgres|mysql|sqlite)
> databases for this test and the files I care about are under this
> directory.  Please store them and give them this name.
> Later in the same testsuite run or between testsuite runs I'd like to
> be able to restore them to that state.
> I've seen "fixture" but on initial viewing it looks like it would
> require me to change my current process quite a bit, where it seems
> like I should be able to just say "record the current state" w/o
> having to massage the data into another format...

Currently, the fixture command produces data when you feed it a single
SQL query; that creates python files suitable for reproducing the data
returned by the query in a test.  There are quite a few limitations to
that (i.e. what if you need to query multiple tables?).  Also, there
are a lot of things that can go wrong with the introspection needed
for this to happen and as the author of fixture I can tell you that
this command is a weak feature of the library.

I had a test suite once where I wanted to make a lot of functional,
black box tests of data processing scripts.  I'd run the fixture
command to get a data snapshot and save that in the test suite.  Then
the test would run the script and assert the data was munged
accordingly.  While most of these functional tests were useful and
easy to *create* they were hard to *maintain*.  Since they were so
easy to create I also became lazy and in hindsight a lot of things I
ended up testing (i.e. incorrect command usage) would have been better
tested with unit tests.

So my lesson learned was whenever you need to set up a database and
insert data for a test you should try to test it another way first.
If you can make the same test without needing to set up data then you
just found a better way to test.  Mock objects are useful for this but
are often misunderstood, lots of pitfalls.

Sometimes you really do need to set up data for a test though.  I'm
just saying it should be a last resort, IMHO.

> Anyone have a tool they like for this?
> Dave
> _______________________________________________
> testing-in-python mailing list
> testing-in-python at lists.idyll.org
> http://lists.idyll.org/listinfo/testing-in-python

More information about the testing-in-python mailing list