Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(123029)

Side by Side Diff: Lib/unittest/test/test_case.py

Issue 15836: unittest assertRaises should verify excClass is actually a BaseException class
Patch Set: Created 4 years, 6 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
OLDNEW
1 import contextlib 1 import contextlib
2 import difflib 2 import difflib
3 import pprint 3 import pprint
4 import pickle 4 import pickle
5 import re 5 import re
6 import sys 6 import sys
7 import logging 7 import logging
8 import warnings 8 import warnings
9 import weakref 9 import weakref
10 import inspect 10 import inspect
(...skipping 1166 matching lines...) Expand 10 before | Expand all | Expand 10 after
1177 with self.assertRaises(ExceptionMock, msg='foobar'): 1177 with self.assertRaises(ExceptionMock, msg='foobar'):
1178 pass 1178 pass
1179 # Invalid keyword argument 1179 # Invalid keyword argument
1180 with self.assertWarnsRegex(DeprecationWarning, 'foobar'), \ 1180 with self.assertWarnsRegex(DeprecationWarning, 'foobar'), \
1181 self.assertRaises(AssertionError): 1181 self.assertRaises(AssertionError):
1182 with self.assertRaises(ExceptionMock, foobar=42): 1182 with self.assertRaises(ExceptionMock, foobar=42):
1183 pass 1183 pass
1184 # Failure when another exception is raised 1184 # Failure when another exception is raised
1185 with self.assertRaises(ExceptionMock): 1185 with self.assertRaises(ExceptionMock):
1186 self.assertRaises(ValueError, Stub) 1186 self.assertRaises(ValueError, Stub)
1187
1188 def testAssertRaisesNoExceptionType(self):
1189 with self.assertRaises(TypeError):
1190 self.assertRaises()
1191 with self.assertRaises(TypeError):
1192 self.assertRaises(1)
1193 with self.assertRaises(TypeError):
1194 self.assertRaises(object)
1195 with self.assertRaises(TypeError):
1196 self.assertRaises((ValueError, 1))
1197 with self.assertRaises(TypeError):
1198 self.assertRaises((ValueError, object))
1187 1199
1188 def testAssertRaisesRegex(self): 1200 def testAssertRaisesRegex(self):
1189 class ExceptionMock(Exception): 1201 class ExceptionMock(Exception):
1190 pass 1202 pass
1191 1203
1192 def Stub(): 1204 def Stub():
1193 raise ExceptionMock('We expect') 1205 raise ExceptionMock('We expect')
1194 1206
1195 self.assertRaisesRegex(ExceptionMock, re.compile('expect$'), Stub) 1207 self.assertRaisesRegex(ExceptionMock, re.compile('expect$'), Stub)
1196 self.assertRaisesRegex(ExceptionMock, 'expect$', Stub) 1208 self.assertRaisesRegex(ExceptionMock, 'expect$', Stub)
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
1250 def Stub(foo): 1262 def Stub(foo):
1251 raise ExceptionMock(foo) 1263 raise ExceptionMock(foo)
1252 v = "particular value" 1264 v = "particular value"
1253 1265
1254 ctx = self.assertRaises(ExceptionMock) 1266 ctx = self.assertRaises(ExceptionMock)
1255 with ctx: 1267 with ctx:
1256 Stub(v) 1268 Stub(v)
1257 e = ctx.exception 1269 e = ctx.exception
1258 self.assertIsInstance(e, ExceptionMock) 1270 self.assertIsInstance(e, ExceptionMock)
1259 self.assertEqual(e.args[0], v) 1271 self.assertEqual(e.args[0], v)
1272
1273 def testAssertRaisesRegexNoExceptionType(self):
1274 with self.assertRaises(TypeError):
1275 self.assertRaisesRegex()
1276 with self.assertRaises(TypeError):
1277 self.assertRaisesRegex(ValueError)
1278 with self.assertRaises(TypeError):
1279 self.assertRaisesRegex(1, 'expect')
1280 with self.assertRaises(TypeError):
1281 self.assertRaisesRegex(object, 'expect')
1282 with self.assertRaises(TypeError):
1283 self.assertRaisesRegex((ValueError, 1), 'expect')
1284 with self.assertRaises(TypeError):
1285 self.assertRaisesRegex((ValueError, object), 'expect')
1260 1286
1261 def testAssertWarnsCallable(self): 1287 def testAssertWarnsCallable(self):
1262 def _runtime_warn(): 1288 def _runtime_warn():
1263 warnings.warn("foo", RuntimeWarning) 1289 warnings.warn("foo", RuntimeWarning)
1264 # Success when the right warning is triggered, even several times 1290 # Success when the right warning is triggered, even several times
1265 self.assertWarns(RuntimeWarning, _runtime_warn) 1291 self.assertWarns(RuntimeWarning, _runtime_warn)
1266 self.assertWarns(RuntimeWarning, _runtime_warn) 1292 self.assertWarns(RuntimeWarning, _runtime_warn)
1267 # A tuple of warning classes is accepted 1293 # A tuple of warning classes is accepted
1268 self.assertWarns((DeprecationWarning, RuntimeWarning), _runtime_warn) 1294 self.assertWarns((DeprecationWarning, RuntimeWarning), _runtime_warn)
1269 # *args and **kwargs also work 1295 # *args and **kwargs also work
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
1328 warnings.simplefilter("default", RuntimeWarning) 1354 warnings.simplefilter("default", RuntimeWarning)
1329 with self.assertRaises(self.failureException): 1355 with self.assertRaises(self.failureException):
1330 with self.assertWarns(DeprecationWarning): 1356 with self.assertWarns(DeprecationWarning):
1331 _runtime_warn() 1357 _runtime_warn()
1332 # Filters for other warnings are not modified 1358 # Filters for other warnings are not modified
1333 with warnings.catch_warnings(): 1359 with warnings.catch_warnings():
1334 warnings.simplefilter("error", RuntimeWarning) 1360 warnings.simplefilter("error", RuntimeWarning)
1335 with self.assertRaises(RuntimeWarning): 1361 with self.assertRaises(RuntimeWarning):
1336 with self.assertWarns(DeprecationWarning): 1362 with self.assertWarns(DeprecationWarning):
1337 _runtime_warn() 1363 _runtime_warn()
1364
1365 def testAssertWarnsNoExceptionType(self):
1366 with self.assertRaises(TypeError):
1367 self.assertWarns()
1368 with self.assertRaises(TypeError):
1369 self.assertWarns(1)
1370 with self.assertRaises(TypeError):
1371 self.assertWarns(object)
1372 with self.assertRaises(TypeError):
1373 self.assertWarns((UserWarning, 1))
1374 with self.assertRaises(TypeError):
1375 self.assertWarns((UserWarning, object))
1376 with self.assertRaises(TypeError):
1377 self.assertWarns((UserWarning, Exception))
1338 1378
1339 def testAssertWarnsRegexCallable(self): 1379 def testAssertWarnsRegexCallable(self):
1340 def _runtime_warn(msg): 1380 def _runtime_warn(msg):
1341 warnings.warn(msg, RuntimeWarning) 1381 warnings.warn(msg, RuntimeWarning)
1342 self.assertWarnsRegex(RuntimeWarning, "o+", 1382 self.assertWarnsRegex(RuntimeWarning, "o+",
1343 _runtime_warn, "foox") 1383 _runtime_warn, "foox")
1344 # Failure when no warning is triggered 1384 # Failure when no warning is triggered
1345 with self.assertRaises(self.failureException): 1385 with self.assertRaises(self.failureException):
1346 self.assertWarnsRegex(RuntimeWarning, "o+", 1386 self.assertWarnsRegex(RuntimeWarning, "o+",
1347 lambda: 0) 1387 lambda: 0)
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
1406 _runtime_warn("barz") 1446 _runtime_warn("barz")
1407 # A little trickier: we ask RuntimeWarnings to be raised, and then 1447 # A little trickier: we ask RuntimeWarnings to be raised, and then
1408 # check for some of them. It is implementation-defined whether 1448 # check for some of them. It is implementation-defined whether
1409 # non-matching RuntimeWarnings are simply re-raised, or produce a 1449 # non-matching RuntimeWarnings are simply re-raised, or produce a
1410 # failureException. 1450 # failureException.
1411 with warnings.catch_warnings(): 1451 with warnings.catch_warnings():
1412 warnings.simplefilter("error", RuntimeWarning) 1452 warnings.simplefilter("error", RuntimeWarning)
1413 with self.assertRaises((RuntimeWarning, self.failureException)): 1453 with self.assertRaises((RuntimeWarning, self.failureException)):
1414 with self.assertWarnsRegex(RuntimeWarning, "o+"): 1454 with self.assertWarnsRegex(RuntimeWarning, "o+"):
1415 _runtime_warn("barz") 1455 _runtime_warn("barz")
1456
1457 def testAssertWarnsRegexNoExceptionType(self):
1458 with self.assertRaises(TypeError):
1459 self.assertWarnsRegex()
1460 with self.assertRaises(TypeError):
1461 self.assertWarnsRegex(UserWarning)
1462 with self.assertRaises(TypeError):
1463 self.assertWarnsRegex(1, 'expect')
1464 with self.assertRaises(TypeError):
1465 self.assertWarnsRegex(object, 'expect')
1466 with self.assertRaises(TypeError):
1467 self.assertWarnsRegex((UserWarning, 1), 'expect')
1468 with self.assertRaises(TypeError):
1469 self.assertWarnsRegex((UserWarning, object), 'expect')
1470 with self.assertRaises(TypeError):
1471 self.assertWarnsRegex((UserWarning, Exception), 'expect')
1416 1472
1417 @contextlib.contextmanager 1473 @contextlib.contextmanager
1418 def assertNoStderr(self): 1474 def assertNoStderr(self):
1419 with captured_stderr() as buf: 1475 with captured_stderr() as buf:
1420 yield 1476 yield
1421 self.assertEqual(buf.getvalue(), "") 1477 self.assertEqual(buf.getvalue(), "")
1422 1478
1423 def assertLogRecords(self, records, matches): 1479 def assertLogRecords(self, records, matches):
1424 self.assertEqual(len(records), len(matches)) 1480 self.assertEqual(len(records), len(matches))
1425 for rec, match in zip(records, matches): 1481 for rec, match in zip(records, matches):
(...skipping 253 matching lines...) Expand 10 before | Expand all | Expand 10 after
1679 raise MyException() 1735 raise MyException()
1680 1736
1681 for method_name in ('test1', 'test2'): 1737 for method_name in ('test1', 'test2'):
1682 testcase = TestCase(method_name) 1738 testcase = TestCase(method_name)
1683 testcase.run() 1739 testcase.run()
1684 self.assertEqual(MyException.ninstance, 0) 1740 self.assertEqual(MyException.ninstance, 0)
1685 1741
1686 1742
1687 if __name__ == "__main__": 1743 if __name__ == "__main__":
1688 unittest.main() 1744 unittest.main()
OLDNEW
« Lib/unittest/case.py ('K') | « Lib/unittest/case.py ('k') | Misc/ACKS » ('j') | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+