moving the mixins to their own module to hide them in stackframes

Reference:
https://mail.python.org/pipermail/python-list/2012-October/632386.html
This commit is contained in:
Brian Stinson 2015-07-20 16:20:37 -05:00
parent cf167f1c37
commit ddb17b05cb
3 changed files with 36 additions and 34 deletions

View file

@ -1,38 +1,6 @@
import os
import sys
import warnings
sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)),
'../src'))
class _AssertWarnsContext(object):
def __init__(self, warningtype, testcase, msg=''):
self.warningtype = warningtype
warnings.filterwarnings('error')
self.failureException = 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
class CatchWarningsMixin(object):
def assertWarns(self, wrnClass, callableObj=None, *args, **kwargs):
context = _AssertWarnsContext(wrnClass, self)
if callableObj is None:
return context
with context:
callableObj(*args, **kwargs)

35
tests/mixins.py Normal file
View file

@ -0,0 +1,35 @@
import warnings
__unittest=True
class _AssertWarnsContext(object):
def __init__(self, warningtype, testcase, msg=''):
self.warningtype = warningtype
warnings.filterwarnings('error')
self.failureException = 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
class CatchWarningsMixin(object):
def assertWarns(self, wrnClass, callableObj=None, *args, **kwargs):
context = _AssertWarnsContext(wrnClass, self)
if callableObj is None:
return context
with context:
callableObj(*args, **kwargs)

View file

@ -1,7 +1,6 @@
import unittest
from . import CatchWarningsMixin
from mixins import CatchWarningsMixin
from centpkg import DistGitDirectory
class TestDistGitNothing(unittest.TestCase):