testing.postgresql
automatically setups a postgresql instance in a temporary directory, and destroys it after testing.
- Documentation
- https://github.com/tk0miya/testing.postgresql
- Issues
- https://github.com/tk0miya/testing.postgresql/issues
- Download
- https://pypi.python.org/pypi/testing.postgresql
Use pip:
$ pip install testing.postgresql
And testing.postgresql
requires PostgreSQL server in your PATH.
Create PostgreSQL instance using testing.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 here
testing.postgresql.Postgresql
executes initdb
and postgres
on 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,
use copy_data_from
keyword:
# 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()
testing.postgresql.Postgresql
invokes initdb
command 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, use testing.postgresql.PostgresqlFactory
.
The factory class is able to cache the generated database beyond the testcases,
and it reduces the number of invocation of initdb
command:
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, use initdb_handler
option:
# 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)
- Python 2.7, 3.4, 3.5, 3.6
- pg8000 1.10
Apache License 2.0
- Add testing.postgresql.PostgresqlFactory
- Depend on
testing.common.database
package
- Fix bug:
- Close #3 Fix AttributeError on end of tests
- Use pg8000 for connector to create test database
- Connect to postgres to create test database (instead of template1)
- Fix bugs:
- Do not call os.getpid() on destructor (if not needed)
- Raise detailed RuntimeError if initdb exits non-zero
- Disable logging_collector feature (For Fedora)
- Fix bugs:
- MacPorts default path is /opt/local/lib/postgresql*, no dash
- Invoke 'postgres' command instead of 'postmaster'
- Fix #1 Dirty postmaster shut down
- Fix path for PostgreSQL
- Use absolute path for which command
- Fix timeout on terminating postgresql
- Support PostgreSQL on /usr/local/bin (cf. FreeBSD ports)
- Fix bugs
- Fix ImportError if caught SIGINT on py3
- Change behavior: Postgresql#stop() cleans workdir
- Fix caught AttributeError on object deletion
- Add @skipIfNotInstalled decorator (alias of skipIfNotFound)
- Suport python 2.6 and 3.2
- Add @skipIfNotFound decorator
- First release