summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Kluyver <takowl@gmail.com>2014-06-15 19:26:42 -0700
committerThomas Kluyver <takowl@gmail.com>2014-06-15 19:26:42 -0700
commit0ffc3142adbeb189e2f2ffc2dac45501fc3c0e57 (patch)
tree9e04e859f7b5854c7c3139c7db685a2ff5622213
parentd0fd37033e1a48bff38399394604a653cd0f3baa (diff)
downloadpexpect-0ffc3142adbeb189e2f2ffc2dac45501fc3c0e57.tar.gz
Implement assertRaises and assertRaisesRegexp context managers for Python 2.6
-rw-r--r--tests/PexpectTestCase.py24
1 files changed, 24 insertions, 0 deletions
diff --git a/tests/PexpectTestCase.py b/tests/PexpectTestCase.py
index 7ec7e53..7a9574e 100644
--- a/tests/PexpectTestCase.py
+++ b/tests/PexpectTestCase.py
@@ -20,6 +20,7 @@ PEXPECT LICENSE
'''
from __future__ import print_function
+import contextlib
import unittest
import sys
import os
@@ -40,3 +41,26 @@ class PexpectTestCase(unittest.TestCase):
def tearDown(self):
os.chdir (self.original_path)
+ if sys.version_info < (2,7):
+ # We want to use these methods, which are new/improved in 2.7, but
+ # we are still supporting 2.6 for the moment. This section can be
+ # removed when we drop Python 2.6 support.
+ @contextlib.contextmanager
+ def assertRaises(self, excClass):
+ try:
+ yield
+ except Exception as e:
+ assert isinstance(e, excClass)
+ else:
+ raise AssertionError("%s was not raised" % excClass)
+
+ @contextlib.contextmanager
+ def assertRaisesRegexp(self, excClass, pattern):
+ import re
+ try:
+ yield
+ except Exception as e:
+ assert isinstance(e, excClass)
+ assert re.match(pattern, str(e))
+ else:
+ raise AssertionError("%s was not raised" % excClass)