QuickCheck for Python
Project description
Props
Property-based testing for Python à la QuickCheck.
for_all
for_all takes a list of generators (see below) and a property. It then tests the property for arbitrary values of the generators.
Here’s an example testing the commutative and associative properties of ints:
for_all(int, int)(lambda a, b: a + b == b + a)
for_all(int, int)(lambda a, b: a * b == b * a)
for_all(int, int, int)(lambda a, b, c: c * (a + b) == a * c + b * c)
Properties
Properties are functions which take instances of generators and return True if their condition is met:
def prop_associative(a, b, c):
return a + (b + c) == (a + b) + c
for_all(int, int, int)(prop_associative)
for_all(float, float, float)(prop_associative) # Warning: float isn't actually associative!
Properties can also fail early by raising AssertionError:
def prop_list_append_pop(list, element):
if element not in list:
list.append(element)
assert element in list
list.pop()
return element not in list
return element in list
for_all(list, int)(prop_list_append_pop)
Generators
Note: These are not the same as Python generators. We should rename them. Generaters? Blech.
A generator is a specification of a set of possible Python objects. A generator is either:
One of the following built-in types:
None, bool, int, float, long, complex, str, tuple, set, list, or dict
A class that implements the ArbitraryInterface
Or constructed using the generator combinators.
Combinators
maybe_a
Generates either an arbitrary value of the specified generator or None.
maybe_an
An alias for maybe_a. Provided for syntactic convenience.
one_of
Generates an arbitrary value of one of the specified generators.
tuple_of
Generates a tuple by generating values for each of the specified generators.
set_of
Generates a homogeneous set of the specified generator. You can generate non-homogeneous sets using set.
list_of
Generates a homogeneous list of the specified generator. You can generate non-homogeneous lists using list.
dict_of
Generates a homogeneous dict of the specified generators using kwargs. You can generate non-homogeneous dicts using dict.
arbitrary
arbitrary takes a generator and returns a single instance of the generator.
ArbitraryInterface
We provide a mixin with one classmethod, arbitrary, which raises NotImplementedError. To implement generators for your own classes, please inherit from ArbitraryInterface and provide an implementation for arbitrary.
Here’s an example implementation of a Binary Tree class:
class BinaryTree(ArbitraryInterface):
...
@classmethod
def arbitrary(cls):
return arbitrary(one_of(Leaf, Node))
class Leaf(BinaryTree):
...
@classmethod
def arbitrary(cls):
return cls(...) # an instance of Leaf.
class Node(BinaryTree):
...
@classmethod
def arbitrary(cls):
return cls(
...
# This is equivalent:
arbitrary(BinaryTree),
# to this:
BinaryTree.arbitrary()
) # an instance of Node with two subtrees.
AbstractTestArbitraryInterface
We also provide an AbstractTestArbitraryInterface with you can mixin to your test cases for each class that implements ArbitraryInterface to ensure the arbitrary method is implemented:
class TestBinaryTree(AbstractTestArbitraryInterface,
TestCase):
def setUp(self):
self.cls = BinaryTree
To Do
all built in types: http://docs.python.org/2/library/stdtypes.html
ranges
import some faker generators for more semantic random values
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file props-0.0.3.tar.gz
.
File metadata
- Download URL: props-0.0.3.tar.gz
- Upload date:
- Size: 4.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.0.0 requests-toolbelt/0.8.0 tqdm/4.24.0 CPython/3.7.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 98f4afddaefbfe7a22b205b2751221f62f44685eb7bc8c962353c3d848ce7981 |
|
MD5 | eecb9049d41c3e2120b951c25a994f3a |
|
BLAKE2b-256 | 0599dc1801925f3c72e17a525347e1edf4ceb7e85f70f12d0d21528a8ccdbc4f |
File details
Details for the file props-0.0.3-py3-none-any.whl
.
File metadata
- Download URL: props-0.0.3-py3-none-any.whl
- Upload date:
- Size: 4.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.0.0 requests-toolbelt/0.8.0 tqdm/4.24.0 CPython/3.7.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 95a2e560fcf3cd5186e182c0091ea99a76525a7c2a24e999d1175725c7220c17 |
|
MD5 | 89ec1fcb2c7c6c22cecc4c36588a1b73 |
|
BLAKE2b-256 | dd5cde8686a5ebe03e6ae0f1e6b208950b92e7f3a2067dd1e73fceedb70efb11 |