MCPcopy Index your code
hub / github.com/python/cpython / assertRaises

Method assertRaises

Lib/test/test_optparse.py:80–134  ·  view source on GitHub ↗

Assert that the expected exception is raised when calling a function, and that the right error message is included with that exception. Arguments: func -- the function to call args -- positional arguments to `func` kwargs -- keyword arg

(self,
                     func,
                     args,
                     kwargs,
                     expected_exception,
                     expected_message)

Source from the content-addressed store, hash-verified

78 return (options, positional_args)
79
80 def assertRaises(self,
81 func,
82 args,
83 kwargs,
84 expected_exception,
85 expected_message):
86 """
87 Assert that the expected exception is raised when calling a
88 function, and that the right error message is included with
89 that exception.
90
91 Arguments:
92 func -- the function to call
93 args -- positional arguments to `func`
94 kwargs -- keyword arguments to `func`
95 expected_exception -- exception that should be raised
96 expected_message -- expected exception message (or pattern
97 if a compiled regex object)
98
99 Returns the exception raised for further testing.
100 """
101 if args is None:
102 args = ()
103 if kwargs is None:
104 kwargs = {}
105
106 try:
107 func(*args, **kwargs)
108 except expected_exception as err:
109 actual_message = str(err)
110 if isinstance(expected_message, re.Pattern):
111 self.assertTrue(expected_message.search(actual_message),
112 """\
113expected exception message pattern:
114/%s/
115actual exception message:
116'''%s'''
117""" % (expected_message.pattern, actual_message))
118 else:
119 self.assertEqual(actual_message,
120 expected_message,
121 """\
122expected exception message:
123'''%s'''
124actual exception message:
125'''%s'''
126""" % (expected_message, actual_message))
127
128 return err
129 else:
130 self.fail("""expected exception %(expected_exception)s not raised
131called %(func)r
132with args %(args)r
133and kwargs %(kwargs)r
134""" % locals ())
135
136
137 # -- Assertions used in more than one class --------------------

Callers 15

checkraisesMethod · 0.95
test_indexMethod · 0.95
test_rindexMethod · 0.95
test_no_chainingMethod · 0.95
test_evil_repr3Method · 0.95
test_isinstanceMethod · 0.95
test_issubclassMethod · 0.95
test_no_kwargsMethod · 0.95
test_constructorMethod · 0.95

Calls 6

strFunction · 0.85
assertTrueMethod · 0.80
funcFunction · 0.70
searchMethod · 0.45
assertEqualMethod · 0.45
failMethod · 0.45

Tested by

no test coverage detected