Package: testing.postgresql
Abouttesting.postgresqlautomatically setups a postgresql instance in a temporary directory, and destroys it after testing.Documentationhttps://github.com/tk0miya/testing.postgresqlIssueshttps://github.com/tk0miya/testing.postgresql/issuesDownloadhttps://pypi.python.org/pypi/testing.postgresqlInstallUse pip:$ pip install testing.postgresqlAndtesting.postgresqlrequires PostgreSQL server in your PATH.UsageCreate PostgreSQL instance usingtesting.postgresql.Postgresql:import testing.postgresql
from sqlalchemy import create_engine

# Lanuch new PostgreSQL server
with testing.postgresql.Postgresql() as postgresql:
    # connect to PostgreSQL
    engine = create_engine(postgresql.url())

    # if you use postgresql or other drivers:
    #   import psycopg2
    #   db = psycopg2.connect(**postgresql.dsn())

    #
    # do any tests using PostgreSQL...
    #

# PostgreSQL server is terminated heretesting.postgresql.Postgresqlexecutesinitdbandpostgreson instantiation.
On deleting Postgresql object, it terminates PostgreSQL instance and removes temporary directory.If you want a database including tables and any fixtures for your apps,
usecopy_data_fromkeyword:# uses a copy of specified data directory of PostgreSQL.
postgresql = testing.postgresql.Postgresql(copy_data_from='/path/to/your/database')For example, you can setup new PostgreSQL server for each testcases on setUp() method:import unittest
import testing.postgresql

class MyTestCase(unittest.TestCase):
    def setUp(self):
        self.postgresql = testing.postgresql.Postgresql()

    def tearDown(self):
        self.postgresql.stop()To make your tests fastertesting.postgresql.Postgresqlinvokesinitdbcommand on every instantiation.
That is very simple. But, in many cases, it is very waste that generating brandnew database for each testcase.To optimize the behavior, usetesting.postgresql.PostgresqlFactory.
The factory class is able to cache the generated database beyond the testcases,
and it reduces the number of invocation ofinitdbcommand:import unittest
import testing.postgresql

# Generate Postgresql class which shares the generated database
Postgresql = testing.postgresql.PostgresqlFactory(cache_initialized_db=True)


def tearDownModule(self):
    # clear cached database at end of tests
    Postgresql.clear_cache()


class MyTestCase(unittest.TestCase):
    def setUp(self):
        # Use the generated Postgresql class instead of testing.postgresql.Postgresql
        self.postgresql = Postgresql()

    def tearDown(self):
        self.postgresql.stop()If you want to insert fixtures to the cached database, useinitdb_handleroption:# create initial data on create as fixtures into the database
def handler(postgresql):
    conn = psycopg2.connect(**postgresql.dsn())
    cursor = conn.cursor()
    cursor.execute("CREATE TABLE hello(id int, value varchar(256))")
    cursor.execute("INSERT INTO hello values(1, 'hello'), (2, 'ciao')")
    cursor.close()
    conn.commit()
    conn.close()

# Use `handler()` on initialize database
Postgresql = testing.postgresql.PostgresqlFactory(cache_initialized_db=True,
                                                  on_initialized=handler)RequirementsPython 2.6, 2.7, 3.2, 3.3, 3.4, 3.5pg8000 1.10LicenseApache License 2.0History1.3.0 (2016-02-03)Add testing.postgresql.PostgresqlFactoryDepend ontesting.common.databasepackage1.2.1 (2015-08-22)Fix bug:Close #3 Fix AttributeError on end of tests1.2.0 (2015-05-17)Usepg8000for connector to create test databaseConnect topostgresto create test database (instead oftemplate1)1.1.2 (2015-04-06)Fix bugs:Do not call os.getpid() on destructor (if not needed)Raise detailed RuntimeError if initdb exits non-zero1.1.1 (2015-01-18)Disable logging_collector feature (For Fedora)Fix bugs:MacPorts default path is /opt/local/lib/postgresql*, no dash1.1.0 (2014-12-20)Invoke ‘postgres’ command instead of ‘postmaster’1.0.6 (2014-07-19)Fix #1 Dirty postmaster shut down1.0.5 (2014-07-19)Fix path for PostgreSQLUse absolute path for which command1.0.4 (2014-06-19)Fix timeout on terminating postgresqlSupport PostgreSQL on /usr/local/bin (cf. FreeBSD ports)Fix bugs1.0.3 (2014-06-11)Fix ImportError if caught SIGINT on py31.0.2 (2013-12-06)Change behavior: Postgresql#stop() cleans workdirFix caught AttributeError on object deletion1.0.1 (2013-12-05)Add @skipIfNotInstalled decorator (alias of skipIfNotFound)Suport python 2.6 and 3.21.0.0 (2013-12-04)Add @skipIfNotFound decorator0.1.0 (2013-11-26)First release