1# Copyright (c) 2008-2012 testtools developers. See LICENSE for details. 2 3"""Helpers for tests.""" 4 5__all__ = [ 6 'LoggingResult', 7 ] 8 9import sys 10 11from extras import safe_hasattr 12 13from testtools import TestResult 14from testtools.content import StackLinesContent 15from testtools import runtest 16 17 18# Importing to preserve compatibility. 19safe_hasattr 20 21# GZ 2010-08-12: Don't do this, pointlessly creates an exc_info cycle 22try: 23 raise Exception 24except Exception: 25 an_exc_info = sys.exc_info() 26 27# Deprecated: This classes attributes are somewhat non deterministic which 28# leads to hard to predict tests (because Python upstream are changing things. 29class LoggingResult(TestResult): 30 """TestResult that logs its event to a list.""" 31 32 def __init__(self, log): 33 self._events = log 34 super(LoggingResult, self).__init__() 35 36 def startTest(self, test): 37 self._events.append(('startTest', test)) 38 super(LoggingResult, self).startTest(test) 39 40 def stop(self): 41 self._events.append('stop') 42 super(LoggingResult, self).stop() 43 44 def stopTest(self, test): 45 self._events.append(('stopTest', test)) 46 super(LoggingResult, self).stopTest(test) 47 48 def addFailure(self, test, error): 49 self._events.append(('addFailure', test, error)) 50 super(LoggingResult, self).addFailure(test, error) 51 52 def addError(self, test, error): 53 self._events.append(('addError', test, error)) 54 super(LoggingResult, self).addError(test, error) 55 56 def addSkip(self, test, reason): 57 self._events.append(('addSkip', test, reason)) 58 super(LoggingResult, self).addSkip(test, reason) 59 60 def addSuccess(self, test): 61 self._events.append(('addSuccess', test)) 62 super(LoggingResult, self).addSuccess(test) 63 64 def startTestRun(self): 65 self._events.append('startTestRun') 66 super(LoggingResult, self).startTestRun() 67 68 def stopTestRun(self): 69 self._events.append('stopTestRun') 70 super(LoggingResult, self).stopTestRun() 71 72 def done(self): 73 self._events.append('done') 74 super(LoggingResult, self).done() 75 76 def tags(self, new_tags, gone_tags): 77 self._events.append(('tags', new_tags, gone_tags)) 78 super(LoggingResult, self).tags(new_tags, gone_tags) 79 80 def time(self, a_datetime): 81 self._events.append(('time', a_datetime)) 82 super(LoggingResult, self).time(a_datetime) 83 84 85def is_stack_hidden(): 86 return StackLinesContent.HIDE_INTERNAL_STACK 87 88 89def hide_testtools_stack(should_hide=True): 90 result = StackLinesContent.HIDE_INTERNAL_STACK 91 StackLinesContent.HIDE_INTERNAL_STACK = should_hide 92 return result 93 94 95def run_with_stack_hidden(should_hide, f, *args, **kwargs): 96 old_should_hide = hide_testtools_stack(should_hide) 97 try: 98 return f(*args, **kwargs) 99 finally: 100 hide_testtools_stack(old_should_hide) 101 102 103class FullStackRunTest(runtest.RunTest): 104 105 def _run_user(self, fn, *args, **kwargs): 106 return run_with_stack_hidden( 107 False, 108 super(FullStackRunTest, self)._run_user, fn, *args, **kwargs) 109