summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorStanislav Laznicka <slaznick@redhat.com>2016-09-22 08:12:45 +0200
committerMartin Babinsky <mbabinsk@redhat.com>2016-09-30 09:55:58 +0200
commitecd6cb4e45096f8d6653c6bb2e4701e683ce4e61 (patch)
tree8918c4be4131895b41d29f0e08cd3a78b73ae417
parent81ead980fb808b70d7590800518b655abe64948b (diff)
downloadfreeipa-ecd6cb4e45096f8d6653c6bb2e4701e683ce4e61.tar.gz
freeipa-ecd6cb4e45096f8d6653c6bb2e4701e683ce4e61.tar.xz
freeipa-ecd6cb4e45096f8d6653c6bb2e4701e683ce4e61.zip
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 <mbasti@redhat.com> Reviewed-By: Petr Spacek <pspacek@redhat.com>
-rw-r--r--ipatests/pytest.ini1
-rw-r--r--ipatests/pytest_plugins/additional_config.py8
-rw-r--r--ipatests/util.py35
3 files changed, 37 insertions, 7 deletions
diff --git a/ipatests/pytest.ini b/ipatests/pytest.ini
index 233cf4395..5b8994288 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 000000000..06ae97035
--- /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 0b50f8554..38a3f4bd2 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]