flexmock 是一个用于Python的测试库,它使得创建模拟、存根和伪造变得简单。
项目描述
flexmock - Python的模拟、存根和间谍库。
Flexmock是Python的一个测试库,使得创建模拟、存根和虚假对象变得容易。
功能
- 模拟:轻松创建模拟对象并对使用的方法或属性及其调用参数进行断言。
- 间谍:代理对象的原生方法或属性的调用并基于返回值或调用次数进行断言。
- 虚假:轻松生成用于测试的虚假对象。
- 存根:创建存根对象,只需一次调用即可替换现有对象和类的一部分。
- 无外部依赖:Flexmock轻量级,仅使用Python标准库。没有外部依赖。
- 简单直观:声明结构更像是英语句子而不是API调用,因此易于学习和使用。
- 完全类型注释:外部API完全类型注释,因此与静态分析工具和编辑器自动完成配合良好。
- 与测试运行器的集成:与所有主要测试运行器(如unittest、doctest和pytest)无缝集成。
- Python 3.6+ 和 PyPy3:经过广泛测试,与最新Python版本兼容。
安装
使用pip安装
pip install flexmock
示例
Flexmock与几乎所有流行的测试运行器都有平滑的集成,因此不需要特殊设置。只需将flexmock导入到您的测试模块中,就可以开始以下示例
from flexmock import flexmock
模拟
断言有多种形式,Flexmock有许多不同的功能来生成它们
# Simplest is ensuring that a certain method is called
flexmock(Train).should_receive("get_tickets").once()
# Of course, it is also possible to provide a default return value
flexmock(Train).should_receive("get_destination").and_return("Paris").once()
# Or check that a method is called with specific arguments
flexmock(Train).should_receive("set_destination").with_args("Seoul").at_least().twice()
间谍
有时您想执行实际的方法并简单地找出它被调用了多少次,而不是模拟。Flexmock使用should_call
而不是should_receive
来生成此类断言
# Verify that a method is called at most three times
flexmock(Train).should_call("get_tickets").at_most().times(3)
# Make sure that a method is never called with specific arguments
flexmock(Train).should_call("set_destination").with_args("Helsinki").never()
# More complex example with features like argument type and exception matching
flexmock(Train).should_call("crash").with_args(str, int).and_raise(AttributeError).once()
请参阅文档中的更多示例。
文档
用户指南、示例和完整的API参考可在: https://flexmock.readthedocs.io
贡献
贡献是绝对欢迎并鼓励的!请参阅CONTRIBUTING.md以开始。
项目详情
下载文件
下载适用于您平台的文件。如果您不确定选择哪个,请了解更多关于安装包的信息。
源分发
flexmock-0.12.1.tar.gz (52.8 kB 查看散列)
构建分发
flexmock-0.12.1-py3-none-any.whl (20.6 kB 查看散列)