| OLD | NEW |
| (Empty) |
| 1 # Copyright (c) 2001-2007 Twisted Matrix Laboratories. | |
| 2 # See LICENSE for details. | |
| 3 # | |
| 4 # Maintainer: Jonathan Lange <jml@twistedmatrix.com> | |
| 5 | |
| 6 | |
| 7 import sys | |
| 8 import traceback | |
| 9 | |
| 10 from zope.interface import implements | |
| 11 | |
| 12 from twisted.python import reflect | |
| 13 from twisted.python.failure import Failure | |
| 14 from twisted.trial import util | |
| 15 from twisted.trial.unittest import TestCase, PyUnitResultAdapter | |
| 16 from twisted.trial.itrial import IReporter, ITestCase | |
| 17 from twisted.trial.test import erroneous | |
| 18 | |
| 19 pyunit = __import__('unittest') | |
| 20 | |
| 21 | |
| 22 class TestPyUnitTestCase(TestCase): | |
| 23 | |
| 24 class PyUnitTest(pyunit.TestCase): | |
| 25 | |
| 26 def test_pass(self): | |
| 27 pass | |
| 28 | |
| 29 | |
| 30 def setUp(self): | |
| 31 self.original = self.PyUnitTest('test_pass') | |
| 32 self.test = ITestCase(self.original) | |
| 33 | |
| 34 | |
| 35 def test_visit(self): | |
| 36 """ | |
| 37 Trial assumes that test cases implement visit(). | |
| 38 """ | |
| 39 log = [] | |
| 40 def visitor(test): | |
| 41 log.append(test) | |
| 42 self.test.visit(visitor) | |
| 43 self.assertEqual(log, [self.test]) | |
| 44 test_visit.suppress = [ | |
| 45 util.suppress(category=DeprecationWarning, | |
| 46 message="Test visitors deprecated in Twisted 8.0")] | |
| 47 | |
| 48 | |
| 49 def test_callable(self): | |
| 50 """ | |
| 51 Tests must be callable in order to be used with Python's unittest.py. | |
| 52 """ | |
| 53 self.assertTrue(callable(self.test), | |
| 54 "%r is not callable." % (self.test,)) | |
| 55 | |
| 56 | |
| 57 class TestPyUnitResult(TestCase): | |
| 58 """ | |
| 59 Tests to show that PyUnitResultAdapter wraps TestResult objects from the | |
| 60 standard library 'unittest' module in such a way as to make them usable and | |
| 61 useful from Trial. | |
| 62 """ | |
| 63 | |
| 64 def test_dontUseAdapterWhenReporterProvidesIReporter(self): | |
| 65 """ | |
| 66 The L{PyUnitResultAdapter} is only used when the result passed to | |
| 67 C{run} does *not* provide L{IReporter}. | |
| 68 """ | |
| 69 class StubReporter(object): | |
| 70 """ | |
| 71 A reporter which records data about calls made to it. | |
| 72 | |
| 73 @ivar errors: Errors passed to L{addError}. | |
| 74 @ivar failures: Failures passed to L{addFailure}. | |
| 75 """ | |
| 76 | |
| 77 implements(IReporter) | |
| 78 | |
| 79 def __init__(self): | |
| 80 self.errors = [] | |
| 81 self.failures = [] | |
| 82 | |
| 83 def startTest(self, test): | |
| 84 """ | |
| 85 Do nothing. | |
| 86 """ | |
| 87 | |
| 88 def stopTest(self, test): | |
| 89 """ | |
| 90 Do nothing. | |
| 91 """ | |
| 92 | |
| 93 def addError(self, test, error): | |
| 94 """ | |
| 95 Record the error. | |
| 96 """ | |
| 97 self.errors.append(error) | |
| 98 | |
| 99 test = erroneous.ErrorTest("test_foo") | |
| 100 result = StubReporter() | |
| 101 test.run(result) | |
| 102 self.assertIsInstance(result.errors[0], Failure) | |
| 103 | |
| 104 | |
| 105 def test_success(self): | |
| 106 class SuccessTest(TestCase): | |
| 107 ran = False | |
| 108 def test_foo(s): | |
| 109 s.ran = True | |
| 110 test = SuccessTest('test_foo') | |
| 111 result = pyunit.TestResult() | |
| 112 test.run(result) | |
| 113 | |
| 114 self.failUnless(test.ran) | |
| 115 self.assertEqual(1, result.testsRun) | |
| 116 self.failUnless(result.wasSuccessful()) | |
| 117 | |
| 118 def test_failure(self): | |
| 119 class FailureTest(TestCase): | |
| 120 ran = False | |
| 121 def test_foo(s): | |
| 122 s.ran = True | |
| 123 s.fail('boom!') | |
| 124 test = FailureTest('test_foo') | |
| 125 result = pyunit.TestResult() | |
| 126 test.run(result) | |
| 127 | |
| 128 self.failUnless(test.ran) | |
| 129 self.assertEqual(1, result.testsRun) | |
| 130 self.assertEqual(1, len(result.failures)) | |
| 131 self.failIf(result.wasSuccessful()) | |
| 132 | |
| 133 def test_error(self): | |
| 134 test = erroneous.ErrorTest('test_foo') | |
| 135 result = pyunit.TestResult() | |
| 136 test.run(result) | |
| 137 | |
| 138 self.failUnless(test.ran) | |
| 139 self.assertEqual(1, result.testsRun) | |
| 140 self.assertEqual(1, len(result.errors)) | |
| 141 self.failIf(result.wasSuccessful()) | |
| 142 | |
| 143 def test_setUpError(self): | |
| 144 class ErrorTest(TestCase): | |
| 145 ran = False | |
| 146 def setUp(self): | |
| 147 1/0 | |
| 148 def test_foo(s): | |
| 149 s.ran = True | |
| 150 test = ErrorTest('test_foo') | |
| 151 result = pyunit.TestResult() | |
| 152 test.run(result) | |
| 153 | |
| 154 self.failIf(test.ran) | |
| 155 self.assertEqual(1, result.testsRun) | |
| 156 self.assertEqual(1, len(result.errors)) | |
| 157 self.failIf(result.wasSuccessful()) | |
| 158 | |
| 159 def test_tracebackFromFailure(self): | |
| 160 """ | |
| 161 Errors added through the L{PyUnitResultAdapter} have the same traceback | |
| 162 information as if there were no adapter at all. | |
| 163 """ | |
| 164 try: | |
| 165 1/0 | |
| 166 except ZeroDivisionError: | |
| 167 exc_info = sys.exc_info() | |
| 168 f = Failure() | |
| 169 pyresult = pyunit.TestResult() | |
| 170 result = PyUnitResultAdapter(pyresult) | |
| 171 result.addError(self, f) | |
| 172 self.assertEqual(pyresult.errors[0][1], | |
| 173 ''.join(traceback.format_exception(*exc_info))) | |
| 174 | |
| 175 | |
| 176 def test_traceback(self): | |
| 177 """ | |
| 178 As test_tracebackFromFailure, but covering more code. | |
| 179 """ | |
| 180 class ErrorTest(TestCase): | |
| 181 exc_info = None | |
| 182 def test_foo(self): | |
| 183 try: | |
| 184 1/0 | |
| 185 except ZeroDivisionError: | |
| 186 self.exc_info = sys.exc_info() | |
| 187 raise | |
| 188 test = ErrorTest('test_foo') | |
| 189 result = pyunit.TestResult() | |
| 190 test.run(result) | |
| 191 | |
| 192 # We can't test that the tracebacks are equal, because Trial's | |
| 193 # machinery inserts a few extra frames on the top and we don't really | |
| 194 # want to trim them off without an extremely good reason. | |
| 195 # | |
| 196 # So, we just test that the result's stack ends with the the | |
| 197 # exception's stack. | |
| 198 | |
| 199 expected_stack = ''.join(traceback.format_tb(test.exc_info[2])) | |
| 200 observed_stack = '\n'.join(result.errors[0][1].splitlines()[:-1]) | |
| 201 | |
| 202 self.assertEqual(expected_stack.strip(), | |
| 203 observed_stack[-len(expected_stack):].strip()) | |
| 204 | |
| 205 | |
| 206 def test_tracebackFromCleanFailure(self): | |
| 207 """ | |
| 208 Errors added through the L{PyUnitResultAdapter} have the same | |
| 209 traceback information as if there were no adapter at all, even | |
| 210 if the Failure that held the information has been cleaned. | |
| 211 """ | |
| 212 try: | |
| 213 1/0 | |
| 214 except ZeroDivisionError: | |
| 215 exc_info = sys.exc_info() | |
| 216 f = Failure() | |
| 217 f.cleanFailure() | |
| 218 pyresult = pyunit.TestResult() | |
| 219 result = PyUnitResultAdapter(pyresult) | |
| 220 result.addError(self, f) | |
| 221 self.assertEqual(pyresult.errors[0][1], | |
| 222 ''.join(traceback.format_exception(*exc_info))) | |
| OLD | NEW |