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

Side by Side Diff: Lib/unittest/case.py

Issue 15836: unittest assertRaises should verify excClass is actually a BaseException class
Patch Set: Created 7 years 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 """Test case implementation""" 1 """Test case implementation"""
2 2
3 import sys 3 import sys
4 import functools 4 import functools
5 import difflib 5 import difflib
6 import pprint 6 import pprint
7 import re 7 import re
8 import warnings 8 import warnings
9 import collections 9 import collections
10 10
(...skipping 549 matching lines...) Expand 10 before | Expand all | Expand 10 after
560 560
561 The context manager keeps a reference to the exception as 561 The context manager keeps a reference to the exception as
562 the 'exception' attribute. This allows you to inspect the 562 the 'exception' attribute. This allows you to inspect the
563 exception after the assertion:: 563 exception after the assertion::
564 564
565 with self.assertRaises(SomeException) as cm: 565 with self.assertRaises(SomeException) as cm:
566 do_something() 566 do_something()
567 the_exception = cm.exception 567 the_exception = cm.exception
568 self.assertEqual(the_exception.error_code, 3) 568 self.assertEqual(the_exception.error_code, 3)
569 """ 569 """
570 def check_is_exception(e):
r.david.murray 2012/09/01 03:00:27 I prefer the style where a helper function like th
illicitonion 2012/09/01 03:35:38 Pushed up to private module level helper
571 if not type(e) is type or not issubclass(e, BaseException):
572 raise TypeError('assertRaises arg 1 must be an exception type '
573 'or tuple of exception types')
574
575 if type(excClass) is tuple:
r.david.murray 2012/09/01 03:00:27 These checks should be done inside the _AssertRais
illicitonion 2012/09/01 03:35:38 Done.
576 for e in excClass:
577 check_is_exception(e)
578 else:
579 check_is_exception(excClass)
580
570 context = _AssertRaisesContext(excClass, self, callableObj) 581 context = _AssertRaisesContext(excClass, self, callableObj)
571 return context.handle('assertRaises', callableObj, args, kwargs) 582 return context.handle('assertRaises', callableObj, args, kwargs)
572 583
573 def assertWarns(self, expected_warning, callable_obj=None, *args, **kwargs): 584 def assertWarns(self, expected_warning, callable_obj=None, *args, **kwargs):
574 """Fail unless a warning of class warnClass is triggered 585 """Fail unless a warning of class warnClass is triggered
575 by callable_obj when invoked with arguments args and keyword 586 by callable_obj when invoked with arguments args and keyword
576 arguments kwargs. If a different type of warning is 587 arguments kwargs. If a different type of warning is
577 triggered, it will not be handled: depending on the other 588 triggered, it will not be handled: depending on the other
578 warning filtering rules in effect, it might be silenced, printed 589 warning filtering rules in effect, it might be silenced, printed
579 out, or raised as an exception. 590 out, or raised as an exception.
(...skipping 626 matching lines...) Expand 10 before | Expand all | Expand 10 after
1206 1217
1207 def __repr__(self): 1218 def __repr__(self):
1208 return "<%s tec=%s>" % (strclass(self.__class__), 1219 return "<%s tec=%s>" % (strclass(self.__class__),
1209 self._testFunc) 1220 self._testFunc)
1210 1221
1211 def shortDescription(self): 1222 def shortDescription(self):
1212 if self._description is not None: 1223 if self._description is not None:
1213 return self._description 1224 return self._description
1214 doc = self._testFunc.__doc__ 1225 doc = self._testFunc.__doc__
1215 return doc and doc.split("\n")[0].strip() or None 1226 return doc and doc.split("\n")[0].strip() or None
OLDNEW

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