testtools-dev team mailing list archive
-
testtools-dev team
-
Mailing list archive
-
Message #00053
[Merge] lp:~jml/testtools/better-equality-error into lp:testtools
Jonathan Lange has proposed merging lp:~jml/testtools/better-equality-error into lp:testtools.
Requested reviews:
testtools developers (testtools-dev)
This branch changes the formatting of assertEqual failures to be more like bzrlib.
--
https://code.launchpad.net/~jml/testtools/better-equality-error/+merge/33229
Your team testtools developers is requested to review the proposed merge of lp:~jml/testtools/better-equality-error into lp:testtools.
=== modified file 'NEWS'
--- NEWS 2010-08-17 13:08:01 +0000
+++ NEWS 2010-08-20 15:24:51 +0000
@@ -7,6 +7,9 @@
Improvements
------------
+ * 'TestCase.assertEqual' now formats errors a little more nicely, in the
+ style of bzrlib.
+
* 'Mismatch' now takes optional description and details parameters, so
custom Matchers aren't compelled to make their own subclass.
=== modified file 'testtools/testcase.py'
--- testtools/testcase.py 2010-08-04 13:05:20 +0000
+++ testtools/testcase.py 2010-08-20 15:24:51 +0000
@@ -17,6 +17,7 @@
except ImportError:
wraps = None
import itertools
+from pprint import pformat
import sys
import types
import unittest
@@ -222,6 +223,28 @@
content.ContentType('text', 'plain'),
lambda: [reason.encode('utf8')]))
+ def assertEqual(self, expected, observed, message=''):
+ """Assert that 'expected' is equal to 'observed'.
+
+ :param expected: The expected value.
+ :param observed: The observed value.
+ :param message: An optional message to include in the error.
+ """
+ try:
+ return super(TestCase, self).assertEqual(expected, observed)
+ except self.failureException:
+ lines = []
+ if message:
+ lines.append(message)
+ lines.extend(
+ ["not equal:",
+ "a = %s" % pformat(expected),
+ "b = %s" % pformat(observed),
+ ''])
+ self.fail('\n'.join(lines))
+
+ failUnlessEqual = assertEquals = assertEqual
+
def assertIn(self, needle, haystack):
"""Assert that needle is in haystack."""
self.assertTrue(
=== modified file 'testtools/tests/test_testtools.py'
--- testtools/tests/test_testtools.py 2010-08-04 13:05:20 +0000
+++ testtools/tests/test_testtools.py 2010-08-20 15:24:51 +0000
@@ -1,7 +1,8 @@
-# Copyright (c) 2008 Jonathan M. Lange. See LICENSE for details.
+# Copyright (c) 2008-2010 Jonathan M. Lange. See LICENSE for details.
"""Tests for extensions to the base test library."""
+from pprint import pformat
import sys
import unittest
@@ -284,6 +285,23 @@
], calls)
self.assertFalse(result.wasSuccessful())
+ def test_assertEqual_nice_formatting(self):
+ message = "These things ought not be equal."
+ a = ['apple', 'banana', 'cherry']
+ b = {'Thatcher': 'One who mends roofs of straw',
+ 'Major': 'A military officer, ranked below colonel',
+ 'Blair': 'To shout loudly',
+ 'Brown': 'The colour of healthy human faeces'}
+ expected_error = '\n'.join(
+ [message,
+ 'not equal:',
+ 'a = %s' % pformat(a),
+ 'b = %s' % pformat(b),
+ ''])
+ self.assertFails(expected_error, self.assertEqual, a, b, message)
+ self.assertFails(expected_error, self.assertEquals, a, b, message)
+ self.assertFails(expected_error, self.failUnlessEqual, a, b, message)
+
class TestAddCleanup(TestCase):
"""Tests for TestCase.addCleanup."""
Follow ups