diff options
Diffstat (limited to 'lib/testtools/testtools/tests/helpers.py')
-rw-r--r-- | lib/testtools/testtools/tests/helpers.py | 53 |
1 files changed, 46 insertions, 7 deletions
diff --git a/lib/testtools/testtools/tests/helpers.py b/lib/testtools/testtools/tests/helpers.py index 5f3187db29..660cfecb72 100644 --- a/lib/testtools/testtools/tests/helpers.py +++ b/lib/testtools/testtools/tests/helpers.py @@ -1,15 +1,19 @@ -# Copyright (c) 2008 Jonathan M. Lange. See LICENSE for details. +# Copyright (c) 2008-2011 testtools developers. See LICENSE for details. """Helpers for tests.""" -import sys - -__metaclass__ = type __all__ = [ 'LoggingResult', ] +import sys + from testtools import TestResult +from testtools.helpers import ( + safe_hasattr, + try_import, + ) +from testtools import runtest # GZ 2010-08-12: Don't do this, pointlessly creates an exc_info cycle @@ -67,6 +71,41 @@ class LoggingResult(TestResult): self._events.append(('time', a_datetime)) super(LoggingResult, self).time(a_datetime) -# Note, the following three classes are different to LoggingResult by -# being fully defined exact matches rather than supersets. -from testtools.testresult.doubles import * + +def is_stack_hidden(): + return safe_hasattr(runtest, '__unittest') + + +def hide_testtools_stack(should_hide=True): + modules = [ + 'testtools.matchers', + 'testtools.runtest', + 'testtools.testcase', + ] + result = is_stack_hidden() + for module_name in modules: + module = try_import(module_name) + if should_hide: + setattr(module, '__unittest', True) + else: + try: + delattr(module, '__unittest') + except AttributeError: + # Attribute already doesn't exist. Our work here is done. + pass + return result + + +def run_with_stack_hidden(should_hide, f, *args, **kwargs): + old_should_hide = hide_testtools_stack(should_hide) + try: + return f(*args, **kwargs) + finally: + hide_testtools_stack(old_should_hide) + + + +class FullStackRunTest(runtest.RunTest): + + def _run_user(self, fn, *args, **kwargs): + return run_with_stack_hidden(False, fn, *args, **kwargs) |