Worth is a Python library that helps you assert relevant object attributes
For example
from worth import Omit
def test_something_like():
a = Model(id=12345, name="Hoff")
b = Model(id=67890, name="Hoff")
assert a == b | Omit("id")
It implements some helpers
Always
Always()
assertion is always true.
assert "a string" == Always()
assert 42 == Always()
assert False == Always()
Never
Never()
assertion is always false.
assert "a string" == Never()
assert 42 == Never()
assert False == Never()
OneOf
OneOf()
asserts one of its values.
assert "foo" == OneOf("foo", "bar")
assert "bar" == OneOf("foo", "bar")
assert "qux" != OneOf("foo", "bar")
Things that applies to models (dataclasses, attrs, or msgspec)
Omit
Omit()
let you to exclude some properties of you model from the comparison.
a = Model(id=12345, name="Hoff")
b = Model(id=67890, name="Hoff")
assert a == b | Omit("id")
Only
Only()
let you to choose precisely which properties you want to compair.
a = Model(id=12345, name="Hoff")
b = Model(id=67890, name="Hoff")
assert a == b | Only("name")
Things that applies to mappings
contains
contains()
let you to compare some mapping items.
assert {"foo": 42, "bar": True} == contains({"foo": 42})
assert {"foo": 42, "bar": True} != contains({"foo": "wrong"})
- dataclasses
- attrs
- msgspec
- vanilla