[Python-checkins] cpython (merge 3.5 -> default): Issue #20362: Honour TestCase.longMessage correctly in assertRegex.

robert.collins python-checkins at python.org
Thu Aug 20 01:14:05 CEST 2015


https://hg.python.org/cpython/rev/977e60f597de
changeset:   97461:977e60f597de
parent:      97459:2a0c04260bd4
parent:      97460:738de9a9a3ea
user:        Robert Collins <rbtcollins at hp.com>
date:        Thu Aug 20 11:13:38 2015 +1200
summary:
  Issue #20362: Honour TestCase.longMessage correctly in assertRegex.

Patch from Ilia Kurenkov.

files:
  Lib/unittest/case.py                 |  18 +++++++++------
  Lib/unittest/test/test_assertions.py |  15 ++++++++++++-
  Misc/ACKS                            |   1 +
  Misc/NEWS                            |   3 ++
  4 files changed, 29 insertions(+), 8 deletions(-)


diff --git a/Lib/unittest/case.py b/Lib/unittest/case.py
--- a/Lib/unittest/case.py
+++ b/Lib/unittest/case.py
@@ -1279,8 +1279,10 @@
             assert expected_regex, "expected_regex must not be empty."
             expected_regex = re.compile(expected_regex)
         if not expected_regex.search(text):
-            msg = msg or "Regex didn't match"
-            msg = '%s: %r not found in %r' % (msg, expected_regex.pattern, text)
+            standardMsg = "Regex didn't match: %r not found in %r" % (
+                expected_regex.pattern, text)
+            # _formatMessage ensures the longMessage option is respected
+            msg = self._formatMessage(msg, standardMsg)
             raise self.failureException(msg)
 
     def assertNotRegex(self, text, unexpected_regex, msg=None):
@@ -1289,11 +1291,12 @@
             unexpected_regex = re.compile(unexpected_regex)
         match = unexpected_regex.search(text)
         if match:
-            msg = msg or "Regex matched"
-            msg = '%s: %r matches %r in %r' % (msg,
-                                               text[match.start():match.end()],
-                                               unexpected_regex.pattern,
-                                               text)
+            standardMsg = 'Regex matched: %r matches %r in %r' % (
+                text[match.start() : match.end()],
+                unexpected_regex.pattern,
+                text)
+            # _formatMessage ensures the longMessage option is respected
+            msg = self._formatMessage(msg, standardMsg)
             raise self.failureException(msg)
 
 
@@ -1315,6 +1318,7 @@
     failIf = _deprecate(assertFalse)
     assertRaisesRegexp = _deprecate(assertRaisesRegex)
     assertRegexpMatches = _deprecate(assertRegex)
+    assertNotRegexpMatches = _deprecate(assertNotRegex)
 
 
 
diff --git a/Lib/unittest/test/test_assertions.py b/Lib/unittest/test/test_assertions.py
--- a/Lib/unittest/test/test_assertions.py
+++ b/Lib/unittest/test/test_assertions.py
@@ -133,7 +133,6 @@
         try:
             self.assertNotRegex('Ala ma kota', r'k.t', 'Message')
         except self.failureException as e:
-            self.assertIn("'kot'", e.args[0])
             self.assertIn('Message', e.args[0])
         else:
             self.fail('assertNotRegex should have failed.')
@@ -329,6 +328,20 @@
                              "^unexpectedly identical: None$",
                              "^unexpectedly identical: None : oops$"])
 
+    def testAssertRegex(self):
+        self.assertMessages('assertRegex', ('foo', 'bar'),
+                            ["^Regex didn't match:",
+                             "^oops$",
+                             "^Regex didn't match:",
+                             "^Regex didn't match: (.*) : oops$"])
+
+    def testAssertNotRegex(self):
+        self.assertMessages('assertNotRegex', ('foo', 'foo'),
+                            ["^Regex matched:",
+                             "^oops$",
+                             "^Regex matched:",
+                             "^Regex matched: (.*) : oops$"])
+
 
     def assertMessagesCM(self, methodName, args, func, errors):
         """
diff --git a/Misc/ACKS b/Misc/ACKS
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -786,6 +786,7 @@
 Dave Kuhlman
 Jon Kuhn
 Toshio Kuratomi
+Ilia Kurenkov
 Vladimir Kushnir
 Erno Kuusela
 Ross Lagerwall
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -22,6 +22,9 @@
 Library
 -------
 
+- Issue #20362: Honour TestCase.longMessage correctly in assertRegex.
+  Patch from Ilia Kurenkov.
+
 - Issue #24764: cgi.FieldStorage.read_multi() now ignores the Content-Length
   header in part headers. Patch written by Peter Landry and reviewed by Pierre
   Quentel.

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list