fixture is a python module for loading and referencing test data
It provides several utilities for achieving a fixed state when testing Python programs. Specifically, these utilities setup / teardown databases and work with temporary file systems. This is useful for testing and came about to fulfill stories like these:
For more info, this concept is explained in the wikipedia article, Test Fixture.
Database testing is easier than I had thought. Kumar’s fixture helps provide a stable database to drive testing. – Steven F. Lott, summarizing PyCon 2007
Using the easy_install command:
easy_install fixture
If you want to use decorators like @fixture.with_data() you need nose installed, so run:
easy_install 'fixture[decorators]'
For more variants on the easy_install command, such as installing database libraries, see Using LoadableFixture.
The source is available from the fixture package or the fixture subversion repository and the following command works with or without setuptools:
cd /path/to/source python setup.py install
Note
The above commands may require root access
If you want to try out fixture before installing it globally, you may be interested in virtualenv.
At the moment fixture is only tested on Python 2.4 and 2.5 so it may or may not work with other versions. The module does not depend on external libraries for its core functionality but to do something interesting you will need one of several 3rd party libraries (explained later in the documentation).
There are a couple ways to test a database-backed application. You can create mock objects and concentrate entirely on unit testing individual components without testing the database layer itself, or you can simply load up sample data before you run a test. Thanks to sqlite in-memory connections, the latter may be more efficient than you think.
But it’s easy enough to insert data line by line in code, right? Or simply load a SQL file? Yes, but this has two major downsides: you often have to worry about and manage complex chains of foreign keys manually; and when referencing data values later on, you either have to copy / paste the values or pass around lots of variables.
The fixture module simplifies this by breaking the process down to two independent components:
Fixture can be used to load DataSet objects into SQLAlchemy, SQLObject, or Google Datastore backends (more on that). For a complete end-to-end example see Using Fixture To Test A Pylons + SQLAlchemy App or Using Fixture To Test A Google App Engine Site. This should help you understand how to fit fixture into a finished app.
Now, on to the knitty gritty details ...
fixture is a reimplementation of testtools.fixtures which was my first attempt at the idea. As of July 2008 the fixture module is used in several test suites by my development teams at work. One project is a data migration (ETL) framework using SQLObject and another is an in-house Pylons + Ext JS application using SQLAlchemy and Elixir. It has been open source for a while but with only a few contributors so I’m not sure how much it’s used outside of that.
Read on for info on submitting issues. For general discussion about fixture, how about the Testing In Python mailing list? Otherwise, you can try me here: kumar.mcmillan @gmail.com
Please submit any issues, patches, failing tests, and/or bugs using the Issue Tracker on the fixture project site. If your code is used, your contribution will be documented.