用于pytest的postgresql fixture和fixture工厂。
pytest-postgresql的Python项目详细描述
这是什么?
这是一个pytest插件,它使您能够测试依赖于正在运行的postgresql数据库的代码。 它允许您为postgresql进程和客户机指定fixture。
如何使用
警告
在PostgreSQL版本>;9.x上测试。有关详细信息,请参阅测试。
安装时使用:
pip install pytest-postgresql
您还需要安装psycopg2,或它的一个替代包,如psycopg2-binary (预编译的控制盘)或psycopg2cffi(基于cffi,对pypy有用)。
插件包含两个固定装置:
- postgresql-它是一个具有功能范围的客户端fixture。每次测试后,它都会结束所有剩余的连接,并从postgresql中删除测试数据库以确保可重复性。
- postgresql_proc-会话范围的fixture,它在postgresql实例首次使用时启动,在测试结束时停止。
只需将其中一个fixture包含到测试fixture列表中。
如果需要,您还可以创建其他PostgreSQL客户端和进程装置:
frompytest_postgresqlimportfactoriespostgresql_my_proc=factories.postgresql_proc(port=None,unixsocketdir='/var/run')postgresql_my=factories.postgresql('postgresql_my_proc')
注意
每个postgresql进程fixture都可以通过fixture工厂参数以不同的方式配置。
配置
可以通过三种方式定义设置,即fixture factory参数、命令行选项和pytest.ini配置选项。 您可以选择您喜欢的设置,但请记住,这些设置的处理顺序如下:
- ^{tt4}$
- ^{tt5}$
- ^{tt6}$
PostgreSQL option | Fixture factory argument | Command line option | pytest.ini option | Default |
---|---|---|---|---|
Path to executable | executable | –postgresql-exec | postgresql_exec | /usr/lib/postgresql/9.1/bin/pg_ctl |
host | host | –postgresql-host | postgresql_host | 127.0.0.1 |
port | port | –postgresql-port | postgresql_port | random |
postgresql user | user | –postgresql-user | postgresql_user | postgres |
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_proc=factories.postgresql_proc(port=8888)
运行测试时使用--postgresql-port命令行选项
py.test tests --postgresql-port=8888
在pytest.ini文件中将端口指定为postgresql_port。
To do so, put a line like the following under the ^{tt10}$ section of your ^{tt9}$:
[pytest]postgresql_port=8888
更改日志
2.0.0
- [功能]放弃对Python2.7的支持。从现在起,仅支持Python3.5及更高版本
- [功能]通过插件选项配置数据库名称的能力
- [增强]使用tmpdir_工厂。删除logsdir参数
- [e金融]仅支持PostgreSQL 9.0及以上版本
- [bugfix]启动postgresql时,始终将lc_all、lc_type和lang设置为c.utf-8。 它使postgresql以英语开始。
1.4.1
- [错误修复]允许使用连字符创建测试数据库
1.4.0
- [增强]能够为PostgreSQL进程和连接配置其他选项
- [bugfix]-删除了对psycopg2的硬依赖,允许它的任何替代包,比如 psycopg2-binary,待使用。
- [维护]放弃对Python3.4的支持,改用3.7
1.3.4
[错误修复]正确检测执行器是否正在运行,并在执行器停止后进行清理
Note
Previously if a test failed, there was a possibility of the executor being removed when python was closing, causing it to print ignored errors on already unloaded modules.
1.3.3
- [增强]使用Executor的上下文管理器在fixture中启动/停止PostrgeSql服务器
1.3.2
- [错误修复]版本regexp以正确捕获PostgreSQL 10
1.3.1
- [增强]显式关闭日志采集器
1.3.0
- [功能]pypy兼容性
1.2.0
[错误修正]-在数据库被删除之前禁止连接。
Note
Otherwise it caused random test subprocess to connect again and this the drop was unsucessfull which resulted in many more test failes on setup.
[清除]-删除path.py依赖项
1.1.1
- [BugFix]——修复默认的PGYCTL路径创建< /LI>
1.1.0
- [功能]-将getFuncArgValue的用法迁移到getFixtureValue。至少需要Pytest 3.0.0
1.0.0
- 为PostgreSQL启动参数创建命令行和pytest.ini配置选项
- 为PostgreSQL用户名创建命令行和pytest.ini配置选项
- 默认情况下使端口随机
- 为可执行文件创建命令行和pytest.ini配置选项
- 为主机创建命令行和pytest.ini配置选项
- 为端口创建命令行和pytest.ini配置选项
- 从pytest dbfixtures中提取的代码