Skip to main content

PyPI version No dependencies Code style: black

^fassert$: Fuzzy assert

Fuzzy assert in your tests only a subset of data that matters

from fassert import fassert

# Usage: fassert(<data>, <template>)

fassert("This is expected", "This is expected")

fassert("This matches as well", re.compile(r"[thismachewl ]+", re.I))

fassert(
    {"key": "value", "key2": "value2"},
    {"key": "value"}
) # key2: value2 is ignored as it's not defined in the template

fassert(
    {"key": "value", "abc": "value"},
    # You can nest and combine the fuzzy matching types in containers
    {re.compile(r"[a-z]{3}"): "value"}
)

fassert(
    [1, {"very": {"nested": {"dictionary": "data"}}}, {"not": "this"}],
    # Isn't this cool?
    [{"very": {re.compile(".{6}"): {"dictionary": lambda x: len(x) == 4}}}]
)

# Template can contain callables as well
fassert("value", lambda x: x == "value")

try:
    fassert("expected", "to not match")
    fassert({"a": "b"}, {"c": "d"})  # This will fail, {"c":"d"} is not in the target data
    fassert([1, 2, 3], [4])
    fassert("string", b"string")  # bytes != string in fassert
except AssertionError:
    pass
else:
    raise RuntimeError("All examples within the try block must raise the AssertionError")

In fassert, you can define a template to match your data against. When you use a type that is a container (e.g. list, tuple, dict, etc...), then only the data that you defined in the template will be asserted. All the addition data in the container will be ignored

Did you know this project is also dedicated to have 0 dependencies on other packages?

Installation

From PyPI: pip install fassert

Locally:

pip install . 

# Run tests with:
python -m unittest discover -s tests/

Advanced usage

fassert function is equivalent in creating a default FuzzyAssert() object and calling match on it. You can configure some behaviour of the fuzzy matcher via the object attributes like so:

from fassert import FuzzyAssert

fasserter = FuzzyAssert()
# Eval functions is turned on by default
fasserter.match("value", lambda x: x == "value")

fasserter.eval_functions = False
# This will now raise Assertion error
fasserter.match("value", lambda x: x == "value")

Bellow is an overview of the configurable options and their default values

Name Default value Description
eval_functions True Enable template matching as callable functions
regex_allowed True Enable matching regexes from template agains strings in the data
fuzzy_sequence_types False Ignore types of similar sequence types when matching the template (e.g. tuple vs list)
check_minimum_sequence_length True Check that the data has a minimum length of greater or equal to the template

You can also define custom data types such as following:

from fassert import fassert, FassertInterface, FuzzyAssert

class IsNumberEvenOrEqual(FassertInterface):
    def __init__(self, value):
        self.value = value

    def __fassert__(self, other: Any, matcher: FuzzyAssert, as_template: bool) -> Literal[True]:
        if self.value == other:
            return True
        elif isinstance(other, (int, float)) and int(other) % 2 == 0:
            return True
        
        raise AssertionError("Data does not match the template")


# In these examples the parameter `as_template` would be set to True as the data type is used as a template for matching
fassert(10, IsNumberEvenOrEqual(15))
fassert(15, IsNumberEvenOrEqual(15))
fassert(42.0, IsNumberEvenOrEqual(15))

try:
    fassert(15, IsNumberEvenOrEqual(17))
    fassert("some_string", IsNumberEvenOrEqual(15))
except AssertionError:
    pass
else:
    raise RuntimeError("All examples within the try block must raise the AssertionError")

Metadata

Release files for fassert 0.3

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

Source distribution (sdist)

Source distribution for fassert 0.3
File Size Uploaded Provenance
fassert-0.3.tar.gz 4.9 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for fassert 0.3
File Interpreter ABI Platform
fassert-0.3-py3-none-any.whl Python 3 none any Details

Total release size: 10.1 kB

Release files / fassert-0.3.tar.gz

Download URL fassert-0.3.tar.gz
Size 4.9 kB
Tags Source
SHA-256 checksum
How to use checksums
4b4fa5e1920f980a6c6708f78944b5681517fa3b14dddda781c1f472e37961af
BLAKE2b-256 checksum
How to use checksums
76149557e587d292bb3c8a058a35daa91aaa24d18ed0547c8d6f614ab9b5ffa6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.63.1 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.8.9

Release files / fassert-0.3-py3-none-any.whl

Download URL fassert-0.3-py3-none-any.whl
Size 5.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7a77849ee148eca1b9817fc1452b657ca2749a3c44750f3c6d17476418b6b71d
BLAKE2b-256 checksum
How to use checksums
214acc0fe9c516051c5640a1bd31a13fee42175340a3b2168b8a792741dee795
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.63.1 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.8.9

Security for fassert 0.3

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.3 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