Skip to content

Commit

Permalink
Add unittest assert(Not)Regex for Python 2 users
Browse files Browse the repository at this point in the history
  • Loading branch information
benmwebb committed May 2, 2020
1 parent 5a5b791 commit 7a90377
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
13 changes: 9 additions & 4 deletions python/saliweb/test/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@


def _add_unittest_methods():
"""Add more modern unittest methods to Python 2.6"""
"""Add more modern unittest methods to Python 2.6, 3,0, or 3.1"""
def assertRegexpMatches(self, text, regexp, msg=None):
if isinstance(regexp, basestring):
regexp = re.compile(regexp)
Expand Down Expand Up @@ -67,10 +67,15 @@ def assertAlmostEqual(self, first, second, places=None, msg=None,
unittest.TestCase.assertAlmostEqual = assertAlmostEqual


# If we're using Python 2.6, add in more modern unittest convenience
# methods
if not hasattr(unittest.TestCase, 'assertIn'):
# If we're using Python 2.6, 3.0, or 3.1, add in more modern unittest
# convenience methods
if not hasattr(unittest.TestCase, 'assertIsInstance'):
_add_unittest_methods()
# Provide assert(Not)Regex for Python 2 users (assertRegexMatches is
# deprecated in Python 3)
if not hasattr(unittest.TestCase, 'assertRegex'):
assertRegex = unittest.TestCase.assertRegexpMatches
assertNotRegex = unittest.TestCase.assertNotRegexpMatches


class RunInDir(object):
Expand Down
5 changes: 5 additions & 0 deletions test/build/testutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ def assertAlmostEqual(self, first, second, places=None, msg=None,
unittest.TestCase.assertIsNone = assertIsNone
unittest.TestCase.assertIsNotNone = assertIsNotNone
unittest.TestCase.assertAlmostEqual = assertAlmostEqual
# Provide assert(Not)Regex for Python 2 users (assertRegexMatches is
# deprecated in Python 3)
if not hasattr(unittest.TestCase, 'assertRegex'):
assertRegex = unittest.TestCase.assertRegexpMatches
assertNotRegex = unittest.TestCase.assertNotRegexpMatches


def run_catch_warnings(method, *args, **keys):
Expand Down

0 comments on commit 7a90377

Please sign in to comment.