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)
41 def stopTest(self, test):
42 self._events.append(('stopTest', test))
43 super(LoggingResult, self).stopTest(test)
45 def addFailure(self, test, error):
46 self._events.append(('addFailure', test, error))
47 super(LoggingResult, self).addFailure(test, error)
49 def addError(self, test, error):
50 self._events.append(('addError', test, error))
51 super(LoggingResult, self).addError(test, error)
53 def addSkip(self, test, reason):
54 self._events.append(('addSkip', test, reason))
55 super(LoggingResult, self).addSkip(test, reason)
57 def addSuccess(self, test):
58 self._events.append(('addSuccess', test))
59 super(LoggingResult, self).addSuccess(test)
61 def startTestRun(self):
62 self._events.append('startTestRun')
63 super(LoggingResult, self).startTestRun()
65 def stopTestRun(self):
66 self._events.append('stopTestRun')
67 super(LoggingResult, self).stopTestRun()
70 self._events.append('done')
71 super(LoggingResult, self).done()
73 def tags(self, new_tags, gone_tags):
74 self._events.append(('tags', new_tags, gone_tags))
75 super(LoggingResult, self).tags(new_tags, gone_tags)
77 def time(self, a_datetime):
78 self._events.append(('time', a_datetime))
79 super(LoggingResult, self).time(a_datetime)
82 def is_stack_hidden():
83 return TracebackContent.HIDE_INTERNAL_STACK
86 def hide_testtools_stack(should_hide=True):
87 result = TracebackContent.HIDE_INTERNAL_STACK
88 TracebackContent.HIDE_INTERNAL_STACK = should_hide
92 def run_with_stack_hidden(should_hide, f, *args, **kwargs):
93 old_should_hide = hide_testtools_stack(should_hide)
95 return f(*args, **kwargs)
97 hide_testtools_stack(old_should_hide)
100 class FullStackRunTest(runtest.RunTest):
102 def _run_user(self, fn, *args, **kwargs):
103 return run_with_stack_hidden(
105 super(FullStackRunTest, self)._run_user, fn, *args, **kwargs)