Skip to main content

Useful assertion utilities for use with pytest

Project description

pytest-assert-utils

Handy assertion utilities for use with pytest

Installation

pip install pytest-assert-utils

Usage

assert_dict_is_subset

def assert_dict_is_subset(subset, superset, recursive=True)

Assert subset is a non-strict subset of superset

If this assertion fails, a pretty diff will be printed by pytest.

>>> from pytest_assert_utils import assert_dict_is_subset

>>> expected = {'a': 12}
>>> actual = {'b': 20, 'a': 12}
>>> assert_dict_is_subset(expected, actual)

>>> expected = {'a': 12}
>>> actual = {'b': 50000}
>>> assert_dict_is_subset(expected, actual)
Traceback (most recent call last):
 ...
AssertionError

assert_model_attrs

def assert_model_attrs(instance, _d=UNSET, **attrs)

Assert a model instance has the specified attr values

May be passed a dict of attrs, or kwargs as attrs

>>> from pytest_assert_utils import assert_model_attrs

>>> from collections import namedtuple
>>> Model = namedtuple('Model', 'id,key,other_key,parent', defaults=(None,)*4)

>>> assert_model_attrs(Model(), {})

>>> assert_model_attrs(Model(key='value'), {'key': 'value'})
>>> assert_model_attrs(Model(key='value'), key='value')
>>> assert_model_attrs(Model(key='value'), key='not the value')
Traceback (most recent call last):
 ...
AssertionError

>>> assert_model_attrs(Model(key='value', other_key='other_value'), key='value')

Any

Meta-value which compares True to any object (of the specified type(s))

>>> from pytest_assert_utils import util

>>> util.Any() == 'stuff'
True
>>> util.Any() == 1
True
>>> util.Any() == None
True
>>> util.Any() == object()
True

>>> util.Any(int) == 1
True
>>> util.Any(int) == '1'
False

Optional

Meta-value which compares True to None or the optionally specified value

>>> from pytest_assert_utils import util

>>> util.Optional() == None
True
>>> util.Optional() is None  # this will not work!
False
>>> util.Optional(24) == 24
True
>>> util.Optional(24) == None
True

>>> util.Optional(Any(int)) == 1
True
>>> util.Optional(Any(int)) == None
True
>>> util.Optional(Any(int)) == '1'
False

Collection

Special class enabling equality comparisons to check items in any collection (list, set, tuple, etc)

>>> from pytest_assert_utils import util

>>> util.Collection.containing(1) == [1, 2, 3]
True
>>> util.Collection.containing(1) == {1, 2, 3}
True
>>> util.Collection.containing(1) == (1, 2, 3)
True

>>> util.Collection.containing(1) == [4, 5, 6]
False
>>> util.Collection.containing(1) == {4, 5, 6}
False
>>> util.Collection.containing(1) == (4, 5, 6)
False

List

Special class enabling equality comparisons to check items in a list

>>> from pytest_assert_utils import util

>>> util.List.containing(1) == [1, 2, 3]
True
>>> util.List.containing(1) == [4, 5, 6]
False

>>> util.List.containing_only(1, 2) == [1, 2, 3]
False
>>> util.List.containing_only(1, 2) == [1, 2, 2]
True
>>> util.List.containing_only(4, 5, 6) == [4, 5, 6]
True
>>> util.List.containing_only(4, 5, 6, 7) == [4, 5, 6]
True

>>> util.List.containing_exactly(1, 2) == [1, 2, 3]
False
>>> util.List.containing_exactly(4, 5, 6, 7) == [4, 5, 6]
False
>>> util.List.containing_exactly(5, 6, 4) == [4, 5, 6]
True
>>> util.List.containing_exactly(4, 5) == [4, 5, 5]
False
>>> util.List.containing_exactly(5, 4, 5) == [4, 5, 5]
True

>>> util.List.not_containing(1) == [1, 2, 3]
False
>>> util.List.not_containing(1) == [4, 5, 6]
True

>>> util.List.empty() == [1, 2, 3]
False
>>> util.List.empty() == []
True

>>> util.List.not_empty() == [1, 2, 3]
True
>>> util.List.not_empty() == []
False

Set

Special class enabling equality comparisons to check items in a set

>>> from pytest_assert_utils import util

>>> util.Set.containing(1) == {1, 2, 3}
True
>>> util.Set.containing(1) == {4, 5, 6}
False

