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

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
« no previous file with comments | « no previous file | Lib/unittest/test/test_case.py » ('j') | Lib/unittest/test/test_case.py » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 """Test case implementation""" 1 """Test case implementation"""
2 2
3 import collections 3 import collections
4 import sys 4 import sys
5 import functools 5 import functools
6 import difflib 6 import difflib
7 import pprint 7 import pprint
8 import re 8 import re
9 import types 9 import types
10 import warnings 10 import warnings
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
86 86
87 def expectedFailure(func): 87 def expectedFailure(func):
88 @functools.wraps(func) 88 @functools.wraps(func)
89 def wrapper(*args, **kwargs): 89 def wrapper(*args, **kwargs):
90 try: 90 try:
91 func(*args, **kwargs) 91 func(*args, **kwargs)
92 except Exception: 92 except Exception:
93 raise _ExpectedFailure(sys.exc_info()) 93 raise _ExpectedFailure(sys.exc_info())
94 raise _UnexpectedSuccess 94 raise _UnexpectedSuccess
95 return wrapper 95 return wrapper
96
97 def _ensure_is_exception_type(e):
98 if isinstance(e, types.ClassType):
99 return
ezio.melotti 2014/06/29 11:24:12 If this is to support exceptions that are not deri
100 if not isinstance(e, type) or not issubclass(e, BaseException):
101 raise TypeError('assertRaises arg 1 must be an exception type '
102 'or tuple of exception types')
96 103
97 104
98 class _AssertRaisesContext(object): 105 class _AssertRaisesContext(object):
99 """A context manager used to implement TestCase.assertRaises* methods.""" 106 """A context manager used to implement TestCase.assertRaises* methods."""
100 107
101 def __init__(self, expected, test_case, expected_regexp=None): 108 def __init__(self, expected, test_case, expected_regexp=None):
102 self.expected = expected 109 self.expected = expected
110
111 if type(expected) is tuple:
Claudiu.Popa 2014/06/14 17:01:42 You could use isinstance here.
112 for e in expected:
113 _ensure_is_exception_type(e)
114 else:
115 _ensure_is_exception_type(expected)
116
103 self.failureException = test_case.failureException 117 self.failureException = test_case.failureException
104 self.expected_regexp = expected_regexp 118 self.expected_regexp = expected_regexp
105 119
106 def __enter__(self): 120 def __enter__(self):
107 return self 121 return self
108 122
109 def __exit__(self, exc_type, exc_value, tb): 123 def __exit__(self, exc_type, exc_value, tb):
110 if exc_type is None: 124 if exc_type is None:
111 try: 125 try:
112 exc_name = self.expected.__name__ 126 exc_name = self.expected.__name__
(...skipping 955 matching lines...) Expand 10 before | Expand all | Expand 10 after
1068 1082
1069 def __repr__(self): 1083 def __repr__(self):
1070 return "<%s tec=%s>" % (strclass(self.__class__), 1084 return "<%s tec=%s>" % (strclass(self.__class__),
1071 self._testFunc) 1085 self._testFunc)
1072 1086
1073 def shortDescription(self): 1087 def shortDescription(self):
1074 if self._description is not None: 1088 if self._description is not None:
1075 return self._description 1089 return self._description
1076 doc = self._testFunc.__doc__ 1090 doc = self._testFunc.__doc__
1077 return doc and doc.split("\n")[0].strip() or None 1091 return doc and doc.split("\n")[0].strip() or None
OLDNEW
« no previous file with comments | « no previous file | Lib/unittest/test/test_case.py » ('j') | Lib/unittest/test/test_case.py » ('J')

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