Skip to main content

A fluent DSL for python assertions.

Project description

Asserto

version codecov docs

Asserto:

Asserto is a clean, fluent and powerful assertion library for python. We recommend using pytest as a test runner but asserto will work well with any test runner.

Asserto was developed using pytest as it's test runner and has a pytest-asserto plugin that exposes asserto through a fixture. Asserto will work on any runner or even without one. Note: It is common practice for a test runner to apply assertion rewriting to change the behaviour of the assert keyword under the hood.

The main features of asserto are (and will be):

  • Chainable and Fluent API.
  • Ability for both Hard and Soft assertions.
  • Rich diffs to highlight problems, reduce churn and improve effeciency and debuggability.
  • Dynamic assertions; check any obj attribute or invoke any of it's function types.
  • Robust set of methods out of the box for common types.
  • Extensibility. Bolt on your own assertions at runtime.
  • Human error detection, elaborate warnings when something is amiss.
  • Much more to come.

Feature Set:

Fluent API:

Asserto exposes a fully fluent API for chaining assertions against a value.

from asserto import asserto


def test_multiple_assert_fluency() -> None:
    asserto("Hello").has_length(5).match(r"\w{5}$").ends_with("lo").starts_with("Hel")

Soft Assertions:

Asserto Has soft capabilities; allowing multiple assertions to be performed before failing with a summary of the failures.

from asserto import asserto

def test_baz() -> None:
    with asserto("Baz") as context:
        # asserto when used in a python context is run in 'soft' mode;
        # upon exiting the context; congregated errors are subsequently raised (if any)
        context.starts_with("B").ends_with("z").is_equal_to("Baz").has_length(2)  # Ends in a failure.

Will result in the following:

    def test_foo(asserto) -> None:
>       with asserto("Bar") as context:
E       AssertionError: 1 Soft Assertion Failures
E       [AssertionError("Length of: 'Bar' was not equal to: 2")]

Exception Handling:

Asserto has the ability to assert exceptions are raised on callables using a simple API.

from asserto import asserto
import typing


def simple_callable(x: int) -> typing.NoReturn:
    raise ValueError(x)


def test_exc_handling():
    asserto(simple_callable).should_raise(ValueError).when_called_with(25)

Dynamic Lookups:

Asserto has the ability to dynamically lookup attributes on any object type. This is handled using the attr_is(expected) syntax.

from asserto import asserto


class Foo:

    def __init__(self, x) -> None:
        self.x = x

    def double_x(self) -> int:
        return self.x * 2


def test_foo_dynamically() -> None:
     # dynamically looking up `x` (attr) or `double_x` bound method & invoking it!
    asserto(Foo(10)).x_is(10).double_x_is(20)

Dynamic assert registration

Asserto allows users to easily bolt on their assertion functions.

from asserto import asserto
from asserto import register_assert


@register_assert  # Option 1.
def custom_assert(self):
    if self.actual != 5:
        self.check_should_raise(f"{self.actual} did not equal five!")


register_assert(custom_assert)  # Option 2


def test_user_defined_callables() -> None:
    asserto(6).custom_assert()

yields the following:

E       AssertionError: 6 did not equal five!

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

asserto-0.0.6a0.tar.gz (17.4 kB view details)

Uploaded Source

Built Distribution

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

asserto-0.0.6a0-py3-none-any.whl (21.7 kB view details)

Uploaded Python 3

File details

Details for the file asserto-0.0.6a0.tar.gz.

File metadata

  • Download URL: asserto-0.0.6a0.tar.gz
  • Upload date:
  • Size: 17.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.13 CPython/3.10.5 Linux/5.13.0-1029-azure

File hashes

Hashes for asserto-0.0.6a0.tar.gz
Algorithm Hash digest
SHA256 495c5f642548bc5ac6d8f04b5b407ac2e4a194d3bb9965c30f381bf3246074df
MD5 6112e05931b38a91c2188f71103ecab3
BLAKE2b-256 5314d1d1307ccebf118950c2b16329f3bf28f45c5151937d7546dfb072f7770c

See more details on using hashes here.

File details

Details for the file asserto-0.0.6a0-py3-none-any.whl.

File metadata

  • Download URL: asserto-0.0.6a0-py3-none-any.whl
  • Upload date:
  • Size: 21.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.13 CPython/3.10.5 Linux/5.13.0-1029-azure

File hashes

Hashes for asserto-0.0.6a0-py3-none-any.whl
Algorithm Hash digest
SHA256 dc32820b151c08bab8b8b2f51b695473646fbb605e6f3f499a79837c464d4f6f
MD5 c532d7823a80b769dbf8b28a038cd69c
BLAKE2b-256 4505aaa765fa75fdbe4605f3b0a5c5b09086d8b16536648e14c33826d22a153e

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