A tiny plugin for pytest which runs PostgreSQL in Docker
Project description
pytest-pg
A pytest plugin that provides session-scoped fixtures for running PostgreSQL inside Docker containers.
It automatically spins up a container, waits for PostgreSQL to become ready, exposes connection details
(host, port, user, password, database) via a PG dataclass, and tears the container down after
the test session. Pre-built fixtures are available for PostgreSQL versions 11 through 18 as well as latest.
Readiness checks work with any of the common drivers — asyncpg, psycopg2, or psycopg3.
To speed up tests, pytest-pg does the following tweaks:
- fsync=off
- full_page_writes=off
- synchronous_commit=off
- jit=off
- bgwriter_lru_maxpages=0
- data directory is mounted to a tmpfs
How to use?
You can use the following fixtures:
pg– the latest PostgreSQL image availablepg_11– PostgreSQL 11pg_12– PostgreSQL 12pg_13– PostgreSQL 13pg_14– PostgreSQL 14pg_15– PostgreSQL 15pg_16– PostgreSQL 16pg_17– PostgreSQL 17pg_18– PostgreSQL 18
import asyncpg
async def test_asyncpg_query(pg):
conn = await asyncpg.connect(
user=pg.user,
password=pg.password,
database=pg.database,
host=pg.host,
port=pg.port,
)
await conn.execute("CREATE TABLE test_table (id serial PRIMARY KEY, value text);")
await conn.execute("INSERT INTO test_table (value) VALUES ($1)", "hello")
row = await conn.fetchrow("SELECT value FROM test_table WHERE id = $1", 1)
assert row["value"] == "hello"
await conn.close()
Also run_pg context manager is available, you can use it to create your own fixture, using docker image you need:
import os
import pytest
import pytest_pg
@pytest.fixture(scope='session', autouse=True)
def postgres_env_vars() -> Generator[None]:
docker_image = 'postgres:18'
with pytest_pg.run_pg(docker_image) as pg:
os.environ['POSTGRES_USER'] = pg.user
os.environ['POSTGRES_PASSWORD'] = pg.password
os.environ['POSTGRES_HOST'] = pg.host
os.environ['POSTGRES_PORT'] = str(pg.port)
os.environ['POSTGRES_DBNAME'] = pg.database
yield
# or like so:
@pytest.fixture(scope='session', autouse=True)
def postgres_env_vars(pg_18: pytest_pg.PG) -> Generator[None]:
os.environ['POSTGRES_USER'] = pg_18.user
os.environ['POSTGRES_PASSWORD'] = pg_18.password
os.environ['POSTGRES_HOST'] = pg_18.host
os.environ['POSTGRES_PORT'] = str(pg_18.port)
os.environ['POSTGRES_DBNAME'] = pg_18.database
yield
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pytest_pg-0.0.28.tar.gz.
File metadata
- Download URL: pytest_pg-0.0.28.tar.gz
- Upload date:
- Size: 31.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.11.11 {"installer":{"name":"uv","version":"0.11.11","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
86921858259fbd7e0b845537ee0270bf85d80b462da962168e16588dbe616c0a
|
|
| MD5 |
bfafd358c92c14d8b1c37b7ebff230e3
|
|
| BLAKE2b-256 |
57edfed1889457629ce6c9d1d74f7d35d8cdee7e1cf84c08cae79bbc3d65012a
|
File details
Details for the file pytest_pg-0.0.28-py3-none-any.whl.
File metadata
- Download URL: pytest_pg-0.0.28-py3-none-any.whl
- Upload date:
- Size: 6.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.11.11 {"installer":{"name":"uv","version":"0.11.11","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
dec4233689497a5c366b4305578a64fee0f1f2ce9d2740cd1e1882394c86a47a
|
|
| MD5 |
3f5f67ae57625b567cf24af606d5ea33
|
|
| BLAKE2b-256 |
49f6d739660c34eb9d46a8088d9f60258e042ffcd1f7c744b3b9f0695d90889f
|