From ddb17b05cb747b4d0d896f23b5f56df6952a69fd Mon Sep 17 00:00:00 2001 From: Brian Stinson Date: Jul 20 2015 21:51:20 +0000 Subject: moving the mixins to their own module to hide them in stackframes Reference: https://mail.python.org/pipermail/python-list/2012-October/632386.html --- diff --git a/tests/__init__.py b/tests/__init__.py index 547e947..6c4d60c 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -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) diff --git a/tests/mixins.py b/tests/mixins.py new file mode 100644 index 0000000..49add16 --- /dev/null +++ b/tests/mixins.py @@ -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) diff --git a/tests/test_distgit.py b/tests/test_distgit.py index 1cc9a9d..4081522 100644 --- a/tests/test_distgit.py +++ b/tests/test_distgit.py @@ -1,7 +1,6 @@ import unittest - -from . import CatchWarningsMixin +from mixins import CatchWarningsMixin from centpkg import DistGitDirectory class TestDistGitNothing(unittest.TestCase):