Skip to main content

A small assertion library for testing

Project description

Assertive

Assertive is a lightweight Python assertion library for writing declarative, composable test expectations.

Install

pip install assertive

Basic usage

Criteria are objects that implement a match operation. You compare a subject to criteria with ==.

from assertive import is_eq, is_gt, is_odd

assert 5 == is_eq(5)
assert 5 == is_gt(4)
assert 5 == is_odd()

Composition

from assertive import is_gt, is_lt, is_even

assert 8 == is_gt(0) & is_lt(10)
assert 8 == is_even() | is_lt(0)
assert 8 == ~is_lt(0)

Nested Criteria

You can nest criteria inside other criteria. This is especially useful for dicts and lists.

Dict values can be criteria

from assertive import has_key_values, is_gt, starts_with

user = {"name": "Ada", "age": 37, "team": "platform"}

assert user == has_key_values({
    "name": starts_with("A"),
    "age": is_gt(18),
})

List positions can be criteria

Use contains_exactly when position/order matters. You can place criteria in specific slots.

from assertive import contains_exactly, is_gt, starts_with

record = ["user_123", 42, "active"]

assert record == contains_exactly(
    starts_with("user_"),  # first item
    is_gt(40),             # second item
    "active",              # third item
)

Asserting Mock Calls

You can assert on Mock and AsyncMock interactions with the same criteria style.

from unittest.mock import Mock
from assertive import was_called, was_called_once, was_called_once_with, was_called_with

gateway = Mock()
gateway.charge(customer_id="cus_123", amount=2500, currency="USD")

assert gateway.charge == was_called().once()
assert gateway.charge == was_called_once()
assert gateway.charge == was_called_with(customer_id="cus_123", amount=2500, currency="USD").once()
assert gateway.charge == was_called_once_with(amount=2500, currency="USD")

Advanced example 1: rich argument matching with call-count constraints

from unittest.mock import Mock
from assertive import is_gt, starts_with, was_called_with

gateway = Mock()
gateway.charge("cus_001", amount=1200, currency="USD", retry_count=1)
gateway.charge("cus_002", amount=2600, currency="USD", retry_count=2)

assert gateway.charge == was_called_with(
    starts_with("cus_"),
    amount=is_gt(1000),
    currency="USD",
    retry_count=is_gt(0),
).twice()

Advanced example 2: async await assertions with nested criteria

import asyncio
from unittest.mock import AsyncMock
from assertive import has_key_values, is_gt, was_awaited_once_with

publisher = AsyncMock()

async def publish():
    await publisher.send(
        "/events",
        payload={"type": "purchase", "amount": 4999, "meta": {"source": "checkout"}},
    )

asyncio.run(publish())

assert publisher.send == was_awaited_once_with(
    "/events",
    payload=has_key_values({"type": "purchase", "amount": is_gt(0)}),
)

Documentation

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

assertive-1.4.0.tar.gz (14.0 kB view details)

Uploaded Source

Built Distribution

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

assertive-1.4.0-py3-none-any.whl (18.6 kB view details)

Uploaded Python 3

File details

Details for the file assertive-1.4.0.tar.gz.

File metadata

  • Download URL: assertive-1.4.0.tar.gz
  • Upload date:
  • Size: 14.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.9 {"installer":{"name":"uv","version":"0.10.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for assertive-1.4.0.tar.gz
Algorithm Hash digest
SHA256 47cb88dfe75d9cdf9fb2cb5033c020beea1471c7d4bf19a0e1799c1c94363f8e
MD5 43ee5579c72c3c9956423a9801839024
BLAKE2b-256 23342c979387d438808e1e278bd1df70e602a8b3fed3e0f7e2ff1a47cf8f32e0

See more details on using hashes here.

File details

Details for the file assertive-1.4.0-py3-none-any.whl.

File metadata

  • Download URL: assertive-1.4.0-py3-none-any.whl
  • Upload date:
  • Size: 18.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.9 {"installer":{"name":"uv","version":"0.10.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for assertive-1.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c631b69d2cfbd62e7e8da1ecd9f8bffc2d377f1fbbde6f66af826ee1d3a639ab
MD5 9e5190936ab55d1a70274c8fadb2856d
BLAKE2b-256 37ab639f60db9cf4f513842e3db427c315b58991456d1569bf4e8be83973ea43

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