From ecd6cb4e45096f8d6653c6bb2e4701e683ce4e61 Mon Sep 17 00:00:00 2001 From: Stanislav Laznicka Date: Sep 30 2016 07:55:58 +0000 Subject: Pretty-print structures in assert_deepequal By default, ipa-run-tests will now pretty-print structures compared in the assert_deepequal function. This behaviour can be turned off by the --no-pretty-print option. https://fedorahosted.org/freeipa/ticket/6212 Reviewed-By: Martin Basti Reviewed-By: Petr Spacek --- diff --git a/ipatests/pytest.ini b/ipatests/pytest.ini index 233cf43..5b89942 100644 --- a/ipatests/pytest.ini +++ b/ipatests/pytest.ini @@ -12,6 +12,7 @@ addopts = --doctest-modules -p ipatests.pytest_plugins.declarative -p ipatests.pytest_plugins.integration -p ipatests.pytest_plugins.beakerlib + -p ipatests.pytest_plugins.additional_config # Ignore files for doc tests. # TODO: ideally, these should all use __name__=='__main__' guards --ignore=setup.py diff --git a/ipatests/pytest_plugins/additional_config.py b/ipatests/pytest_plugins/additional_config.py new file mode 100644 index 0000000..06ae970 --- /dev/null +++ b/ipatests/pytest_plugins/additional_config.py @@ -0,0 +1,8 @@ +# +# Copyright (C) 2016 FreeIPA Contributors see COPYING for license +# + + +def pytest_addoption(parser): + parser.addoption("--no-pretty-print", action="store_false", + dest="pretty_print", help="Don't pretty-print structures") diff --git a/ipatests/util.py b/ipatests/util.py index 0b50f85..38a3f4b 100644 --- a/ipatests/util.py +++ b/ipatests/util.py @@ -28,7 +28,9 @@ import tempfile import shutil import re import uuid +import pytest from contextlib import contextmanager +from pprint import pformat import six import ldap @@ -273,18 +275,29 @@ LEN = """assert_deepequal: list length mismatch. %s len(expected) = %r len(got) = %r - expected = %r - got = %r + expected = %s + got = %s path = %r""" KEYS = """assert_deepequal: dict keys mismatch. %s missing keys = %r extra keys = %r - expected = %r - got = %r + expected = %s + got = %s path = %r""" +EXPECTED_LEN = len(' expected = ') +GOT_LEN = len(' got = ') + + +def struct_to_string(struct, indent=1): + """ + Function to pretty-format a structure and optionally indent its lines + so they match the visual indention of the first line + """ + return pformat(struct).replace('\n', '\n' + ' ' * indent) + def assert_deepequal(expected, got, doc='', stack=tuple()): """ @@ -315,6 +328,13 @@ def assert_deepequal(expected, got, doc='', stack=tuple()): Note that lists and tuples are considered equivalent, and the order of their elements does not matter. """ + if pytest.config.getoption("pretty_print"): # pylint: disable=no-member + expected_str = struct_to_string(expected, EXPECTED_LEN) + got_str = struct_to_string(got, GOT_LEN) + else: + expected_str = repr(expected) + got_str = repr(got) + if isinstance(expected, tuple): expected = list(expected) if isinstance(got, tuple): @@ -329,7 +349,8 @@ def assert_deepequal(expected, got, doc='', stack=tuple()): if isinstance(expected, (list, tuple)): if len(expected) != len(got): raise AssertionError( - LEN % (doc, len(expected), len(got), expected, got, stack) + LEN % (doc, len(expected), len(got), expected_str, got_str, + stack) ) # Sort list elements, unless they are dictionaries if expected and isinstance(expected[0], dict): @@ -352,8 +373,8 @@ def assert_deepequal(expected, got, doc='', stack=tuple()): extra = set(got).difference(expected) if missing or extra: raise AssertionError(KEYS % ( - doc, sorted(missing), sorted(extra), expected, got, stack - ) + doc, sorted(missing), sorted(extra), expected_str, got_str, + stack) ) for key in sorted(expected): e_sub = expected[key]