diff -r dc65187be892 Lib/unittest/case.py --- a/Lib/unittest/case.py Mon Apr 13 11:07:35 2015 -0500 +++ b/Lib/unittest/case.py Mon Apr 13 11:13:00 2015 -0400 @@ -997,8 +997,10 @@ if isinstance(expected_regexp, basestring): expected_regexp = re.compile(expected_regexp) if not expected_regexp.search(text): - msg = msg or "Regexp didn't match" - msg = '%s: %r not found in %r' % (msg, expected_regexp.pattern, text) + standardMsg = "Regex didn't match: %r not found in %r" % ( + expected_regexp.pattern, text) + # _formatMessage ensures the longMessage option is respected + msg = self._formatMessage(msg, standardMsg) raise self.failureException(msg) def assertNotRegexpMatches(self, text, unexpected_regexp, msg=None): @@ -1007,11 +1009,12 @@ unexpected_regexp = re.compile(unexpected_regexp) match = unexpected_regexp.search(text) if match: - msg = msg or "Regexp matched" - msg = '%s: %r matches %r in %r' % (msg, - text[match.start():match.end()], - unexpected_regexp.pattern, - text) + standardMsg = 'Regex matched: %r matches %r in %r' % ( + text[match.start() : match.end()], + unexpected_regexp.pattern, + text) + # _formatMessage ensures the longMessage option is respected + msg = self._formatMessage(msg, standardMsg) raise self.failureException(msg) diff -r dc65187be892 Lib/unittest/test/test_assertions.py --- a/Lib/unittest/test/test_assertions.py Mon Apr 13 11:07:35 2015 -0500 +++ b/Lib/unittest/test/test_assertions.py Mon Apr 13 11:13:00 2015 -0400 @@ -100,7 +100,6 @@ try: self.assertNotRegexpMatches('Ala ma kota', r'k.t', 'Message') except self.failureException, e: - self.assertIn("'kot'", e.args[0]) self.assertIn('Message', e.args[0]) else: self.fail('assertNotRegexpMatches should have failed.') @@ -285,6 +284,20 @@ "^unexpectedly identical: None$", "^unexpectedly identical: None : oops$"]) + def testAssertRegex(self): + self.assertMessages('assertRegexpMatches', ('foo', 'bar'), + ["^Regex didn't match:", + "^oops$", + "^Regex didn't match:", + "^Regex didn't match: (.*) : oops$"]) + + def testAssertNotRegex(self): + self.assertMessages('assertNotRegexpMatches', ('foo', 'foo'), + ["^Regex matched:", + "^oops$", + "^Regex matched:", + "^Regex matched: (.*) : oops$"]) + if __name__ == '__main__': unittest.main()