-
Notifications
You must be signed in to change notification settings - Fork 853
/
testing.py
34 lines (30 loc) · 1.14 KB
/
testing.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# Sebastian Raschka 2014-2020
# mlxtend Machine Learning Library Extensions
#
# A counter class for printing the progress of an iterator.
# Author: Sebastian Raschka <sebastianraschka.com>
#
# License: BSD 3 clause
def assert_raises(exception_type, message, func, *args, **kwargs):
"""Check that an exception is raised with a specific message
Parameters
----------
exception_type : exception
The exception that should be raised
message : str (default: None)
The error message that should be raised. Ignored if False or None.
func : callable
The function that raises the exception
*args : positional arguments to `func`.
**kwargs : keyword arguments to `func`
"""
try:
func(*args, **kwargs)
except exception_type as e:
error_message = str(e)
if message and message not in error_message:
raise AssertionError("Error message differs from the expected"
" string: %r. Got error message: %r" %
(message, error_message))
else:
raise AssertionError('%s not raised.' % exception_type.__name__)