Skip to main content

Predicate-based equivalence testing

Project description

predeq

PredEq is a utility for testing objects using an equivalence predicate. It facilitates writing declarative-style tests for complex requirements, and for cases where the default __eq__ is inapplicable or missing.

At its core, predeq(predicate) is an object which tests equal to X if predicate(X) returns True.

from predeq import predeq, instanceof


def test_tuple_of_2_ints():
    """Test that value is a tuple of an int and a string."""

    value = (123, 'abc')

    # without predeq
    assert isinstance(value, tuple) and len(value) == 2
    assert isinstance(value[0], int)
    assert isinstance(value[1], str)

    # with predeq
    assert value == (
        predeq(lambda x: isinstance(x, int)),  # provide your own predicate
        instanceof(str),                       # or use one of the recipes
    )

Since PredEq is primarily designed for tests, it also tries to be helpful when they fail. If lambda is used as a predicate, predeq attempts to locate its source code and show it in pytest output:

>   assert value == (
        predeq(lambda x: isinstance(x, int)),  # provide your own predicate
        instanceof(str),                       # or use one of the recipes
    )
E   AssertionError: assert (123.0, 'abc') == (<predeq to m...stanceof(str))
E
E     At index 0 diff: 123.0 != <predeq to meet lambda x: isinstance(x, int)>
E     Use -v to get more diff

Each built-in recipe also provides a readable representation, which typically looks like the corresponding code:

>   assert value == (
        predeq(lambda x: isinstance(x, int)),  # provide your own predicate
        instanceof(str),                       # or use one of the recipes
    )
E   AssertionError: assert (123, b'abc') == (<predeq to m...stanceof(str))
E
E     At index 1 diff: b'abc' != instanceof(str)
E     Use -v to get more diff

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

predeq-0.0.3.tar.gz (6.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

predeq-0.0.3-py3-none-any.whl (7.3 kB view details)

Uploaded Python 3

File details

Details for the file predeq-0.0.3.tar.gz.

File metadata

  • Download URL: predeq-0.0.3.tar.gz
  • Upload date:
  • Size: 6.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.11.8

File hashes

Hashes for predeq-0.0.3.tar.gz
Algorithm Hash digest
SHA256 fe3ed74a5fa859eeaa5e3a30a8d5480a8440a05c91a6b2f74f76da2fe6ec579c
MD5 ea61881caefcc02d03bd515b2e79f799
BLAKE2b-256 36fa48ef3c749e3d3435dbf49ad49b026406a2d170e57bd05fd32cbff30aed57

See more details on using hashes here.

File details

Details for the file predeq-0.0.3-py3-none-any.whl.

File metadata

  • Download URL: predeq-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 7.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.11.8

File hashes

Hashes for predeq-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 8073e6815269811f97127d38ef526ea9d9b824dc53fde336ae5b176b14db61cd
MD5 4745da3fd54978a08009727c90fb144e
BLAKE2b-256 8c29274c820900afde6e29316ecfdec9f661f7566d156949e2dc18b924b0e2f8

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page