@contextmanager
def expected_warnings(matching):
 with all_warnings() as w:
  (yield w)
  remaining = [m for m in matching if ('\\A\\Z' not in m.split('|'))]
  for warn in w:
   found = False
   for match in matching:
    if (re.search(match, str(warn.message)) is not None):
     found = True
     if (match in remaining):
      remaining.remove(match)
   if (not found):
    raise ValueError(('Unexpected warning: %s' % str(warn.message)))
  if (len(remaining) > 0):
   msg = ('No warning raised matching:\n%s' % '\n'.join(remaining))
   raise ValueError(msg)

@ contextmanager def expected_warnings ( matching ) : with all_warnings ( ) as w : ( yield w ) remaining = [ m for m in matching if ( '\\A\\Z' not in m . split ( '|' ) ) ] for warn in w : found = False for match in matching : if ( re . search ( match , str ( warn . message ) ) is not None ) : found = True if ( match in remaining ) : remaining . remove ( match ) if ( not found ) : raise ValueError ( ( 'Unexpected warning: %s' % str ( warn . message ) ) ) if ( len ( remaining ) > 0 ) : msg = ( 'No warning raised matching:\n%s' % '\n' . join ( remaining ) ) raise ValueError ( msg )
context for use in testing to catch known warnings matching regexes parameters matching : list of strings or compiled regexes regexes for the desired warning to catch examples .