简体   繁体   English

在python,postgresql中进行数据库测试

[英]Database testing in python, postgresql

How do you unit test your python DAL that is using postgresql. 你如何对使用postgresql的python DAL进行单元测试。

In sqlite you could create in-memory database for every test but this cannot be done for postgresql. 在sqlite中,您可以为每个测试创建内存数据库,但这不能用于postgresql。

I want a library that could be used to setup a database and clean it once the test is done. 我想要一个可用于设置数据库的库,并在测试完成后清理它。

I am using Sqlalchemy as my ORM. 我使用Sqlalchemy作为我的ORM。

pg_tmp(1) is a utility intended to make this task easy. pg_tmp(1)是一个旨在简化此任务的实用程序。 Here is how you might start up a new connection with SQLAlchemy: 以下是如何使用SQLAlchemy启动新连接:

from subprocess import check_output
from sqlalchemy import create_engine

url = check_output(['pg_tmp', '-t'])
engine = create_engine(url)

This will spin up a new database that is automatically destroyed in 60 seconds. 这将启动一个在60秒内自动销毁的新数据库。 If a connection is open pg_tmp will wait until all active connections are closed. 如果连接打开, pg_tmp将等待,直到所有活动连接都关闭。

你试过testing.postgresql吗?

您可以使用nose编写测试,然后使用SQLAlchemy在setup / teardown方法中创建和清理测试数据库。

There's QuickPiggy too, which is capable of cleaning up after itself. 还有QuickPiggy ,它能够自行清理。 From the docs: 来自文档:

A makeshift PostgresSQL instance can be obtained quite easily: 可以很容易地获得临时PostgresSQL实例:

pig = quickpiggy.Piggy(volatile=True, create_db='somedb') pig = quickpiggy.Piggy(volatile = True,create_db ='somedb')

conn = psycopg2.connect(pig.dsnstring()) conn = psycopg2.connect(pig.dsnstring())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM