Skip to main content

DB-TESTTOOLS

This package contains test fixtures and resources that bring up an isolated database and SQLAlchemy test fixtures, so that your Python unit tests will run without interfering with each other when you are using the SQLAlchemy ORM.

The database is initialised once at the start of a test process run, as is the session fixture. The session fixture ensures that any commits do not permanently commit, and rolls back the database to a clean state after each test completes.

Requirements

Python 3.10 and beyond should work. SQLAlchemy 2.0 or later is required.

Upgrading from SQLAlchemy 1.4 versions

Older releases supported SQLAlchemy’s legacy (v1) API, which was the default unless future=True was passed. This is no longer supported:

  • The future argument is deprecated and ignored, and emits a DeprecationWarning if passed. Remove it from your DatabaseResource, SessionFixture and engine fixture arguments.

  • The test session is always bound to a Connection that is rolled back at the end of each test, rather than to the Engine. If your code expects session.bind or session.get_bind() to be an Engine, use session.get_bind().engine instead.

Quickstart

Install with pip:

pip install db-testtools

Example base test class:

class DBTestCase(testresources.ResourcedTestCase, testtools.TestCase):
    """Base class for all DB tests.

   Brings up a temporary database and gives each test its own session.
   """

   # These are resources that stay active for the entire
   # duration of all the tests being run.
   db_fixture = DatabaseResource(
       ModelBase,
       'myproject.models',
   )
   resources = [('database', db_fixture)]

   def setUp(self):
       super().setUp()

       self.session_fixture = SessionFixture(self.database)
       self.useFixture(self.session_fixture)
       # The session itself.
       self.session = self.session_fixture.session
       # The session factory.
       self.Session = self.session_fixture.Session

This base test class will start a SQLite-based database by default and inject self.session as the SQLAlchemy session and self.Session as the SQLAlchemy session factory.

If you need to use a different database, then you can either:
  • pass the engine_fixture_name parameter to DatabaseResource

  • set an environment variable TEST_ENGINE_FIXTURE

with the name of the engine fixture to use. Currently two are available:

  • SqliteMemoryFixture

  • PostgresContainerFixture

Engine drivers

Currently the two drivers mentioned above are implemented. The SQLite fixture implements a simple in-memory database which is completely dropped and re-instated on every test.

The PostgresContainerFixture starts its own Postgres instance in a local Docker container. Therefore you must have Docker installed before using this fixture. The Postgres image used by default is 16.3-alpine, but this fixture is known to work all the way back to v11.

If you are already running inside Docker you will need to start the container with –network-“host” so that 127.0.0.1 routes to the started PG containers. You will need to do up to two extra things:

  1. Bind mount /var/run/docker.sock to the container so docker clients can create sibling containers on the host.

  2. If you cannot use host networking, supply the IP address of the host’s network bridge (usually docker0, etc), so that the fixture knows where to find the PG server. The IP address is either supplied via the constructor to PostgresContainerFixture or you can set the DBTESTTOOLS_PG_IP_ADDR environment variable.

If you want to use Podman instead of Docker engine you will need to follow these steps:

1. Enable and start podman on your host `bash systemctl --user enable podman systemctl --user start podman ` 2. export DBTESTTOOLS_USE_PODMAN=1 variable.

Postgres DB-API driver

The PostgresContainerFixture can use either psycopg2 (installed by default) or psycopg v3. To install psycopg v3 support:

pip install db-testtools[psycopg3]

The driver is chosen in this order:

  1. The driver argument to PostgresContainerFixture, which may be ‘psycopg2’ or ‘psycopg’ (‘psycopg3’ is accepted as an alias). When using DatabaseResource, pass it via engine_fixture_kwargs, e.g. engine_fixture_kwargs={‘driver’: ‘psycopg’}.

  2. The DBTESTTOOLS_PG_DRIVER environment variable, with the same values as above.

  3. Auto-detection: psycopg2 if it is installed, otherwise psycopg v3.

The SQLAlchemy engine is created with the matching dialect (postgresql+psycopg2 or postgresql+psycopg).

This code has been in use daily on a large project at Cisco for a few years now, and is very stable.

Metadata

Release files for db-testtools 2026.10.8

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for db-testtools 2026.10.8
File Size Uploaded
db_testtools-2026.10.8.tar.gz 15.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for db-testtools 2026.10.8
File Interpreter ABI Platform
db_testtools-2026.10.8-py3-none-any.whl Python 3 none any Details

Total release size: 38.8 kB

Release files / db_testtools-2026.10.8.tar.gz

Download URL db_testtools-2026.10.8.tar.gz
Size 15.4 kB
Tags Source
SHA-256 checksum
How to use checksums
861f3f5e94aa8a0af84679cb94a20703ad66a767f9b8c83744d69e239690efc4
BLAKE2b-256 checksum
How to use checksums
6f5c78678b627d318424c068c8ecbd3c481ef96e9c383f7f44ae77206f310007
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.3

Release files / db_testtools-2026.10.8-py3-none-any.whl

Download URL db_testtools-2026.10.8-py3-none-any.whl
Size 23.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
399b32c716b2966c5302613eb013422a5094fb447d031e2ae5150a1ef9398aa6
BLAKE2b-256 checksum
How to use checksums
bad90c3f6c5721aef7aa31dda8d51cfe9d21cd79d71090924307ee3009b21b3e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.3
Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page