testing.assert_warns(warning_class, *args, **kwargs)
Fail unless the given callable throws the specified warning.
A warning of class warning_class should be thrown by the callable when invoked with arguments args and keyword arguments kwargs. If a different type of warning is thrown, it will not be caught.
If called with all arguments other than the warning class omitted, may be used as a context manager:
withassert_warns(SomeWarning):do_something()The ability to be used as a context manager is new in NumPy v1.11.0.
Deprecated since version 2.4: This is deprecated. Use
or pytest.warns instead.
Parameters:warning_classclassThe class defining the warning that func is expected to throw.
funccallable, optionalCallable to test
*argsArgumentsArguments for func.
**kwargsKwargsKeyword arguments for func.
Returns:The value returned by func.Examples
>>> importwarnings>>> defdeprecated_func(num):... warnings.warn("Please upgrade",DeprecationWarning)... returnnum*num>>> withnp.testing.assert_warns(DeprecationWarning):... assertdeprecated_func(4)==16>>> # or passing a func>>> ret=np.testing.assert_warns(DeprecationWarning,deprecated_func,4)>>> assertret==16