freshcrate
Skin:/
Home > Databases > pytest-postgresql

pytest-postgresql

Postgresql fixtures and fixture factories for Pytest.

Why this rank:Strong adoptionRecent releaseHealthy release cadence

Description

.. image:: https://raw.githubusercontent.com/dbfixtures/pytest-postgresql/main/logo.png :width: 100px :height: 100px pytest-postgresql ================= .. image:: https://img.shields.io/pypi/v/pytest-postgresql.svg :target: https://pypi.python.org/pypi/pytest-postgresql/ :alt: Latest PyPI version .. image:: https://img.shields.io/pypi/wheel/pytest-postgresql.svg :target: https://pypi.python.org/pypi/pytest-postgresql/ :alt: Wheel Status .. image:: https://img.shields.io/pypi/pyversions/pytest-postgresql.svg :target: https://pypi.python.org/pypi/pytest-postgresql/ :alt: Supported Python Versions .. image:: https://img.shields.io/pypi/l/pytest-postgresql.svg :target: https://pypi.python.org/pypi/pytest-postgresql/ :alt: License What is this? ============= This is a pytest plugin that enables you to test code relying on a running PostgreSQL database. It provides fixtures for managing both the PostgreSQL process and the client connections. Quick Start =========== 1. **Install the plugin:** .. code-block:: sh pip install pytest-postgresql You will also need to install ``psycopg`` (version 3). See `its installation instructions <https://www.psycopg.org/psycopg3/docs/basic/install.html>`_. .. note:: While this plugin requires ``psycopg`` 3 to manage the database, your application code can still use ``psycopg`` 2. 2. **Run a test:** Simply include the ``postgresql`` fixture in your test. It provides a connected ``psycopg.Connection`` object. .. code-block:: python def test_example(postgresql): """Check main postgresql fixture.""" with postgresql.cursor() as cur: cur.execute("CREATE TABLE test (id serial PRIMARY KEY, num integer, data varchar);") postgresql.commit() How to use ========== .. warning:: Tested on PostgreSQL versions >= 14. See tests for more details. How does it work ---------------- .. image:: https://raw.githubusercontent.com/dbfixtures/pytest-postgresql/main/docs/images/architecture.svg :alt: Project Architecture Diagram :align: center The plugin provides two main types of fixtures: **1. Client Fixtures** These provide a connection to a database for your tests. * **postgresql** - A function-scoped fixture. It returns a connected ``psycopg.Connection``. After each test, it terminates leftover connections and drops the test database to ensure isolation. **2. Process Fixtures** These manage the PostgreSQL server lifecycle. * **postgresql_proc** - A session-scoped fixture that starts a PostgreSQL instance on its first use and stops it when all tests are finished. * **postgresql_noproc** - A fixture for connecting to an already running PostgreSQL instance (e.g., in Docker or CI). Customizing Fixtures -------------------- You can create additional fixtures using factories: .. code-block:: python from pytest_postgresql import factories # Create a custom process fixture postgresql_my_proc = factories.postgresql_proc( port=None, unixsocketdir='/var/run') # Create a client fixture that uses the custom process postgresql_my = factories.postgresql('postgresql_my_proc') .. note:: Each process fixture can be configured independently through factory arguments. Pre-populating the database for tests ------------------------------------- If you want the database to be automatically pre-populated with your schema and data, there are two levels you can achieve it: #. **Per test:** In a client fixture, by using an intermediary fixture. #. **Per session:** In a process fixture. The process fixture accepts a ``load`` parameter, which supports: * **SQL file paths:** Loads and executes the SQL files. * **Loading functions:** A callable or an import string (e.g., ``"path.to.module:function"``). These functions receive **host**, **port**, **user**, **dbname**, and **password** and must perform the connection themselves (or use an ORM). The process fixture pre-populates the database once per session into a **template database**. The client fixture then clones this template for each test, which significantly **speeds up your tests**. .. code-block:: python from pathlib import Path postgresql_my_proc = factories.postgresql_proc( load=[ Path("schemafile.sql"), "import.path.to.function", load_this_callable ] ) Defining pre-population on the command line: .. code-block:: sh pytest --postgresql-populate-template=path/to/file.sql --postgresql-populate-template=path.to.function Connecting to an existing PostgreSQL database ---------------------------------------------- To connect to an external server (e.g., running in Docker), use the ``postgresql_noproc`` fixture. .. code-block:: python postgresql_external = factories.postgresql('postgresql_noproc') By default, it connects to ``127.0.0.1:5432``. Chaining fixtures

Release History

VersionChangesUrgencyDate
main@2026-06-01Latest activity on main branchHigh6/1/2026
8.0.0Imported from PyPI (8.0.0)Low4/21/2026
v8.0.0Tag v8.0.0Low1/23/2026
v7.0.2Tag v7.0.2Low5/17/2025
v7.0.1Tag v7.0.1Low3/19/2025
v7.0.0Tag v7.0.0Low2/23/2025
v6.1.1Tag v6.1.1Low9/5/2024
v6.1.0Tag v6.1.0Low9/4/2024
v6.0.1Tag v6.0.1Low8/14/2024
v6.0.0Tag v6.0.0Low3/11/2024
v5.1.1Tag v5.1.1Low3/7/2024
v5.1.0Tag v5.1.0Low1/29/2024
v5.0.0Tag v5.0.0Low5/20/2023
v4.1.1Tag v4.1.1Low3/11/2022
v3.1.3Tag v3.1.3Low2/14/2022
v4.1.0Tag v4.1.0Low12/22/2021
v4.0.0Tag v4.0.0Low11/5/2021
v3.1.2Tag v3.1.2Low10/20/2021
v3.1.1Tag v3.1.1Low6/1/2021
v3.1.0Tag v3.1.0Low5/31/2021
v3.0.2Tag v3.0.2Low5/26/2021
v3.0.1Tag v3.0.1Low5/19/2021

Dependencies & License Audit

Loading dependencies...

Similar Packages

mirakuruProcess executor (not only) for tests.main@2026-06-01
kgbUtilities for spying on function calls in unit tests.7.3
alibabacloud-adb20211201Alibaba Cloud adb (20211201) SDK Library for Pythonmaster@2026-06-06
ydbYDB Python SDK3.29.1
typerTyper, build great CLIs. Easy to code. Based on Python type hints.0.26.7

More from pypi

markitdownUtility tool for converting various files to Markdown
fastapiFastAPI framework, high performance, easy to learn, fast to code, ready for production
djangoA high-level Python web framework that encourages rapid development and clean, pragmatic design.
flaskA simple framework for building complex web applications.

More in Databases

milvusMilvus is a high-performance, cloud-native vector database built for scalable vector ANN search
WeKnoraLLM-powered framework for deep document understanding, semantic retrieval, and context-aware answers using RAG paradigm.
ai-real-estate-assistantAdvanced AI Real Estate Assistant using RAG, LLMs, and Python. Features market analysis, property valuation, and intelligent search.
alibabacloud-adb20211201Alibaba Cloud adb (20211201) SDK Library for Python