- class spacepy_testing.assertWarns(test, action='always', message='', category=<class 'Warning'>, module='', lineno=0)¶
Tests that a warning is raised.
Use as a context manager. Code within the context manager block will be executed and, on exit from the block, all warnings issued during execution of the block will be checked to see if the warning specified was issued.
assertWarnsrequires that the matched warning be issued exactly once within the context manager, or the test function will fail (whether the warning was issued not at all, or multiple times).
assertDoesntWarnrequires that matched warnings are not issued at all.
All other warnings are issued as normal, although the warning will not be shown (e.g. printed) until the exit of the context manager. If code within the context manager issues an exception, the test for warnings will be skipped (test failure will not be issued), and all warnings shown before the exception propagates up the stack.
The parameters determining which warning to match are for the code referenced in the warning, not necessarily the code being warned. E.g. if code calls a deprecated function, and the deprecated function issues a
DeprecationWarning, what is matched may be the code in the deprecated function, not the caller; see the
warn()for how this may be changed.
The test case from which this is being called, almost always
fail()method is available).
None, ‘default’, ‘error’, ‘ignore’, ‘module’,
None, a warning filter matching the specified warning will be added to the filter before executing the block. ‘always’ (default) is generally recommended to make sure the tested warning will be raised. If ‘always’ is specified, on Python 2 the log of previously-issued warnings will be edited to work around a Python bug. In this case using
moduleis strongly recommended to minimize the impact of this editing. This filter will be removed on completion of the block.
- messagestr, optional
Regular expression to match the start of warning message. Default is to match all.
- categorytype, optional
Matches if the warning is an instance of this type or a subclass. Default is the base Warning type (matches all warnings).
- modulestr, optional
Regular expression to match the start of the name of the module from which the warning is issued. This is primarily used in setting up the warnings filter; matching the module to determine if the desired warning was issued is based on filename and may not work for modules built into the interpreter. Default is to match all.
- linenoint, optional
Line number from which the warning was issued. This is rarely useful since it will change from version to version. Default (0) will match all lines.
linenoparameters are based on the filter specifications.
This class is primarily useful as part of a test suite, and cannot be easily demonstrated through interactive examples. See the tests of it in
test_testing.pyand its usage throughout the test suite.