python的quickcheck
props的Python项目详细描述
道具
pythonαla的基于属性的测试 QuickCheck。
for_all
for_all获取生成器列表(见下文)和属性。它 然后测试生成器的任意值的属性。
下面是一个测试 ints:
for_all(int,int)(lambdaa,b:a+b==b+a)for_all(int,int)(lambdaa,b:a*b==b*a)for_all(int,int,int)(lambdaa,b,c:c*(a+b)==a*c+b*c)
属性
属性是获取生成器实例并返回 True如果满足条件:
defprop_associative(a,b,c):returna+(b+c)==(a+b)+cfor_all(int,int,int)(prop_associative)for_all(float,float,float)(prop_associative)# Warning: float isn't actually associative!
属性也可以通过提高AssertionError:
提前失败defprop_list_append_pop(list,element):ifelementnotinlist:list.append(element)assertelementinlistlist.pop()returnelementnotinlistreturnelementinlistfor_all(list,int)(prop_list_append_pop)
发电机
注意:这些与python生成器不同。我们应该重新命名 他们。发电机?布莱奇。
生成器是一组可能的python对象的规范。一个 生成器为:
- 以下内置类型之一:
- None,bool,int,float,long,complex, str,tuple,set,list,或dict
- 实现ArbitraryInterface 的类
- 或者使用生成器组合器构造。
组合子
- maybe_a
- 生成指定生成器的任意值或 None。
- maybe_an
- maybe_a的别名。提供句法上的便利。
- one_of
- 生成指定生成器之一的任意值。
- tuple_of
- 通过为每个指定的 发电机。
- set_of
- 生成指定生成器的同构集。你可以 使用set生成非齐次集。
- list_of
- 生成指定生成器的同构列表。你可以 使用list生成非同构列表。
- dict_of
- 使用 夸格斯。可以使用dict生成非齐次dict。
arbitrary
arbitrary获取生成器并返回 发电机。
任意接口
我们提供了一种混合方法,其中一种方法是{TT30}$,它提出 NotImplementedError。为您自己的类实现生成器, 请从任意接口继承并提供 arbitrary。
下面是一个二叉树类的示例实现:
classBinaryTree(ArbitraryInterface):...@classmethoddefarbitrary(cls):returnarbitrary(one_of(Leaf,Node))classLeaf(BinaryTree):...@classmethoddefarbitrary(cls):returncls(...)# an instance of Leaf.classNode(BinaryTree):...@classmethoddefarbitrary(cls):returncls(...# This is equivalent:arbitrary(BinaryTree),# to this:BinaryTree.arbitrary())# an instance of Node with two subtrees.
抽象测试接口
我们还提供了一个^ {TT35}$,可以混合 对于实现^{tt17}的每个类的测试用例$ 要确保实现arbitrary方法:
classTestBinaryTree(AbstractTestArbitraryInterface,TestCase):defsetUp(self):self.cls=BinaryTree
待办事项
- 所有内置类型:http://docs.python.org/2/library/stdtypes.html
- 范围
- 导入一些伪造生成器以获取更多语义随机值