1
# Copyright (c) 2001-2007 Twisted Matrix Laboratories.
2
# See LICENSE for details.
4
# Maintainer: Jonathan Lange
10
from zope.interface import implements
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
19
pyunit = __import__('unittest')
22
class TestPyUnitTestCase(TestCase):
24
class PyUnitTest(pyunit.TestCase):
31
self.original = self.PyUnitTest('test_pass')
32
self.test = ITestCase(self.original)
37
Trial assumes that test cases implement visit().
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")]
49
def test_callable(self):
51
Tests must be callable in order to be used with Python's unittest.py.
53
self.assertTrue(callable(self.test),
54
"%r is not callable." % (self.test,))
57
class TestPyUnitResult(TestCase):
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
64
def test_dontUseAdapterWhenReporterProvidesIReporter(self):
66
The L{PyUnitResultAdapter} is only used when the result passed to
67
C{run} does *not* provide L{IReporter}.
69
class StubReporter(object):
71
A reporter which records data about calls made to it.
73
@ivar errors: Errors passed to L{addError}.
74
@ivar failures: Failures passed to L{addFailure}.
83
def startTest(self, test):
88
def stopTest(self, test):
93
def addError(self, test, error):
97
self.errors.append(error)
99
test = erroneous.ErrorTest("test_foo")
100
result = StubReporter()
102
self.assertIsInstance(result.errors[0], Failure)
105
def test_success(self):
106
class SuccessTest(TestCase):
110
test = SuccessTest('test_foo')
111
result = pyunit.TestResult()
114
self.failUnless(test.ran)
115
self.assertEqual(1, result.testsRun)
116
self.failUnless(result.wasSuccessful())
118
def test_failure(self):
119
class FailureTest(TestCase):
124
test = FailureTest('test_foo')
125
result = pyunit.TestResult()
128
self.failUnless(test.ran)
129
self.assertEqual(1, result.testsRun)
130
self.assertEqual(1, len(result.failures))
131
self.failIf(result.wasSuccessful())
133
def test_error(self):
134
test = erroneous.ErrorTest('test_foo')
135
result = pyunit.TestResult()
138
self.failUnless(test.ran)
139
self.assertEqual(1, result.testsRun)
140
self.assertEqual(1, len(result.errors))
141
self.failIf(result.wasSuccessful())
143
def test_setUpError(self):
144
class ErrorTest(TestCase):
150
test = ErrorTest('test_foo')
151
result = pyunit.TestResult()
154
self.failIf(test.ran)
155
self.assertEqual(1, result.testsRun)
156
self.assertEqual(1, len(result.errors))
157
self.failIf(result.wasSuccessful())
159
def test_tracebackFromFailure(self):
161
Errors added through the L{PyUnitResultAdapter} have the same traceback
162
information as if there were no adapter at all.
166
except ZeroDivisionError:
167
exc_info = sys.exc_info()
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)))
176
def test_traceback(self):
178
As test_tracebackFromFailure, but covering more code.
180
class ErrorTest(TestCase):
185
except ZeroDivisionError:
186
self.exc_info = sys.exc_info()
188
test = ErrorTest('test_foo')
189
result = pyunit.TestResult()
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.
196
# So, we just test that the result's stack ends with the the
199
expected_stack = ''.join(traceback.format_tb(test.exc_info[2]))
200
observed_stack = '\n'.join(result.errors[0][1].splitlines()[:-1])
202
self.assertEqual(expected_stack.strip(),
203
observed_stack[-len(expected_stack):].strip())
206
def test_tracebackFromCleanFailure(self):
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.
214
except ZeroDivisionError:
215
exc_info = sys.exc_info()
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)))