一个pytest插件,用于轻松实例化可复制的模拟资源。
pytest-mock-resources的Python项目详细描述
简介
依赖外部资源(例如postgres、redshift等)的代码可能很困难 为编写自动化测试。传统的智慧可能是模仿或删除实际的数据库 调用并断言代码在调用之前/之后正常工作。在
但是,以下面的simple为例:
defserialize(users):return[{'user':user.serialize(),'address':user.address.serialize(),'purchases':[p.serialize()forpinuser.purchases],}foruserinusers]defview_function(session):users=session.query(User).join(Address).options(selectinload(User.purchases)).all()returnserialize(users)
当然,您可以测试serialize
,但是实际的查询是否正确真的
要求您执行查询。在
球场
让测试依赖于在某处运行的realpostgres实例是一件痛苦的事,非常脆弱, 而且容易出现跨机器的问题和测试失败。在
因此,pytest-mock-resources
(主要)通过管理docker容器的生命周期来工作
并在测试中提供对它们的访问。在
因此,本方案提出了两个主要假设:
- 您使用的是
pytest
(希望这是适当的,给定包名) - 对于许多资源,
docker
必须可用并正在运行(或通过远程docker访问)。在
如果您不熟悉Pytest装置,可以在Pytest documentation中阅读它们。在
在上面的示例中,您的测试文件可能类似于
^{pr2}$现有资源(更多可能)
- 在
SQLite公司
frompytest_mock_resourcesimportcreate_sqlite_fixture
在 - 在
博士后
frompytest_mock_resourcesimportcreate_postgres_fixture
在 - 在
红移
note在底层使用postgres,但是fixture试图支持同样多的内容 尽可能提供红移功能(包括redshift的
COPY
/UNLOAD
命令)。在frompytest_mock_resourcesimportcreate_redshift_fixture
在 - 在
蒙哥
frompytest_mock_resourcesimportcreate_mongo_fixture
在 - 在
雷迪斯
frompytest_mock_resourcesimportcreate_redis_fixture
在 - 在
MySQL数据库
frompytest_mock_resourcesimportcreate_mysql_fixture
在
安装
# Basic fixture support pip install "pytest-mock-resources"# For postgres install EITHER of the following: pip install "pytest-mock-resources[postgres-binary]" pip install "pytest-mock-resources[postgres]"# For redshift install EITHER of the following:# (redshift fixtures require postgres dependencies...) pip install "pytest-mock-resources[postgres, redshift]" pip install "pytest-mock-resources[postgres-binary, redshift]"# For mongo install the following: pip install "pytest-mock-resources[mongo]"# For redis pip install "pytest-mock-resources[redis]"# For mysql pip install "pytest-mock-resources[mysql]"
未来可能的资源
- 兔子经纪人
- AWS普雷斯托
如果您发现任何错误或想围绕您想要实现的模拟资源展开对话,请随意提交issue!在
- 项目
标签: