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.
Warning
Tested on PostgreSQL versions > 9.x. See tests for more details.
Install with:
pip install pytest-postgresqlYou 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 three 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.
- postgresql_nooproc - a nooprocess fixture, that's connecting to already running postgresql
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, unixsocketdir='/var/run')
postgresql_my = factories.postgresql('postgresql_my_proc')Note
Each PostgreSQL process fixture can be configured in a different way than the others through the fixture factory arguments.
Some projects are using already running postgresql servers (ie on docker instances).
In order to connect to them, one would be using the postgresql_nooproc fixture.
postgresql_external = factories.postgresql('postgresql_nooproc')By default the postgresql_nooproc fixture would connect to postgresql instance using 5432 port. Standard configuration options apply to it.
These are the configuration options that are working on all levels with the postgresql_nooproc fixture:
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 argumentCommand line optionConfiguration option in your pytest.ini file
| PostgreSQL option | Fixture factory argument | Command line option | pytest.ini option | Noop process fixture | Default |
|---|---|---|---|---|---|
| Path to executable | executable | --postgresql-exec | postgresql_exec | /usr/lib/postgresql/9.1/bin/pg_ctl | |
| host | host | --postgresql-host | postgresql_host | yes | 127.0.0.1 |
| port | port | --postgresql-port | postgresql_port | yes (5432) | random |
| postgresql user | user | --postgresql-user | postgresql_user | yes | postgres |
| password | password | --postgresql-password | postgresql_password | yes | |
| Starting parameters | startparams | --postgresql-startparams | postgresql_startparams | -w | |
| Log filename's prefix | logsprefix | --postgresql-logsprefix | postgresql_logsprefix | ||
| Location for unixsockets | unixsocket | --postgresql-unixsocketdir | postgresql_unixsocketdir | $TMPDIR | |
| Database name | db_name | --postgresql-dbname | postgresql_dbname | test | |
| PostgreSQL connection options | options | --postgresql-options | postgresql_options | yes |
Example usage:
pass it as an argument in your own fixture
postgresql_proc = factories.postgresql_proc( port=8888)
use
--postgresql-portcommand line option when you run your testspy.test tests --postgresql-port=8888
specify your port as
postgresql_portin yourpytest.inifile.To do so, put a line like the following under the
[pytest]section of yourpytest.ini:[pytest] postgresql_port = 8888
It is possible and appears it's used in other libraries for tests,
to maintain database state with the use of the pytest-postgresql database
managing functionality:
For this import DatabaseJanitor and use its init and drop methods:
from pytest_postgresql.factories import DatabaseJanitor
# variable definition
janitor = DatabaseJanitor(user, host, port, db_name, version)
janitor.init()
# your code, or yield
janitor.drop()
# at this moment you'll have clean database stepor use it as a context manager:
from pytest_postgresql.factories import DatabaseJanitor
# variable definition
with DatabaseJanitor(user, host, port, db_name, version):
# do something here