summaryrefslogtreecommitdiff
path: root/test/backunittest.py
diff options
context:
space:
mode:
authorNed Batchelder <ned@nedbatchelder.com>2009-11-26 09:35:48 -0500
committerNed Batchelder <ned@nedbatchelder.com>2009-11-26 09:35:48 -0500
commit27864c812b0ae954e9adabe81ed19cbf8f407c2b (patch)
treea4773d309299c14c52905e8bbcffbe77464f2e1f /test/backunittest.py
parentac42db5a794b3ddfdc5227b4cc9faccc8df4aaa1 (diff)
downloadpython-coveragepy-git-27864c812b0ae954e9adabe81ed19cbf8f407c2b.tar.gz
Rename our future-happy unittest methods to match their future versions, and let the future versions win if they exist.
Diffstat (limited to 'test/backunittest.py')
-rw-r--r--test/backunittest.py120
1 files changed, 67 insertions, 53 deletions
diff --git a/test/backunittest.py b/test/backunittest.py
index 35003ee5..2088f2f7 100644
--- a/test/backunittest.py
+++ b/test/backunittest.py
@@ -4,68 +4,82 @@ import difflib, re, sys, unittest
from coverage.backward import set # pylint: disable-msg=W0622
+
+def _need(method):
+ """Do we need to define our own `method` method?"""
+ return not hasattr(unittest.TestCase, method)
+
+
class TestCase(unittest.TestCase):
"""Just like unittest.TestCase, but with assert methods added.
- Designed to be compatible with 3.1 unittest.
+ Designed to be compatible with 3.1 unittest. Methods are only defined if
+ the builtin `unittest` doesn't have them.
"""
- def assert_raises_msg(self, excClass, msg, callableObj, *args, **kwargs):
- """ Just like unittest.TestCase.assertRaises,
- but checks that the message is right too.
- """
- try:
- callableObj(*args, **kwargs)
- except excClass:
- _, exc, _ = sys.exc_info()
- excMsg = str(exc)
- if not msg:
- # No message provided: it passes.
- return #pragma: no cover
- elif excMsg == msg:
- # Message provided, and we got the right message: it passes.
- return
+ if _need('assertFalse'):
+ def assertFalse(self, exp):
+ """Assert that `exp` is false."""
+ if exp:
+ self.fail()
+
+ if _need('assertRaisesRegexp'):
+ def assertRaisesRegexp(self, excClass, regexp, callobj, *args, **kw):
+ """ Just like unittest.TestCase.assertRaises,
+ but checks that the message is right too.
+ """
+ try:
+ callobj(*args, **kw)
+ except excClass:
+ _, exc, _ = sys.exc_info()
+ excMsg = str(exc)
+ if re.search(regexp, excMsg):
+ # Message provided, and we got the right one: it passes.
+ return
+ else: #pragma: no cover
+ # Message provided, and it didn't match: fail!
+ raise self.failureException(
+ "Right exception, wrong message: "
+ "'%s' doesn't match '%s'" % (excMsg, regexp)
+ )
+ # No need to catch other exceptions: They'll fail the test all by
+ # themselves!
else: #pragma: no cover
- # Message provided, and it didn't match: fail!
+ if hasattr(excClass, '__name__'):
+ excName = excClass.__name__
+ else:
+ excName = str(excClass)
raise self.failureException(
- "Right exception, wrong message: got '%s' expected '%s'" %
- (excMsg, msg)
+ "Expected to raise %s, didn't get an exception at all" %
+ excName
)
- # No need to catch other exceptions: They'll fail the test all by
- # themselves!
- else: #pragma: no cover
- if hasattr(excClass,'__name__'):
- excName = excClass.__name__
- else:
- excName = str(excClass)
- raise self.failureException(
- "Expected to raise %s, didn't get an exception at all" %
- excName
- )
-
- def assert_equal_sets(self, s1, s2):
- """Assert that the two arguments are equal as sets."""
- self.assertEqual(set(s1), set(s2))
- def assert_matches(self, s, regex):
- """Assert that `s` matches `regex`."""
- m = re.search(regex, s)
- if not m:
- raise self.failureException("%r doesn't match %r" % (s, regex))
+ if _need('assertSameElements'):
+ def assertSameElements(self, s1, s2):
+ """Assert that the two arguments are equal as sets."""
+ self.assertEqual(set(s1), set(s2))
- def assert_multiline_equal(self, first, second):
- """Assert that two multi-line strings are equal.
-
- If they aren't, show a nice diff.
-
- """
- # Adapted from Py3.1 unittest.
- self.assert_(isinstance(first, str), (
- 'First argument is not a string'))
- self.assert_(isinstance(second, str), (
- 'Second argument is not a string'))
+ if _need('assertRegexpMatches'):
+ def assertRegexpMatches(self, s, regex):
+ """Assert that `s` matches `regex`."""
+ m = re.search(regex, s)
+ if not m:
+ raise self.failureException("%r doesn't match %r" % (s, regex))
- if first != second:
- msg = ''.join(difflib.ndiff(first.splitlines(True),
+ if _need('assertMultiLineEqual'):
+ def assertMultiLineEqual(self, first, second):
+ """Assert that two multi-line strings are equal.
+
+ If they aren't, show a nice diff.
+
+ """
+ # Adapted from Py3.1 unittest.
+ self.assert_(isinstance(first, str), (
+ 'First argument is not a string'))
+ self.assert_(isinstance(second, str), (
+ 'Second argument is not a string'))
+
+ if first != second:
+ msg = ''.join(difflib.ndiff(first.splitlines(True),
second.splitlines(True)))
- self.fail("Multi-line strings are unequal:\n" + msg)
+ self.fail("Multi-line strings are unequal:\n" + msg)