PyHamcrest
PyHamcrest copied to clipboard
make assert_that user friendly on assertion errors using contains_string
We use hamcrest in screenpy. Something I would like to request is; making the output of assertion errors a little more user friendly. When using string_contains
the output is not lined up to easily see the differences between the expected and the actual.
Example:
very_long_string = """Asserts that actual value satisfies matcher. (Can also assert plain
boolean condition.)
:param actual: The object to evaluate as the actual value.
:param matcher: The matcher to satisfy as the expected condition.
:param reason: Optional explanation to include in failure description.
``assert_that`` passes the actual value to the matcher for evaluation. If
the matcher is not satisfied, an exception is thrown describing the
mismatch."""
another_long_string = """Asserts that actual value satisfies matcher. (Can also assert plain
boolean condition.)
:param actual: The object to evaluate as the actual value.
:param matcher: The matcher to satisfy as the expected condition.
:param reason: Optional explanation to include in failure description.
``assert_that`` passes the actual value to the matcher for evaluation. If
the matcher is not satisfied, an exception is thrown describing the
mismatches"""
assert_that(very_long_string, contains_string(another_long_string))
exception:
Traceback (most recent call last):
File "/Users/me/.pyenv/versions/project/lib/python3.10/site-packages/hamcrest/core/assert_that.py", line 58, in assert_that
_assert_match(actual=actual_or_assertion, matcher=matcher, reason=reason)
File "/Users/me/.pyenv/versions/project/lib/python3.10/site-packages/hamcrest/core/assert_that.py", line 73, in _assert_match
raise AssertionError(description)
AssertionError:
Expected: a string containing 'Asserts that actual value satisfies matcher. (Can also assert plain\n boolean condition.)\n :param actual: The object to evaluate as the actual value.\n :param matcher: The matcher to satisfy as the expected condition.\n :param reason: Optional explanation to include in failure description.\n ``assert_that`` passes the actual value to the matcher for evaluation. If\n the matcher is not satisfied, an exception is thrown describing the\n mismatches'
but: was 'Asserts that actual value satisfies matcher. (Can also assert plain\n boolean condition.)\n :param actual: The object to evaluate as the actual value.\n :param matcher: The matcher to satisfy as the expected condition.\n :param reason: Optional explanation to include in failure description.\n ``assert_that`` passes the actual value to the matcher for evaluation. If\n the matcher is not satisfied, an exception is thrown describing the\n mismatch.'
It would be awesome if the two strings were lined up somehow:
AssertionError:
Expected: a string containing 'Asserts that actual value satisfies matcher. (Can also assert plain\n boolean condition.)\n :param actual: The object to evaluate as the actual value.\n :param matcher: The matcher to satisfy as the expected condition.\n :param reason: Optional explanation to include in failure description.\n ``assert_that`` passes the actual value to the matcher for evaluation. If\n the matcher is not satisfied, an exception is thrown describing the\n mismatches'
but: was 'Asserts that actual value satisfies matcher. (Can also assert plain\n boolean condition.)\n :param actual: The object to evaluate as the actual value.\n :param matcher: The matcher to satisfy as the expected condition.\n :param reason: Optional explanation to include in failure description.\n ``assert_that`` passes the actual value to the matcher for evaluation. If\n the matcher is not satisfied, an exception is thrown describing the\n mismatch.'