Skip to main content

pytest fixtures for PostgreSQL

Project description

Lathorp - PostgreSQL Testing Fixtures

Lathorp provides handy pytest fixtures for test code that involves access to a PostgreSQL database.

For example, if the code under test writes data to a database, or queries it for data, you want to write tests to see that it writes and reads the data as you expect. You need an isolated and controlled environment for testing, so you need to set up a temporary database just for testing and fill it up with known data. Setting up a new database is a relatively time consuming operation that slows your tests down. You want to do this just once per testing session and let multiple tests share the same temporary database instance. However, many tests should be isolated from each other - you don't want side effects from a test (say, data written to the database) leaking to another test. The solution is to write known data before each test and delete it all after the test has finished.

The Fixtures

The pg fixture is a session-scoped fixture that creates a new temporary database once per test session and deletes it after the session is done.

The pg_connect fixture is a function-scoped fixture that you use to establish a connection to the temporary database. You can optionally load data into the database before the test begins, by pointing to data files that PostgreSQL can read. The data is automatically deleted after the test function returns, so that it doesn't leak to other tests that may expect different data.

A helper function called load_schema_definitions can be used to create the database structure by reading SQL DDL files that you provide. Embed it in your own session-scoped fixture and call that together with pg_connect.

Using Lathorp in Your Project

  1. Include the lathorp package with the development packages of your projects. With pipenv:
pipenv install --dev lathorp
  1. Import fixtures from lathorp in your tests. You can also import them in to make them available to all your tests. See tests/ in this project for an example.

  2. Add either pg or pg_connect as arguments to your test functions, as shown below. There's no point in adding both, since pg_connect itself uses pg.

def my_test(pg_connect):
    """A test that connects to the temporary test database."""
    with pg_connect() as conn:
        with conn.cursor() as cursor:
            cursor.execute("SELECT 'Hello, world!' AS hello;")

For more examples, see tests/ in this project.

  1. Lathorp truly shines when you use it together with schema definitions (SQL DDL) files and test data files (CSV or PostgreSQL-readable text). Create a session-scoped fixture that calls load_schema_definitions and give it the path to your schema definitions. Then use this fixture along with pg_connect and give it the path to your data files.
# In
import pathlib
from lathorp.fixtures import load_schema_definitions
from lathorp.fixtures import pg
from lathorp.fixtures import pg_connect

def init_schema(pg):

# In your test module
def test_my_fun(init_schema, pg_connect):
    conn = pg_connect(pathlib.Path('path/to/my_table.csv'))  # loads data into my_table
    with conn.cursor() as cursor:
        cursor.execute('SELECT * FROM my_table;')

Now every test can have access to an initialized database with test-specific data.

Why "Lathorp?

The name is a reference to Dr. Emmett Lathorp "Doc" Brown, the crazy scientist and inventor of the DeLorean time machine from Back to the Future trilogy.

The Lathorp library too lets you go back in time to a fresh database after every test.

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for lathorp, version 0.1.3
Filename, size File type Python version Upload date Hashes
Filename, size lathorp-0.1.3-py3-none-any.whl (6.6 kB) File type Wheel Python version py3 Upload date Hashes View hashes
Filename, size lathorp-0.1.3.tar.gz (9.1 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page