>>> util.Set.not_containing(1) == {1, 2, 3}
False
>>> util.Set.not_containing(1) == {4, 5, 6}
True

>>> util.Set.empty() == {1, 2, 3}
False
>>> util.Set.empty() == set()
True

>>> util.Set.not_empty() == {1, 2, 3}
True
>>> util.Set.not_empty() == set()
False

Dict

Special class enabling equality comparisons to check items in a dict

>>> from pytest_assert_utils import util

>>> util.Dict.containing('a') == {'a': 1, 'b': 2}
True
>>> util.Dict.containing(a=1) == {'a': 1, 'b': 2}
True
>>> util.Dict.containing({'a': 1}) == {'a': 1, 'b': 2}
True
>>> util.Dict.containing('a') == {'b': 2}
False
>>> util.Dict.containing(a=1) == {'b': 2}
False
>>> util.Dict.containing({'a': 1}) == {'b': 2}
False

>>> util.Dict.not_containing('a') == {'a': 1, 'b': 2}
False
>>> util.Dict.not_containing(a=1) == {'a': 1, 'b': 2}
False
>>> util.Dict.not_containing({'a': 1}) == {'a': 1, 'b': 2}
False
>>> util.Dict.not_containing('a') == {'b': 2}
True
>>> util.Dict.not_containing(a=1) == {'b': 2}
True
>>> util.Dict.not_containing({'a': 1}) == {'b': 2}
True

>>> util.Dict.empty() == {'a': 1, 'b': 2, 'c': 3}
False
>>> util.Dict.empty() == {}
True

>>> util.Dict.not_empty() == {'a': 1, 'b': 2, 'c': 3}
True
>>> util.Dict.not_empty() == {}
False

Str

Special class enabling equality comparisons to check items in a string

>>> from pytest_assert_utils import util

>>> util.Str.containing('app') == 'apple'
True
>>> util.Str.containing('app') == 'happy'
True
>>> util.Str.containing('app') == 'banana'
False

>>> util.Str.not_containing('app') == 'apple'
False
>>> util.Str.not_containing('app') == 'happy'
False
>>> util.Str.not_containing('app') == 'banana'
True

>>> util.Str.empty() == 'hamster'
False
>>> util.Str.empty() == ''
True

>>> util.Str.not_empty() == 'hamster'
True
>>> util.Str.not_empty() == ''
False

Model

Special class enabling equality comparisons to check attrs of another object

>>> from collections import namedtuple
>>> Foo = namedtuple('Foo', 'id,key,other_key,parent', defaults=(None,)*4)

>>> Foo() == Model()
True

>>> Foo(key='value') == Model(key='value')
True
>>> Foo(key='value') == Model(key='not the value')
False
>>> Foo(key='value', other_key='other_value') == Model(key='value')
True
>>> [Foo(key='value', other_key='other_value')] == List.containing(Model(key='value'))
True

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

pytest-assert-utils-0.3.0.tar.gz (11.6 kB view details)

Uploaded Source

Built Distribution

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

pytest_assert_utils-0.3.0-py3-none-any.whl (7.9 kB view details)

Uploaded Python 3

File details

Details for the file pytest-assert-utils-0.3.0.tar.gz.

File metadata

  • Download URL: pytest-assert-utils-0.3.0.tar.gz
  • Upload date:
  • Size: 11.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.9 CPython/3.8.10 Linux/5.4.0-84-generic

File hashes

Hashes for pytest-assert-utils-0.3.0.tar.gz
Algorithm Hash digest
SHA256 68dbcd6e06104d7a4a3b5725ba2eb6383cdbd96c9ac967cf9cd350105c400827
MD5 d8212948c75f9a7ed05e0fd08f570bf8
BLAKE2b-256 6fde1b0c8fc7abc4242836f23618fe0af3ac3cb2a3fe2bf5c94610be2b764d1c

See more details on using hashes here.

File details

Details for the file pytest_assert_utils-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: pytest_assert_utils-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 7.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.9 CPython/3.8.10 Linux/5.4.0-84-generic

File hashes

Hashes for pytest_assert_utils-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7a7c8b448dd9a001a22ec6159d6136752b8d096cf96e7e996e2f699945c7435e
MD5 ab815d3433b00b7044f6ba9d331e5e4a
BLAKE2b-256 20c3bb5499b228612609c9f44cc114a67e05242bc0e3b7d709c1d348685a8997

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