Issue #8038: Addition of unittest.TestCase.assertNotRegexpMatches
diff --git a/Lib/unittest/case.py b/Lib/unittest/case.py
index 9891803..cbbe2b5 100644
--- a/Lib/unittest/case.py
+++ b/Lib/unittest/case.py
@@ -952,6 +952,18 @@
msg = '%s: %r not found in %r' % (msg, expected_regexp.pattern, text)
raise self.failureException(msg)
+ def assertNotRegexpMatches(self, text, unexpected_regexp, msg=None):
+ if isinstance(unexpected_regexp, basestring):
+ 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)
+ raise self.failureException(msg)
+
class FunctionTestCase(TestCase):
"""A test case that wraps a test function.
diff --git a/Lib/unittest/test/test_assertions.py b/Lib/unittest/test/test_assertions.py
index 5d77ce8..1880059 100644
--- a/Lib/unittest/test/test_assertions.py
+++ b/Lib/unittest/test/test_assertions.py
@@ -91,6 +91,16 @@
else:
self.fail("assertRaises() didn't let exception pass through")
+ def testAssertNotRegexpMatches(self):
+ self.assertNotRegexpMatches('Ala ma kota', r'r+')
+ 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.')
+
class TestLongMessage(unittest.TestCase):
"""Test that the individual asserts honour longMessage.