Skip to content

Instantly share code, notes, and snippets.

@romuald
Last active February 22, 2022 15:55
Show Gist options
  • Save romuald/851b64837218de86e123b51db2a38113 to your computer and use it in GitHub Desktop.
Save romuald/851b64837218de86e123b51db2a38113 to your computer and use it in GitHub Desktop.
TestCase that unifies representations str/unicode and int/long
# -*- coding: utf-8 -*-
import pprint
import unittest
class PrettyDiffPrinter(pprint.PrettyPrinter):
"""A pprint subclass to hide some differences
that usually don't matter in tests
"""
_original_pformat = None
def _repr(self, object, context, level):
if isinstance(object, long):
# If object is a long, don't represent it with a trailing 'L'
# (unless it doesn't fit in a python int)
object = int(object)
elif isinstance(object, unicode):
# If object is an unicode string, but ASCII only,
# don't represent it with the leading 'u'
try:
object = object.encode('ascii')
except UnicodeEncodeError:
pass
return pprint.PrettyPrinter._repr(self, object, context, level)
def ppformat(self, object, *unused):
# method replacing the pprint.pformat static one during patch
return self.pformat(object)
def __enter__(self):
self._original_pformat = pprint.pformat
pprint.pformat = self.ppformat
def __exit__(self, *unused):
pprint.pformat = self._original_pformat
class PrettyTestCase(unittest.TestCase):
_printer = PrettyDiffPrinter()
def assertDictEqual(self, *args, **kwargs):
with self._printer:
return super(PrettyTestCase, self).assertDictEqual(*args, **kwargs)
def assertSequenceEqual(self, *args, **kwargs):
with self._printer:
return super(PrettyTestCase, self) \
.assertSequenceEqual(*args, **kwargs)
class DemoTest(PrettyTestCase):
def test_fail_dict(self):
# Expect, as defined by test creator
expect = {'water': 3,
'base': u'café',
'fruits': {'oranges': 1, 'apples': 3},
'vegetables': {'carrots': 2, 'potatoes': 4},
}
# Result, as actually returned by the library being tested
result = {u'water': 3,
u'base': u'café',
u'fruits': {u'oranges': 1, u'apples': 3},
u'vegetables': {u'carrots': 2, u'potatoes': 5},
}
self.assertEqual(result, expect)
def test_fail_list(self):
# Expect, as defined by test creator
expect = [
{'value': 1},
{'value': 2},
{'value': 3},
{'value': 4},
{'value': 5},
{'value': 6}]
# Result, as actually returned by the library being tested
result = [
{'value': 1L},
{'value': 3L},
{'value': 3L},
{'value': 4L},
{'value': 5L},
{'value': 6L}]
self.assertEqual(result, expect)
if __name__ == '__main__':
unittest.main()
"""
======================================================================
FAIL: test_fail_dict (pretty_testcase.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "pretty_testcase.py", line 70, in test_fail_dict
self.assertEqual(result, expect)
AssertionError: {u'water': 3, u'vegetables': {u'potatoes': 5, u'carrots': 2}, u'base': u'caf\xe9 [truncated]... != {'water': 3, 'vegetables': {'potatoes': 4, 'carrots': 2}, 'base': u'caf\xe9', 'f [truncated]...
{'base': u'caf\xe9',
'fruits': {'apples': 3, 'oranges': 1},
- 'vegetables': {'carrots': 2, 'potatoes': 5},
? ^
+ 'vegetables': {'carrots': 2, 'potatoes': 4},
? ^
'water': 3}
======================================================================
FAIL: test_fail_list (pretty_testcase.DemoTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "pretty_testcase.py", line 91, in test_fail_list
self.assertEqual(result, expect)
AssertionError: Lists differ: [{'value': 1L}, {'value': 3L},... != [{'value': 1}, {'value': 2}, {...
First differing element 1:
{'value': 3L}
{'value': 2}
[{'value': 1},
- {'value': 3},
? ^
+ {'value': 2},
? ^
{'value': 3},
{'value': 4},
{'value': 5},
{'value': 6}]
----------------------------------------------------------------------
Ran 2 tests in 0.002s
FAILED (failures=2)
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment