Skip to main content

Nicer list and iterable assertion messages for pytest

Project description

License: MPL 2.0 Build Code style: black

pytest-iterassert

Have you ever wanted to use all or any in a unit test, but found the assert message to be lacking? Do assertions on class attributes in collections almost make you wish you were coding in Java (with a nice assertion framework)?

Then this is the pytest helper for you! pytest-iterassert provides all_match and any_match to give you nice asserts.

The built-in any or all can cause a lot of sadness when tests fail:

    def test_generator_without_iterassert() -> None:
>       assert all(i < 1 for i in range(3))
E       assert False
E        +  where False = all(<genexpr> at 0x10221a250>)

all_match and any_match make debugging easy:

    def test_generator_with_iterassert() -> None:
>       assert all_match(range(3)) < 1
E       assert all(0, 1, 2) < 1
E        +  where all(0, 1, 2) = all_match(range(0, 3))
E        +    where range(0, 3) = range(3)

How about a more complex example? Asserting attributes of a class instance is pretty common.

    def test_attr_of_classes_without_iterassert() -> None:
        foos = [Foo(1), Foo(2), Foo(3)]
>       assert all(foo.bar < 3 for foo in foos)
E       assert False
E        +  where False = all(<genexpr> at 0x10597ca50>)

iterassert makes it easy to apply functions to the iterable, and will convince pytest to show you the result of that function!

    def test_attr_of_classes_with_iterassert() -> None:
        foos = [Foo(1), Foo(2), Foo(3)]
>       assert all_match(foos, get_bar) < 3
E       assert all(9001, 9002, 9003) < 3
E        +  where all(9001, 9002, 9003) = all_match([<Foo(1)>, <Foo(2)>, <Foo(3)>], get_bar)

Even the test summary says it all:

FAILED example.py::test_generator_without_iterassert - assert False
FAILED example.py::test_generator_with_iterassert - assert all(0, 1, 2) < 1
FAILED example.py::test_attr_of_classes_without_iterassert - assert False
FAILED example.py::test_attr_of_classes_with_iterassert - assert all(9001, 9002, 9003) < 3

pytest-iterassert is on PyPI, so you can simply install via pip install pytest-iterassert (requires Python 3.6 or higher).

Development

This library uses Poetry for managing dependencies. You just need to run poetry install, and it will create a virtual environment with all developer dependencies installed.

Please run poetry run ./lint before submitting pull requests.

License

This library is licensed under the Mozilla Public License Version 2.0. For more information, see LICENSE.

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-iterassert-0.0.2.tar.gz (9.2 kB view details)

Uploaded Source

Built Distribution

pytest_iterassert-0.0.2-py3-none-any.whl (8.8 kB view details)

Uploaded Python 3

File details

Details for the file pytest-iterassert-0.0.2.tar.gz.

File metadata

  • Download URL: pytest-iterassert-0.0.2.tar.gz
  • Upload date:
  • Size: 9.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.5 CPython/3.6.10 Linux/5.0.0-1035-azure

File hashes

Hashes for pytest-iterassert-0.0.2.tar.gz
Algorithm Hash digest
SHA256 02a4cce154be5e898c2f42a6f8d6a2cce44edcd7963cb48d9105ac86513287b0
MD5 63f92be4bc6f4ec372ce832d4eef04e0
BLAKE2b-256 66e9875384f6ee0838ae3e03a8f8e2a5b148ceef667acbbf0fd3e83f80080562

See more details on using hashes here.

File details

Details for the file pytest_iterassert-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: pytest_iterassert-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 8.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.5 CPython/3.6.10 Linux/5.0.0-1035-azure

File hashes

Hashes for pytest_iterassert-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 23198317d59fa84ba73e8df71fa2fb7eed12aa5d1c8924eff473fcfdd4445085
MD5 ab685a1dc8c750cc449ffc55f3187a3a
BLAKE2b-256 0841b140a55c0d3c19598967112e771b2234aeab8282265917aa2efae80aa814

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page