mirror of
https://git.centos.org/centos/centpkg.git
synced 2025-02-23 08:12:55 +00:00
add an assertWarns context manager for unit tests
This commit is contained in:
parent
639034222f
commit
5becc45d19
1 changed files with 27 additions and 0 deletions
|
@ -1,5 +1,32 @@
|
|||
import os
|
||||
import sys
|
||||
import unittest
|
||||
import warnings
|
||||
|
||||
sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)),
|
||||
'../src'))
|
||||
|
||||
class CatchWarningsMixin(object):
|
||||
class assertWarns(object):
|
||||
def __init__(self, warningtype, msg=''):
|
||||
self.warningtype = warningtype
|
||||
warnings.filterwarnings('error')
|
||||
self.failureException = unittest.TestCase.failureException
|
||||
|
||||
def __enter__(self):
|
||||
return self
|
||||
|
||||
def __exit__(self, exc_type, exc_value, tb):
|
||||
if exc_type is None:
|
||||
try:
|
||||
exc_name = self.warningtype.__name__
|
||||
except AttributeError:
|
||||
exc_name = str(self.warningtype)
|
||||
raise self.failureException(
|
||||
"{0} not raised".format(exc_name))
|
||||
|
||||
if not issubclass(exc_type, self.warningtype):
|
||||
raise self.failureException('"%s" does not match "%s"' %
|
||||
(self.warningtype.__name__, str(exc_type.__name__)))
|
||||
|
||||
return True
|
||||
|
|
Loading…
Add table
Reference in a new issue