Skip to main content

Assert Matcher Build Status

The purpose of this library is to simplify asserts containing objects and nested data structures, i.e.:

def test_smth(M):  # A special Matcher fixture
    # ...
    assert result.errors == [
        M(message=M.re("^Smth went wrong:"), extensions=M.dict(code=523)),
        M(message=M.any, tags=M.unordered("one", "two")),
    ]

Here all the structures like lists and dicts are followed as usual both outside and inside a matcher object, while the matcher object and other helpers provide their own equality. These could be freely intermixed.

Alternatively may import Matcher directly this is more verbose but also more universal, i.e. works outside of pytest:

from assert_matcher import M

Installation

pip install assert-matcher
pip install git+https://github.com/Suor/assert-matcher.git@master  # the newest one

Matchers

The M fixture provides a number of matchers to help you write more expressive assertions.

M(**attrs)

Matches an object's attributes.

User = namedtuple("User", ["name", "email", "is_active"])

users = [User("Alice", "alice@example.com", True), User("Bob", "bob@example.com", False)]

assert users == [
    M(name="Alice", is_active=True),
    M(name="Bob", is_active=False),
]

# May test class if that is important
assert user == M(__class__=User, name="Alice")

M.re(pattern, flags=0)

Matches a string against a regular expression using re.search.

assert "hello world" == M.re(r"h.*o")

M.dict(d=None, **kwargs)

Matches a dictionary against a subset of keys. It checks that the keys exist and their values match, but ignores other keys.

assert {"a": 1, "b": 2} == M.dict(a=1)
assert {"a": 1, "b": 2} == M.dict({"a": 1})  # same

M.any

Matches any value. This is useful when you want to ignore a particular field value in a larger data structure, but still want to test for a key presence.

assert {"a": 1, "b": 2, "c": "hi"} == M.dict(a=1, b=M.any)

M.any_of(*items)

Matches if the value is one of the provided items.

assert "a" == M.any_of("a", "b", "c")

M.unordered(*items)

Compares a list to the provided items, ignoring the order.

assert [1, 2, 3] == M.unordered(3, 1, 2)

M.isa(*types)

Checks if an object is an instance of one of the provided types.

assert "hello" == M.isa(str)
assert 42 == M.isa(int, float)

M.approx(expected, rel=None, abs=None, nan_ok=False)

Performs approximate comparison of numbers. It's a wrapper around pytest.approx.

assert 0.1 + 0.2 == M.approx(0.3)
assert end_time == M.approx(start_time + timedelta(seconds=1), abs=0.1)

Running tests

To run the tests using your default python:

pip install -r test_requirements.txt
pytest

To fully run tox you need all the supported pythons to be installed. These are 3.9+ and PyPy3. You can run it for particular environment even in absense of all of the above:

tox -e py314
tox -e pypy311
tox -e lint

Pytest Plugin

assert-matcher provides enhanced assertion representation for pytest, making diffs for nested data structures more readable when using Matcher objects.

When a test fails, pytest usually prints a diff of the two objects that were compared. However, these diffs can be hard to read. assert-matcher plugin overrides the default diff representation to pinpoint an exact path and difference.

For example, consider the following failing test:

def test_user(M):
    user = User("Alice", "alice@example.com", True)
    assert user == M(name="Bob", is_active=False)

Without the plugin, the diff would look something like this:

E   AssertionError: assert User(name='Alice', email='alice@example.com', is_active=True) == M(name='Bob', is_active=False)
E    +  where M(name='Bob', is_active=False) = <class 'assert_matcher.matchers.Matcher'>(name='Bob', is_active=False)

With the assert-matcher plugin enabled, the diff is much more readable:

E   AssertionError: assert User(name=...tive=True) == M(name='Bo...ive=False)
E     name: 'Alice' != 'Bob'
E     is_active: True != False

This is even more useful when you compare nested data structures. For example, consider the following test:

def test_nested_structure(M):
    result = {
        "users": [alice, bob],
        "meta": ...
    }
    expected = {
        "users": [
            M(name="Alice", status="active"),
            M(name="Bob", email=M.re(r"@example\.com$")),
        ],
        "meta": M.any
    }
    assert result == expected

Without the plugin, the diff is hard to read (imagine these objects having 10+ fields):

E   AssertionError: assert {'meta': {'er... 'inactive'}]} == {'meta': M.di...ample.com$')]}
E
E     Omitting 1 identical items, use -vv to show
E     Differing items:
E     {'users': [{'email': 'alice@example.com', 'name': 'Alice', 'status': 'active'}, {'email': 'bob@invalid', 'name': 'Bob', 'status': 'inactive'}]} != {'users': [M.dict(name='Alice', status='active'), M.dict(name='Bob', email=r'@example.com$')]}
E     Use -v to get more diff

With the plugin, the diff is much more concise and points to the exact location of the error:

E   AssertionError: assert {'meta': {...active'}} == {'meta': M...e.com$')]}
E     users[1].email: 'bob@invalid' != r'@example.com$'

Metadata

Release files for assert-matcher 0.9.2

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

Source distribution (sdist)

Source distribution for assert-matcher 0.9.2
File Size Uploaded Provenance Actions
assert_matcher-0.9.2.tar.gz 13.6 kB Without provenance Details for assert_matcher-0.9.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for assert-matcher 0.9.2
File Interpreter ABI Platform Actions
assert_matcher-0.9.2-py2.py3-none-any.whl Python 2, Python 3 none any Details for assert_matcher-0.9.2-py2.py3-none-any.whl

Total release size: 24.0 kB

Release files / assert_matcher-0.9.2.tar.gz

Download URL assert_matcher-0.9.2.tar.gz
Size 13.6 kB
Tags Source
SHA-256 checksum
How to use checksums
c67f6d65ef4fa82a9c13e1274023dd39da778781dd4aed6bcfbca8e58a8f66da
BLAKE2b-256 checksum
How to use checksums
d2bbdfc0d0a16e244100c25343d42b8388403d6cb4b73189668ba8cc8bc2b3b3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.3

Release files / assert_matcher-0.9.2-py2.py3-none-any.whl

Download URL assert_matcher-0.9.2-py2.py3-none-any.whl
Size 10.3 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
79e047b2d63fd5131bddca8696c40fa68fde98fabc377c03106d5f95e2dccb69
BLAKE2b-256 checksum
How to use checksums
d652254b32f2d88989bd31a450d7614b340e0e229ea7df126706225d469845d6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.3

Security for assert-matcher 0.9.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.9.2 This release

2 release files No provenance

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