testtools: Update to latest version.
[nivanova/samba-autobuild/.git] / lib / testtools / testtools / tests / helpers.py
1 # Copyright (c) 2008-2012 testtools developers. See LICENSE for details.
2
3 """Helpers for tests."""
4
5 __all__ = [
6     'LoggingResult',
7     ]
8
9 import sys
10
11 from testtools import TestResult
12 from testtools.helpers import (
13     safe_hasattr,
14     )
15 from testtools.content import TracebackContent
16 from testtools import runtest
17
18
19 # Importing to preserve compatibility.
20 safe_hasattr
21
22 # GZ 2010-08-12: Don't do this, pointlessly creates an exc_info cycle
23 try:
24     raise Exception
25 except Exception:
26     an_exc_info = sys.exc_info()
27
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."""
32
33     def __init__(self, log):
34         self._events = log
35         super(LoggingResult, self).__init__()
36
37     def startTest(self, test):
38         self._events.append(('startTest', test))
39         super(LoggingResult, self).startTest(test)
40
41     def stopTest(self, test):
42         self._events.append(('stopTest', test))
43         super(LoggingResult, self).stopTest(test)
44
45     def addFailure(self, test, error):
46         self._events.append(('addFailure', test, error))
47         super(LoggingResult, self).addFailure(test, error)
48
49     def addError(self, test, error):
50         self._events.append(('addError', test, error))
51         super(LoggingResult, self).addError(test, error)
52
53     def addSkip(self, test, reason):
54         self._events.append(('addSkip', test, reason))
55         super(LoggingResult, self).addSkip(test, reason)
56
57     def addSuccess(self, test):
58         self._events.append(('addSuccess', test))
59         super(LoggingResult, self).addSuccess(test)
60
61     def startTestRun(self):
62         self._events.append('startTestRun')
63         super(LoggingResult, self).startTestRun()
64
65     def stopTestRun(self):
66         self._events.append('stopTestRun')
67         super(LoggingResult, self).stopTestRun()
68
69     def done(self):
70         self._events.append('done')
71         super(LoggingResult, self).done()
72
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)
76
77     def time(self, a_datetime):
78         self._events.append(('time', a_datetime))
79         super(LoggingResult, self).time(a_datetime)
80
81
82 def is_stack_hidden():
83     return TracebackContent.HIDE_INTERNAL_STACK
84
85
86 def hide_testtools_stack(should_hide=True):
87     result = TracebackContent.HIDE_INTERNAL_STACK
88     TracebackContent.HIDE_INTERNAL_STACK = should_hide
89     return result
90
91
92 def run_with_stack_hidden(should_hide, f, *args, **kwargs):
93     old_should_hide = hide_testtools_stack(should_hide)
94     try:
95         return f(*args, **kwargs)
96     finally:
97         hide_testtools_stack(old_should_hide)
98
99
100 class FullStackRunTest(runtest.RunTest):
101
102     def _run_user(self, fn, *args, **kwargs):
103         return run_with_stack_hidden(
104             False,
105             super(FullStackRunTest, self)._run_user, fn, *args, **kwargs)