summaryrefslogtreecommitdiffstats
path: root/Lib/unittest/test/test_case.py
diff options
context:
space:
mode:
Diffstat (limited to 'Lib/unittest/test/test_case.py')
-rw-r--r--Lib/unittest/test/test_case.py113
1 files changed, 107 insertions, 6 deletions
diff --git a/Lib/unittest/test/test_case.py b/Lib/unittest/test/test_case.py
index 321d67a..ada733b 100644
--- a/Lib/unittest/test/test_case.py
+++ b/Lib/unittest/test/test_case.py
@@ -1103,12 +1103,9 @@ test case
except self.failureException as e:
# need to remove the first line of the error message
error = str(e).split('\n', 1)[1]
+ self.assertEqual(sample_text_error, error)
- # no fair testing ourself with ourself, and assertEqual is used for strings
- # so can't use assertEqual either. Just use assertTrue.
- self.assertTrue(sample_text_error == error)
-
- def testAsertEqualSingleLine(self):
+ def testAssertEqualSingleLine(self):
sample_text = "laden swallows fly slowly"
revised_sample_text = "unladen swallows fly quickly"
sample_text_error = """\
@@ -1120,8 +1117,9 @@ test case
try:
self.assertEqual(sample_text, revised_sample_text)
except self.failureException as e:
+ # need to remove the first line of the error message
error = str(e).split('\n', 1)[1]
- self.assertTrue(sample_text_error == error)
+ self.assertEqual(sample_text_error, error)
def testAssertIsNone(self):
self.assertIsNone(None)
@@ -1147,6 +1145,9 @@ test case
# Failure when no exception is raised
with self.assertRaises(self.failureException):
self.assertRaises(ExceptionMock, lambda: 0)
+ # Failure when the function is None
+ with self.assertWarns(DeprecationWarning):
+ self.assertRaises(ExceptionMock, None)
# Failure when another exception is raised
with self.assertRaises(ExceptionMock):
self.assertRaises(ValueError, Stub)
@@ -1171,10 +1172,31 @@ test case
with self.assertRaises(self.failureException):
with self.assertRaises(ExceptionMock):
pass
+ # Custom message
+ with self.assertRaisesRegex(self.failureException, 'foobar'):
+ with self.assertRaises(ExceptionMock, msg='foobar'):
+ pass
+ # Invalid keyword argument
+ with self.assertWarnsRegex(DeprecationWarning, 'foobar'), \
+ self.assertRaises(AssertionError):
+ with self.assertRaises(ExceptionMock, foobar=42):
+ pass
# Failure when another exception is raised
with self.assertRaises(ExceptionMock):
self.assertRaises(ValueError, Stub)
+ def testAssertRaisesNoExceptionType(self):
+ with self.assertRaises(TypeError):
+ self.assertRaises()
+ with self.assertRaises(TypeError):
+ self.assertRaises(1)
+ with self.assertRaises(TypeError):
+ self.assertRaises(object)
+ with self.assertRaises(TypeError):
+ self.assertRaises((ValueError, 1))
+ with self.assertRaises(TypeError):
+ self.assertRaises((ValueError, object))
+
def testAssertRaisesRegex(self):
class ExceptionMock(Exception):
pass
@@ -1184,6 +1206,8 @@ test case
self.assertRaisesRegex(ExceptionMock, re.compile('expect$'), Stub)
self.assertRaisesRegex(ExceptionMock, 'expect$', Stub)
+ with self.assertWarns(DeprecationWarning):
+ self.assertRaisesRegex(ExceptionMock, 'expect$', None)
def testAssertNotRaisesRegex(self):
self.assertRaisesRegex(
@@ -1194,6 +1218,15 @@ test case
self.failureException, '^Exception not raised by <lambda>$',
self.assertRaisesRegex, Exception, 'x',
lambda: None)
+ # Custom message
+ with self.assertRaisesRegex(self.failureException, 'foobar'):
+ with self.assertRaisesRegex(Exception, 'expect', msg='foobar'):
+ pass
+ # Invalid keyword argument
+ with self.assertWarnsRegex(DeprecationWarning, 'foobar'), \
+ self.assertRaises(AssertionError):
+ with self.assertRaisesRegex(Exception, 'expect', foobar=42):
+ pass
def testAssertRaisesRegexInvalidRegex(self):
# Issue 20145.
@@ -1237,6 +1270,20 @@ test case
self.assertIsInstance(e, ExceptionMock)
self.assertEqual(e.args[0], v)
+ def testAssertRaisesRegexNoExceptionType(self):
+ with self.assertRaises(TypeError):
+ self.assertRaisesRegex()
+ with self.assertRaises(TypeError):
+ self.assertRaisesRegex(ValueError)
+ with self.assertRaises(TypeError):
+ self.assertRaisesRegex(1, 'expect')
+ with self.assertRaises(TypeError):
+ self.assertRaisesRegex(object, 'expect')
+ with self.assertRaises(TypeError):
+ self.assertRaisesRegex((ValueError, 1), 'expect')
+ with self.assertRaises(TypeError):
+ self.assertRaisesRegex((ValueError, object), 'expect')
+
def testAssertWarnsCallable(self):
def _runtime_warn():
warnings.warn("foo", RuntimeWarning)
@@ -1251,6 +1298,9 @@ test case
# Failure when no warning is triggered
with self.assertRaises(self.failureException):
self.assertWarns(RuntimeWarning, lambda: 0)
+ # Failure when the function is None
+ with self.assertWarns(DeprecationWarning):
+ self.assertWarns(RuntimeWarning, None)
# Failure when another warning is triggered
with warnings.catch_warnings():
# Force default filter (in case tests are run with -We)
@@ -1289,6 +1339,15 @@ test case
with self.assertRaises(self.failureException):
with self.assertWarns(RuntimeWarning):
pass
+ # Custom message
+ with self.assertRaisesRegex(self.failureException, 'foobar'):
+ with self.assertWarns(RuntimeWarning, msg='foobar'):
+ pass
+ # Invalid keyword argument
+ with self.assertWarnsRegex(DeprecationWarning, 'foobar'), \
+ self.assertRaises(AssertionError):
+ with self.assertWarns(RuntimeWarning, foobar=42):
+ pass
# Failure when another warning is triggered
with warnings.catch_warnings():
# Force default filter (in case tests are run with -We)
@@ -1303,6 +1362,20 @@ test case
with self.assertWarns(DeprecationWarning):
_runtime_warn()
+ def testAssertWarnsNoExceptionType(self):
+ with self.assertRaises(TypeError):
+ self.assertWarns()
+ with self.assertRaises(TypeError):
+ self.assertWarns(1)
+ with self.assertRaises(TypeError):
+ self.assertWarns(object)
+ with self.assertRaises(TypeError):
+ self.assertWarns((UserWarning, 1))
+ with self.assertRaises(TypeError):
+ self.assertWarns((UserWarning, object))
+ with self.assertRaises(TypeError):
+ self.assertWarns((UserWarning, Exception))
+
def testAssertWarnsRegexCallable(self):
def _runtime_warn(msg):
warnings.warn(msg, RuntimeWarning)
@@ -1312,6 +1385,9 @@ test case
with self.assertRaises(self.failureException):
self.assertWarnsRegex(RuntimeWarning, "o+",
lambda: 0)
+ # Failure when the function is None
+ with self.assertWarns(DeprecationWarning):
+ self.assertWarnsRegex(RuntimeWarning, "o+", None)
# Failure when another warning is triggered
with warnings.catch_warnings():
# Force default filter (in case tests are run with -We)
@@ -1348,6 +1424,15 @@ test case
with self.assertRaises(self.failureException):
with self.assertWarnsRegex(RuntimeWarning, "o+"):
pass
+ # Custom message
+ with self.assertRaisesRegex(self.failureException, 'foobar'):
+ with self.assertWarnsRegex(RuntimeWarning, 'o+', msg='foobar'):
+ pass
+ # Invalid keyword argument
+ with self.assertWarnsRegex(DeprecationWarning, 'foobar'), \
+ self.assertRaises(AssertionError):
+ with self.assertWarnsRegex(RuntimeWarning, 'o+', foobar=42):
+ pass
# Failure when another warning is triggered
with warnings.catch_warnings():
# Force default filter (in case tests are run with -We)
@@ -1369,6 +1454,22 @@ test case
with self.assertWarnsRegex(RuntimeWarning, "o+"):
_runtime_warn("barz")
+ def testAssertWarnsRegexNoExceptionType(self):
+ with self.assertRaises(TypeError):
+ self.assertWarnsRegex()
+ with self.assertRaises(TypeError):
+ self.assertWarnsRegex(UserWarning)
+ with self.assertRaises(TypeError):
+ self.assertWarnsRegex(1, 'expect')
+ with self.assertRaises(TypeError):
+ self.assertWarnsRegex(object, 'expect')
+ with self.assertRaises(TypeError):
+ self.assertWarnsRegex((UserWarning, 1), 'expect')
+ with self.assertRaises(TypeError):
+ self.assertWarnsRegex((UserWarning, object), 'expect')
+ with self.assertRaises(TypeError):
+ self.assertWarnsRegex((UserWarning, Exception), 'expect')
+
@contextlib.contextmanager
def assertNoStderr(self):
with captured_stderr() as buf: