← Back to team overview

testtools-dev team mailing list archive

[Merge] lp:~jml/testtools/assertThat-less-verbose-675323 into lp:testtools

 

Jonathan Lange has proposed merging lp:~jml/testtools/assertThat-less-verbose-675323 into lp:testtools.

Requested reviews:
  testtools developers (testtools-dev)
Related bugs:
  Bug #593190 in testtools: "assertThat unconditionally prints matchee on mismatch"
  https://bugs.launchpad.net/testtools/+bug/593190
  Bug #675323 in testtools: "assertThat style gives overly verbose output"
  https://bugs.launchpad.net/testtools/+bug/675323

For more details, see:
https://code.launchpad.net/~jml/testtools/assertThat-less-verbose-675323/+merge/68575

This changes assertThat to show only the mismatch description by default. It allows users to get the old-style verbose output by passing verbose=True into assertThat.

This is also how hamcrest does it:

http://www.google.com/codesearch#nmwoqoPm7yk/hamcrest-java/hamcrest-core/src/main/java/org/hamcrest/MatcherAssert.java&q=assertThat%20package:http://hamcrest%5C.googlecode%5C.com

Every matcher that we have includes the matchee in the description, and knowing the exact matcher is rarely ever useful.

-- 
https://code.launchpad.net/~jml/testtools/assertThat-less-verbose-675323/+merge/68575
Your team testtools developers is requested to review the proposed merge of lp:~jml/testtools/assertThat-less-verbose-675323 into lp:testtools.
=== modified file 'NEWS'
--- NEWS	2011-07-20 08:48:46 +0000
+++ NEWS	2011-07-20 17:09:46 +0000
@@ -10,6 +10,10 @@
 * All public matchers are now in ``testtools.matchers.__all__``.
   (Jonathan Lange, #784859)
 
+* assertThat output is much less verbose, displaying only what the mismatch
+  tells us to display. Old-style verbose output can be had by passing
+  ``verbose=True`` to assertThat. (Jonathan Lange, #675323, #593190)
+
 * Automated the Launchpad part of the release process.
   (Jonathan Lange, #623486)
 

=== modified file 'testtools/testcase.py'
--- testtools/testcase.py	2011-06-20 11:57:32 +0000
+++ testtools/testcase.py	2011-07-20 17:09:46 +0000
@@ -388,7 +388,7 @@
             self.fail("%s not raised, %r returned instead." % (excName, ret))
     failUnlessRaises = assertRaises
 
-    def assertThat(self, matchee, matcher):
+    def assertThat(self, matchee, matcher, verbose=False):
         """Assert that matchee is matched by matcher.
 
         :param matchee: An object to match with matcher.
@@ -396,7 +396,7 @@
         :raises self.failureException: When matcher does not match thing.
         """
         # XXX: Should this take an optional 'message' parameter? Would kind of
-        # make sense.
+        # make sense. The hamcrest one does.
         mismatch = matcher.match(matchee)
         if not mismatch:
             return
@@ -408,8 +408,13 @@
                 full_name = "%s-%d" % (name, suffix)
                 suffix += 1
             self.addDetail(full_name, content)
-        self.fail('Match failed. Matchee: "%s"\nMatcher: %s\nDifference: %s\n'
-            % (matchee, matcher, mismatch.describe()))
+        if verbose:
+            message = (
+                'Match failed. Matchee: "%s"\nMatcher: %s\nDifference: %s\n'
+                % (matchee, matcher, mismatch.describe()))
+        else:
+            message = mismatch.describe()
+        self.fail(message)
 
     def defaultTestResult(self):
         return TestResult()

=== modified file 'testtools/tests/test_testcase.py'
--- testtools/tests/test_testcase.py	2011-07-11 10:30:08 +0000
+++ testtools/tests/test_testcase.py	2011-07-20 17:09:46 +0000
@@ -456,10 +456,29 @@
         self.assertEqual([
             ('match', "foo"),
             ('describe_diff', "foo"),
-            ('__str__',),
             ], calls)
         self.assertFalse(result.wasSuccessful())
 
+    def test_assertThat_output(self):
+        matchee = 'foo'
+        matcher = Equals('bar')
+        expected = matcher.match(matchee).describe()
+        self.assertFails(expected, self.assertThat, matchee, matcher)
+
+    def test_assertThat_verbose_output(self):
+        matchee = 'foo'
+        matcher = Equals('bar')
+        expected = (
+            'Match failed. Matchee: "%s"\n'
+            'Matcher: %s\n'
+            'Difference: %s\n' % (
+                matchee,
+                matcher,
+                matcher.match(matchee).describe(),
+                ))
+        self.assertFails(
+            expected, self.assertThat, matchee, matcher, verbose=True)
+
     def test_assertEqual_nice_formatting(self):
         message = "These things ought not be equal."
         a = ['apple', 'banana', 'cherry']
@@ -467,20 +486,11 @@
              '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),
-             ''])
         expected_error = '\n'.join([
-            'Match failed. Matchee: "%r"' % b,
-            'Matcher: Annotate(%r, Equals(%r))' % (message, a),
-            'Difference: !=:',
+            '!=:',
             'reference = %s' % pformat(a),
             'actual = %s' % pformat(b),
             ': ' + message,
-            ''
             ])
         self.assertFails(expected_error, self.assertEqual, a, b, message)
         self.assertFails(expected_error, self.assertEquals, a, b, message)
@@ -489,12 +499,7 @@
     def test_assertEqual_formatting_no_message(self):
         a = "cat"
         b = "dog"
-        expected_error = '\n'.join([
-            'Match failed. Matchee: "dog"',
-            'Matcher: Equals(\'cat\')',
-            'Difference: \'cat\' != \'dog\'',
-            ''
-            ])
+        expected_error = "'cat' != 'dog'"
         self.assertFails(expected_error, self.assertEqual, a, b)
         self.assertFails(expected_error, self.assertEquals, a, b)
         self.assertFails(expected_error, self.failUnlessEqual, a, b)
@@ -502,24 +507,14 @@
     def test_assertIsNone(self):
         self.assertIsNone(None)
 
-        expected_error = '\n'.join([
-            'Match failed. Matchee: "0"',
-            'Matcher: Is(None)',
-            'Difference: None is not 0',
-            ''
-            ])
+        expected_error = 'None is not 0'
         self.assertFails(expected_error, self.assertIsNone, 0)
 
     def test_assertIsNotNone(self):
         self.assertIsNotNone(0)
         self.assertIsNotNone("0")
 
-        expected_error = '\n'.join([
-            'Match failed. Matchee: "None"',
-            'Matcher: Not(Is(None))',
-            'Difference: None matches Is(None)',
-            ''
-            ])
+        expected_error = 'None matches Is(None)'
         self.assertFails(expected_error, self.assertIsNotNone, None)
 
 


Follow ups