Navigation

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:

I want to load data into a test database and easily reference that data when making assertions.

I want data linked by foreign key to load automatically and delete without integrity error.

I want to reference linked rows by meaningful names, not hard-coded ID numbers.

I don’t want to worry about auto-incrementing sequences.

I want to recreate an environment (say, for a bug) by querying a database for real data.

I want to test with files in a temporary, transparent file system.

For more info, this concept is explained in the wikipedia article, Test Fixture.

At the moment fixture is only tested on Python 2.4, 2.5, and 2.6 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 was originally a reimplementation of testtools.fixtures, the first attempt at the idea. As of September 2009 the fixture module is used in several test suites by the author’s development team at work using the SQLAlchemy backend. Several contributors seem to be using it in their own projects as well for various backends like Google App Engine and Django. The project started in 2007.