1 # Copyright (c) 2008-2012 testtools developers. See LICENSE for details.
3 """Helpers for tests."""
11 from testtools import TestResult
12 from testtools.helpers import (
15 from testtools.content import TracebackContent
16 from testtools import runtest
19 # Importing to preserve compatibility.
22 # GZ 2010-08-12: Don't do this, pointlessly creates an exc_info cycle
26 an_exc_info = sys.exc_info()
28 # Deprecated: This classes attributes are somewhat non deterministic which
29 # leads to hard to predict tests (because Python upstream are changing things.
30 class LoggingResult(TestResult):
31 """TestResult that logs its event to a list."""
33 def __init__(self, log):
35 super(LoggingResult, self).__init__()
37 def startTest(self, test):
38 self._events.append(('startTest', test))
39 super(LoggingResult, self).startTest(test)
42 self._events.append('stop')
43 super(LoggingResult, self).stop()
45 def stopTest(self, test):
46 self._events.append(('stopTest', test))
47 super(LoggingResult, self).stopTest(test)
49 def addFailure(self, test, error):
50 self._events.append(('addFailure', test, error))
51 super(LoggingResult, self).addFailure(test, error)
53 def addError(self, test, error):
54 self._events.append(('addError', test, error))
55 super(LoggingResult, self).addError(test, error)
57 def addSkip(self, test, reason):
58 self._events.append(('addSkip', test, reason))
59 super(LoggingResult, self).addSkip(test, reason)
61 def addSuccess(self, test):
62 self._events.append(('addSuccess', test))
63 super(LoggingResult, self).addSuccess(test)
65 def startTestRun(self):
66 self._events.append('startTestRun')
67 super(LoggingResult, self).startTestRun()
69 def stopTestRun(self):
70 self._events.append('stopTestRun')
71 super(LoggingResult, self).stopTestRun()
74 self._events.append('done')
75 super(LoggingResult, self).done()
77 def tags(self, new_tags, gone_tags):
78 self._events.append(('tags', new_tags, gone_tags))
79 super(LoggingResult, self).tags(new_tags, gone_tags)
81 def time(self, a_datetime):
82 self._events.append(('time', a_datetime))
83 super(LoggingResult, self).time(a_datetime)
86 def is_stack_hidden():
87 return TracebackContent.HIDE_INTERNAL_STACK
90 def hide_testtools_stack(should_hide=True):
91 result = TracebackContent.HIDE_INTERNAL_STACK
92 TracebackContent.HIDE_INTERNAL_STACK = should_hide
96 def run_with_stack_hidden(should_hide, f, *args, **kwargs):
97 old_should_hide = hide_testtools_stack(should_hide)
99 return f(*args, **kwargs)
101 hide_testtools_stack(old_should_hide)
104 class FullStackRunTest(runtest.RunTest):
106 def _run_user(self, fn, *args, **kwargs):
107 return run_with_stack_hidden(
109 super(FullStackRunTest, self)._run_user, fn, *args, **kwargs)