]> granicus.if.org Git - python/commitdiff
Issue #8038: Addition of unittest.TestCase.assertNotRegexpMatches
authorMichael Foord <fuzzyman@voidspace.org.uk>
Fri, 2 Apr 2010 22:55:59 +0000 (22:55 +0000)
committerMichael Foord <fuzzyman@voidspace.org.uk>
Fri, 2 Apr 2010 22:55:59 +0000 (22:55 +0000)
Lib/unittest/case.py
Lib/unittest/test/test_assertions.py
Misc/NEWS

index 98918038187b508bd517478c3ac9458cdd714d5c..cbbe2b5962e4c9cac4853965a97dbc436fba5d70 100644 (file)
@@ -952,6 +952,18 @@ class TestCase(object):
             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.
index 5d77ce8fa459b1d6a5116a2c8ec46308da2b2459..18800596ba472ec863543404d2f7f708f69a7fa7 100644 (file)
@@ -91,6 +91,16 @@ class Test_Assertions(unittest.TestCase):
         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.
index 1c03baf39c50eee21502f06b068de9ebdefbdefc..4003b99a128d9ef4f0c88a6d2ccebc821d444ecd 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -148,6 +148,11 @@ Library
 - Issue #8235: _socket: Add the constant ``SO_SETFIB``.  SO_SETFIB is
   a socket option available on FreeBSD 7.1 and newer.
 
+- Issue #8038: unittest.TestCase.assertNotRegexpMatches
+
+- Addition of -b command line option to unittest for buffering stdout / stderr
+  during test runs.
+
 Extension Modules
 -----------------