Testgres documentation¶
Testgres is a PostgreSQL testing framework.
Installation¶
To install testgres, run:
pip install testgres
We encourage you to usevirtualenv for your testing environment.
Usage¶
Environment¶
Note: by default testgres runsinitdb,pg_ctl,psql provided byPATH.
There are several ways to specify a custom postgres installation:
- export
PG_CONFIGenvironment variable pointing to thepg_configexecutable; - export
PG_BINenvironment variable pointing to the directory with executable files.
Example:
exportPG_BIN=$HOME/pg_10/binpython my_tests.py
Examples¶
Here is an example of what you can do withtestgres:
# create a node with random name, port, etcwithtestgres.get_new_node()asnode:# run inidbnode.init()# start PostgreSQLnode.start()# execute a query in a default DBprint(node.execute('select 1'))# ... node stops and its files are about to be removed
Backup & replication¶
It’s quite easy to create a backup and start a new replica:
withtestgres.get_new_node('master')asmaster:master.init().start()# create a backupwithmaster.backup()asbackup:# create and start a new replicareplica=backup.spawn_replica('replica').start()# catch up with master nodereplica.catchup()# execute a dummy queryprint(replica.execute('postgres','select 1'))