diff --git a/documentation/index.rst b/documentation/index.rst index e5e299428..e2e82e3df 100644 --- a/documentation/index.rst +++ b/documentation/index.rst @@ -510,6 +510,11 @@ Note these functions are only available on Python 2.7 or later. Alias for :meth:`~py3:unittest.TestCase.assertRegex` on Python 3 and :meth:`~py2:unittest.TestCase.assertRegexpMatches` on Python 2. +.. function:: assertNotRegex() + + Alias for :meth:`~py3:unittest.TestCase.assertNotRegex` on Python 3 and + :meth:`~py2:unittest.TestCase.assertNotRegexpMatches` on Python 2. + Renamed modules and attributes compatibility >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> diff --git a/six.py b/six.py index 1296655e2..30de42ddc 100644 --- a/six.py +++ b/six.py @@ -644,9 +644,11 @@ def u(s): if sys.version_info[1] <= 1: _assertRaisesRegex = "assertRaisesRegexp" _assertRegex = "assertRegexpMatches" + _assertNotRegex = "assertNotRegexpMatches" else: _assertRaisesRegex = "assertRaisesRegex" _assertRegex = "assertRegex" + _assertNotRegex = "assertNotRegex" else: def b(s): return s @@ -668,6 +670,7 @@ def indexbytes(buf, i): _assertCountEqual = "assertItemsEqual" _assertRaisesRegex = "assertRaisesRegexp" _assertRegex = "assertRegexpMatches" + _assertNotRegex = "assertNotRegexpMatches" _add_doc(b, """Byte literal""") _add_doc(u, """Text literal""") @@ -684,6 +687,10 @@ def assertRegex(self, *args, **kwargs): return getattr(self, _assertRegex)(*args, **kwargs) +def assertNotRegex(self, *args, **kwargs): + return getattr(self, _assertNotRegex)(*args, **kwargs) + + if PY3: exec_ = getattr(moves.builtins, "exec") diff --git a/test_six.py b/test_six.py index 2cd211275..7b8b03b5e 100644 --- a/test_six.py +++ b/test_six.py @@ -938,6 +938,17 @@ def test(self): TestAssertRegex('test').test() +def test_assertNotRegex(): + class TestAssertNotRegex(unittest.TestCase): + def test(self): + with self.assertRaises(AssertionError): + six.assertNotRegex(self, 'test', r'^t') + + six.assertNotRegex(self, 'test', r'^a') + + TestAssertNotRegex('test').test() + + def test_assertRaisesRegex(): class TestAssertRaisesRegex(unittest.TestCase): def test(self):