相等性测试的虚拟值
pystubs的Python项目详细描述
pystubs:相等性测试的伪值
这个小python模块提供了各种类型,可以用来检查
对于使用相等(==
)测试的条件。例如,测试
数字小于5:
>>>fromstubsimport*>>>2==LessThan(5)True
完全等同于:
>>> 2 < 5
True
当比较嵌套容器时,这种方法非常方便,例如 api返回的json对象:
>>>expected_order={...'id':MatchesRegex('[0-9a-z]{10}'),...'quantity':GreaterThan(10),...'shippingAddress':Contains('Dublin'),...'deliveryDate':ANY,...'amount':InRange(200,500),...'items':Contains('apples')&...Contains('bananas')&...~Contains('pears'),...}>>>expected_order=={...'id':'cxw23fac3n',...'quantity':26,...'shippingAddress':'18 Some Street, Dublin, London',...'deliveryDate':'tomorrow',...'amount':300,...'items':[...'apples',...'avocados',...'bananas',...],...}True>>>expected_order=={...'id':'XXX',...'quantity':3,...'shippingAddress':'18 Some Other Street, New York City, USA',...'deliveryDate':'yesterday',...'amount':600,...'items':[...'apples',...'pears',...],...}False
这种方法在单元测试中非常有用,可以生成测试用例 更短更容易理解。
安装
pystubs没有外部依赖项。要安装它:
$ pip install pystubs
存根列表
泛型:
Stub | Usage | Equivalent to | Notes |
---|---|---|---|
ANY | ^{ | ^{ | |
PLACEHOLDER | ^{ | ^{ | Meant to be used as a placeholder to be replaced at a later time |
AnyOf | ^{ | ^{ | |
NoneOf | ^{ | ^{ |
相等:
Stub | Usage | Equivalent to | Notes |
---|---|---|---|
Equal | ^{ | ^{ | Useful when combined with other stubs |
NotEqual | ^{ | ^{ |
顺序:
Stub | Usage | Equivalent to | Notes |
---|---|---|---|
LessThan | ^{ | ^{ | |
LessThanOrEqual | ^{ | ^{ | |
GreaterThan | ^{ | ^{ | |
GreaterThanOrEqual | ^{ | ^{ | |
InRange | ^{ | ^{ |
容器:
Stub | Usage | Equivalent to | Notes |
---|---|---|---|
Contains | ^{ | ^{ | |
HasSize | ^{ | ^{ | |
CountOf | ^{ | ^{ | Works on any iterable, even those that don't support ^{ |
HasItems | ^{ | ^{ |
字符串:
这些函数同时作用于str
和bytes
对象。
Stub | Usage | Equivalent to | Notes |
---|---|---|---|
StartsWith | ^{ | ^{ | |
EndsWith | ^{ | ^{ | |
MatchesRegex | ^{ | ^{ | |
ContainsRegex | ^{ | ^{ |
类型和标识测试:
Stub | Usage | Equivalent to | Notes |
---|---|---|---|
Is | ^{ | ^{ | |
IsNot | ^{ | ^{ | |
InstanceOf | ^{ | ^{ |
组合用存根
存根可以与|
(or)、&
(and)、^
(exclusive or)和
~
(非)运算符。例如,以下存根表达式:
>>>'apple'==HasSize(5)&Contains('a')True
相当于表达式:
>>>len('apple')==5and'a'in'apple'True
许可证
pystubs放在公共域中。想做什么就做什么 它和/或它的源代码!