Anys: Pytest Matchers

0.3.1 · active · verified Sun Apr 12

anys provides a collection of matchers designed for pytest-style assertions, enabling more flexible comparisons in unit tests. Instead of strict equality checks, it allows developers to assert that values meet specific criteria (e.g., `ANY_DATETIME_STR` for any valid ISO 8601 timestamp string). This simplifies testing dynamic or partially unpredictable data structures. The library is actively maintained, with the current version being 0.3.1.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `anys` for flexible assertions, particularly with dynamic data like API responses. It uses `ANY_STR` to match any string and `ANY_DATETIME_STR` to match any valid ISO 8601 datetime string, allowing the test to pass even if the exact values for `id`, `created_at`, `status`, or `last_login` are not known in advance.

from anys import ANY_DATETIME_STR

def test_api_response():
    # Imagine this is a dynamic response from an API or function
    dynamic_data = {
        "id": "user-123",
        "name": "Test User",
        "created_at": "2024-04-12T10:30:00Z",
        "details": {
            "status": "active",
            "last_login": "2024-04-12T10:29:55Z"
        }
    }

    # Assert against a structure with matchers for dynamic fields
    expected_pattern = {
        "id": ANY_STR, # Match any string
        "name": "Test User",
        "created_at": ANY_DATETIME_STR, # Match any valid ISO datetime string
        "details": {
            "status": ANY_STR,
            "last_login": ANY_DATETIME_STR
        }
    }

    assert dynamic_data == expected_pattern
    print("Assertion successful: Dynamic data matches expected pattern!")

test_api_response()

view raw JSON →