Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes
unittest.case._AssertWarnsContext Class Reference
Inheritance diagram for unittest.case._AssertWarnsContext:
Inheritance graph
[legend]
Collaboration diagram for unittest.case._AssertWarnsContext:
Collaboration graph
[legend]

List of all members.

Public Member Functions

def __enter__
def __exit__

Public Attributes

 warnings_manager
 warnings
 warning
 filename
 lineno
 expected
 failureException
 obj_name
 expected_regex
_PyObject_HEAD_EXTRA Py_ssize_t ob_refcnt
struct _typeobjectob_type

Detailed Description

A context manager used to implement TestCase.assertWarns* methods.

Definition at line 155 of file case.py.


Member Function Documentation

Definition at line 158 of file case.py.

00158 
00159     def __enter__(self):
00160         # The __warningregistry__'s need to be in a pristine state for tests
00161         # to work properly.
00162         for v in sys.modules.values():
00163             if getattr(v, '__warningregistry__', None):
00164                 v.__warningregistry__ = {}
00165         self.warnings_manager = warnings.catch_warnings(record=True)
00166         self.warnings = self.warnings_manager.__enter__()
00167         warnings.simplefilter("always", self.expected)
00168         return self

Here is the call graph for this function:

def unittest.case._AssertWarnsContext.__exit__ (   self,
  exc_type,
  exc_value,
  tb 
)

Definition at line 169 of file case.py.

00169 
00170     def __exit__(self, exc_type, exc_value, tb):
00171         self.warnings_manager.__exit__(exc_type, exc_value, tb)
00172         if exc_type is not None:
00173             # let unexpected exceptions pass through
00174             return
00175         try:
00176             exc_name = self.expected.__name__
00177         except AttributeError:
00178             exc_name = str(self.expected)
00179         first_matching = None
00180         for m in self.warnings:
00181             w = m.message
00182             if not isinstance(w, self.expected):
00183                 continue
00184             if first_matching is None:
00185                 first_matching = w
00186             if (self.expected_regex is not None and
00187                 not self.expected_regex.search(str(w))):
00188                 continue
00189             # store warning for later retrieval
00190             self.warning = w
00191             self.filename = m.filename
00192             self.lineno = m.lineno
00193             return
00194         # Now we simply try to choose a helpful failure message
00195         if first_matching is not None:
00196             raise self.failureException('"%s" does not match "%s"' %
00197                      (self.expected_regex.pattern, str(first_matching)))
00198         if self.obj_name:
00199             raise self.failureException("{0} not triggered by {1}"
00200                 .format(exc_name, self.obj_name))
00201         else:
00202             raise self.failureException("{0} not triggered"
00203                 .format(exc_name))
00204 


Member Data Documentation

Definition at line 108 of file case.py.

Definition at line 119 of file case.py.

Definition at line 109 of file case.py.

Definition at line 190 of file case.py.

Definition at line 191 of file case.py.

Definition at line 107 of file object.h.

struct _typeobject* _object::ob_type [inherited]

Definition at line 108 of file object.h.

Definition at line 112 of file case.py.

Definition at line 189 of file case.py.

Definition at line 165 of file case.py.

Definition at line 164 of file case.py.


The documentation for this class was generated from the following file: