Skip to main content

Postgresql fixtures and fixture factories for Pytest.

Project description

pytest-postgresql

Latest PyPI version Wheel Status Supported Python Versions License

Package status

Tests Coverage Status Requirements Status

What is this?

This is a pytest plugin, that enables you to test your code that relies on a running PostgreSQL Database. It allows you to specify fixtures for PostgreSQL process and client.

How to use

Install with:

pip install pytest-postgresql

You will also need to install psycopg2, or one of its alternative packagings such as psycopg2-binary (pre-compiled wheels) or psycopg2cffi (CFFI based, useful on PyPy).

Plugin contains two fixtures:

  • postgresql - it’s a client fixture that has functional scope. After each test it ends all leftover connections, and drops test database from PostgreSQL ensuring repeatability.

  • postgresql_proc - session scoped fixture, that starts PostgreSQL instance at it’s first use and stops at the end of the tests.

Simply include one of these fixtures into your tests fixture list.

You can also create additional postgresql client and process fixtures if you’d need to:

from pytest_postgresql import factories

postgresql_my_proc = factories.postgresql_proc(
    port=None, logsdir='/tmp')
postgresql_my = factories.postgresql('postgresql_my_proc')

Configuration

You can define your settings in three ways, it’s fixture factory argument, command line option and pytest.ini configuration option. You can pick which you prefer, but remember that these settings are handled in the following order:

  • Fixture factory argument

  • Command line option

  • Configuration option in your pytest.ini file

PostgreSQL option

Fixture factory argument

Command line option

pytest.ini option

Default

Path to executable

executable

–postgresql-exec

postgresql_exec

/usr/lib/postgresql/9.1/bin/pg_ctl

host

host

–postgresql-host

postgresql_host

127.0.0.1

port

port

–postgresql-port

postgresql_port

random

postgresql user

user

–postgresql-user

postgresql_user

postgres

Starting parameters

startparams

–postgresql-startparams

postgresql_startparams

-w

Log directory location

logsdir

–postgresql-logsdir

postgresql_logsdir

$TMPDIR

Log filename’s prefix

logsprefix

–postgresql-logsprefix

postgresql_logsprefix

Location for unixsockets

unixsocket

–postgresql-unixsocketdir

postgresql_unixsocketdir

$TMPDIR

Example usage:

  • pass it as an argument in your own fixture

    postgresql_proc = factories.postgresql_proc(
        port=8888)
  • use --postgresql-port command line option when you run your tests

    py.test tests --postgresql-port=8888
  • specify your port as postgresql_port in your pytest.ini file.

    To do so, put a line like the following under the [pytest] section of your pytest.ini:

    [pytest]
    postgresql_port = 8888

Package resources

CHANGELOG

1.4.1

  • [bugfix] Allow creating test databse with hyphens

1.4.0

  • [enhancements] Ability to configure additional options for postgresql process and connection

  • [bugfix] - removed hard dependency on psycopg2, allowing any of its alternative packages, like psycopg2-binary, to be used.

  • [maintenance] Drop support for python 3.4 and use 3.7 instead

1.3.4

  • [bugfix] properly detect if executor running and clean after executor is being stopped

1.3.3

  • [enhancement] use executor’s context manager to start/stop postrgesql server in a fixture

1.3.2

  • [bugfix] version regexp to correctly catch postgresql 10

1.3.1

  • [enhancement] explicitly turn off logging_collector

1.3.0

  • [feature] pypy compatibility

1.2.0

  • [bugfix] - disallow connection to database before it gets dropped.

  • [cleanup] - removed path.py dependency

1.1.1

  • [bugfix] - Fixing the default pg_ctl path creation

1.1.0

  • [feature] - migrate usage of getfuncargvalue to getfixturevalue. require at least pytest 3.0.0

1.0.0

  • create command line and pytest.ini configuration options for postgresql starting parameters

  • create command line and pytest.ini configuration options for postgresql username

  • make the port random by default

  • create command line and pytest.ini configuration options for executable

  • create command line and pytest.ini configuration options for host

  • create command line and pytest.ini configuration options for port

  • Extracted code from pytest-dbfixtures

Project details


Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

pytest_postgresql-1.4.1-py2.py3-none-any.whl (28.6 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file pytest_postgresql-1.4.1-py2.py3-none-any.whl.

File metadata

  • Download URL: pytest_postgresql-1.4.1-py2.py3-none-any.whl
  • Upload date:
  • Size: 28.6 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.1

File hashes

Hashes for pytest_postgresql-1.4.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 9fe4fda8884ae979abc83ae1517423e206cb556de356aa8f071122547854aa75
MD5 683180770674f4f585830d25aa8d5cfb
BLAKE2b-256 938462458473a5f0529c159d9cd1494cec65660ac34ff007c7922a324d6996d4

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page