Skip to main content
Archived

This project has been archived by its maintainers, and is no longer receiving any updates.

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

Metadata

Release files for props 0.0.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for props 0.0.4
File Size Uploaded
props-0.0.4.tar.gz 5.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for props 0.0.4
File Interpreter ABI Platform
props-0.0.4-py3-none-any.whl Python 3 none any Details

Total release size: 11.3 kB

Release files / props-0.0.4.tar.gz

Download URL props-0.0.4.tar.gz
Size 5.4 kB
Tags Source
SHA-256 checksum
How to use checksums
353f653a1dbf7103b3df146234999dac4bb3350aed352b56061a095719138afd
BLAKE2b-256 checksum
How to use checksums
6d69cc4da89454561dfdca9059309a28cbac7173e6bfb80d782358aa7a32e2cb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.6

Release files / props-0.0.4-py3-none-any.whl

Download URL props-0.0.4-py3-none-any.whl
Size 5.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
581931e71d7d249194bee83ef9543e06c05c0b54eb7201a943dbb9ac1ba9ce1f
BLAKE2b-256 checksum
How to use checksums
38a549736357833f32c4097399774209ef65dd90a3b5113c12978b1ff3b294d0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.6

Release history Release notifications | RSS feed

This release

0.0.4 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page