1
# Copyright (c) 2001-2009 Twisted Matrix Laboratories.
2
# See LICENSE for details.
5
Tests for the behaviour of unit tests.
8
import gc, StringIO, sys, weakref
10
from twisted.internet import defer, reactor
11
from twisted.trial import unittest, runner, reporter, util
12
from twisted.trial.test import erroneous, suppression
13
from twisted.trial.test.test_reporter import LoggingReporter
16
class ResultsTestMixin:
17
def loadSuite(self, suite):
18
self.loader = runner.TestLoader()
19
self.suite = self.loader.loadClass(suite)
20
self.reporter = reporter.TestResult()
23
self.failUnless(self.reporter.wasSuccessful())
24
self.failUnlessEqual(self.reporter.errors, [])
25
self.failUnlessEqual(self.reporter.failures, [])
26
self.failUnlessEqual(self.reporter.skips, [])
28
def assertCount(self, numTests):
29
self.failUnlessEqual(self.suite.countTestCases(), numTests)
30
self.suite(self.reporter)
31
self.failUnlessEqual(self.reporter.testsRun, numTests)
35
class TestSuccess(unittest.TestCase):
37
Test that successful tests are reported as such.
41
self.result = reporter.TestResult()
44
def test_successful(self):
46
A successful test, used by other tests.
50
def assertSuccessful(self, test, result):
51
self.assertEqual(result.successes, 1)
52
self.assertEqual(result.failures, [])
53
self.assertEqual(result.errors, [])
54
self.assertEqual(result.expectedFailures, [])
55
self.assertEqual(result.unexpectedSuccesses, [])
56
self.assertEqual(result.skips, [])
59
def test_successfulIsReported(self):
61
Test that when a successful test is run, it is reported as a success,
62
and not as any other kind of result.
64
test = TestSuccess('test_successful')
66
self.assertSuccessful(test, self.result)
69
def test_defaultIsSuccessful(self):
71
Test that L{unittest.TestCase} itself can be instantiated, run, and
72
reported as being successful.
74
test = unittest.TestCase()
76
self.assertSuccessful(test, self.result)
79
def test_noReference(self):
81
Test that no reference is kept on a successful test.
83
test = TestSuccess('test_successful')
84
ref = weakref.ref(test)
86
self.assertSuccessful(test, self.result)
89
self.assertIdentical(ref(), None)
93
class TestSkipMethods(unittest.TestCase, ResultsTestMixin):
94
class SkippingTests(unittest.TestCase):
96
raise unittest.SkipTest('skip1')
99
raise RuntimeError("I should not get raised")
100
test_skip2.skip = 'skip2'
102
def test_skip3(self):
103
self.fail('I should not fail')
104
test_skip3.skip = 'skip3'
106
class SkippingSetUp(unittest.TestCase):
108
raise unittest.SkipTest('skipSetUp')
117
self.loadSuite(TestSkipMethods.SkippingTests)
119
def test_counting(self):
122
def test_results(self):
123
self.suite(self.reporter)
124
self.failUnless(self.reporter.wasSuccessful())
125
self.failUnlessEqual(self.reporter.errors, [])
126
self.failUnlessEqual(self.reporter.failures, [])
127
self.failUnlessEqual(len(self.reporter.skips), 3)
129
def test_setUp(self):
130
self.loadSuite(TestSkipMethods.SkippingSetUp)
131
self.suite(self.reporter)
132
self.failUnless(self.reporter.wasSuccessful())
133
self.failUnlessEqual(self.reporter.errors, [])
134
self.failUnlessEqual(self.reporter.failures, [])
135
self.failUnlessEqual(len(self.reporter.skips), 2)
137
def test_reasons(self):
138
self.suite(self.reporter)
140
# whiteboxing reporter
141
for test, reason in self.reporter.skips:
142
self.failUnlessEqual(test.shortDescription()[len(prefix):],
146
class TestSkipClasses(unittest.TestCase, ResultsTestMixin):
147
class SkippedClass(unittest.TestCase):
150
self.__class__._setUpRan = True
151
def test_skip1(self):
152
raise unittest.SkipTest('skip1')
153
def test_skip2(self):
154
raise RuntimeError("Ought to skip me")
155
test_skip2.skip = 'skip2'
156
def test_skip3(self):
158
def test_skip4(self):
159
raise RuntimeError("Skip me too")
163
self.loadSuite(TestSkipClasses.SkippedClass)
164
TestSkipClasses.SkippedClass._setUpRan = False
167
def test_counting(self):
169
Skipped test methods still contribute to the total test count.
174
def test_setUpRan(self):
176
The C{setUp} method is not called if the class is set to skip.
178
self.suite(self.reporter)
179
self.assertFalse(TestSkipClasses.SkippedClass._setUpRan)
182
def test_results(self):
184
Skipped test methods don't cause C{wasSuccessful} to return C{False},
185
nor do they contribute to the C{errors} or C{failures} of the reporter.
186
They do, however, add elements to the reporter's C{skips} list.
188
self.suite(self.reporter)
189
self.failUnless(self.reporter.wasSuccessful())
190
self.failUnlessEqual(self.reporter.errors, [])
191
self.failUnlessEqual(self.reporter.failures, [])
192
self.failUnlessEqual(len(self.reporter.skips), 4)
195
def test_reasons(self):
197
Test methods which raise L{unittest.SkipTest} or have their C{skip}
198
attribute set to something are skipped.
200
self.suite(self.reporter)
201
expectedReasons = ['class', 'skip2', 'class', 'class']
203
reasonsGiven = [reason for test, reason in self.reporter.skips]
204
self.assertEquals(expectedReasons, reasonsGiven)
208
class TestTodo(unittest.TestCase, ResultsTestMixin):
209
class TodoTests(unittest.TestCase):
210
def test_todo1(self):
211
self.fail("deliberate failure")
212
test_todo1.todo = "todo1"
214
def test_todo2(self):
215
raise RuntimeError("deliberate error")
216
test_todo2.todo = "todo2"
218
def test_todo3(self):
219
"""unexpected success"""
220
test_todo3.todo = 'todo3'
223
self.loadSuite(TestTodo.TodoTests)
225
def test_counting(self):
228
def test_results(self):
229
self.suite(self.reporter)
230
self.failUnless(self.reporter.wasSuccessful())
231
self.failUnlessEqual(self.reporter.errors, [])
232
self.failUnlessEqual(self.reporter.failures, [])
233
self.failUnlessEqual(self.reporter.skips, [])
234
self.failUnlessEqual(len(self.reporter.expectedFailures), 2)
235
self.failUnlessEqual(len(self.reporter.unexpectedSuccesses), 1)
237
def test_expectedFailures(self):
238
self.suite(self.reporter)
239
expectedReasons = ['todo1', 'todo2']
240
reasonsGiven = [ r.reason
241
for t, e, r in self.reporter.expectedFailures ]
242
self.failUnlessEqual(expectedReasons, reasonsGiven)
244
def test_unexpectedSuccesses(self):
245
self.suite(self.reporter)
246
expectedReasons = ['todo3']
247
reasonsGiven = [ r.reason
248
for t, r in self.reporter.unexpectedSuccesses ]
249
self.failUnlessEqual(expectedReasons, reasonsGiven)
252
class TestTodoClass(unittest.TestCase, ResultsTestMixin):
253
class TodoClass(unittest.TestCase):
254
def test_todo1(self):
256
test_todo1.todo = "method"
257
def test_todo2(self):
259
def test_todo3(self):
260
self.fail("Deliberate Failure")
261
test_todo3.todo = "method"
262
def test_todo4(self):
263
self.fail("Deliberate Failure")
264
TodoClass.todo = "class"
267
self.loadSuite(TestTodoClass.TodoClass)
269
def test_counting(self):
272
def test_results(self):
273
self.suite(self.reporter)
274
self.failUnless(self.reporter.wasSuccessful())
275
self.failUnlessEqual(self.reporter.errors, [])
276
self.failUnlessEqual(self.reporter.failures, [])
277
self.failUnlessEqual(self.reporter.skips, [])
278
self.failUnlessEqual(len(self.reporter.expectedFailures), 2)
279
self.failUnlessEqual(len(self.reporter.unexpectedSuccesses), 2)
281
def test_expectedFailures(self):
282
self.suite(self.reporter)
283
expectedReasons = ['method', 'class']
284
reasonsGiven = [ r.reason
285
for t, e, r in self.reporter.expectedFailures ]
286
self.failUnlessEqual(expectedReasons, reasonsGiven)
288
def test_unexpectedSuccesses(self):
289
self.suite(self.reporter)
290
expectedReasons = ['method', 'class']
291
reasonsGiven = [ r.reason
292
for t, r in self.reporter.unexpectedSuccesses ]
293
self.failUnlessEqual(expectedReasons, reasonsGiven)
296
class TestStrictTodo(unittest.TestCase, ResultsTestMixin):
297
class Todos(unittest.TestCase):
298
def test_todo1(self):
299
raise RuntimeError, "expected failure"
300
test_todo1.todo = (RuntimeError, "todo1")
302
def test_todo2(self):
303
raise RuntimeError, "expected failure"
304
test_todo2.todo = ((RuntimeError, OSError), "todo2")
306
def test_todo3(self):
307
raise RuntimeError, "we had no idea!"
308
test_todo3.todo = (OSError, "todo3")
310
def test_todo4(self):
311
raise RuntimeError, "we had no idea!"
312
test_todo4.todo = ((OSError, SyntaxError), "todo4")
314
def test_todo5(self):
315
self.fail("deliberate failure")
316
test_todo5.todo = (unittest.FailTest, "todo5")
318
def test_todo6(self):
319
self.fail("deliberate failure")
320
test_todo6.todo = (RuntimeError, "todo6")
322
def test_todo7(self):
324
test_todo7.todo = (RuntimeError, "todo7")
327
self.loadSuite(TestStrictTodo.Todos)
329
def test_counting(self):
332
def test_results(self):
333
self.suite(self.reporter)
334
self.failIf(self.reporter.wasSuccessful())
335
self.failUnlessEqual(len(self.reporter.errors), 2)
336
self.failUnlessEqual(len(self.reporter.failures), 1)
337
self.failUnlessEqual(len(self.reporter.expectedFailures), 3)
338
self.failUnlessEqual(len(self.reporter.unexpectedSuccesses), 1)
339
self.failUnlessEqual(self.reporter.skips, [])
341
def test_expectedFailures(self):
342
self.suite(self.reporter)
343
expectedReasons = ['todo1', 'todo2', 'todo5']
344
reasonsGotten = [ r.reason
345
for t, e, r in self.reporter.expectedFailures ]
346
self.failUnlessEqual(expectedReasons, reasonsGotten)
348
def test_unexpectedSuccesses(self):
349
self.suite(self.reporter)
350
expectedReasons = [([RuntimeError], 'todo7')]
351
reasonsGotten = [ (r.errors, r.reason)
352
for t, r in self.reporter.unexpectedSuccesses ]
353
self.failUnlessEqual(expectedReasons, reasonsGotten)
357
class TestCleanup(unittest.TestCase):
360
self.result = reporter.Reporter(StringIO.StringIO())
361
self.loader = runner.TestLoader()
364
def testLeftoverSockets(self):
366
Trial reports a L{util.DirtyReactorAggregateError} if a test leaves
369
suite = self.loader.loadMethod(
370
erroneous.SocketOpenTest.test_socketsLeftOpen)
371
suite.run(self.result)
372
self.failIf(self.result.wasSuccessful())
373
# socket cleanup happens at end of class's tests.
374
# all the tests in the class are successful, even if the suite
376
self.assertEqual(self.result.successes, 1)
377
failure = self.result.errors[0][1]
378
self.failUnless(failure.check(util.DirtyReactorAggregateError))
381
def testLeftoverPendingCalls(self):
383
Trial reports a L{util.DirtyReactorAggregateError} and fails the test
384
if a test leaves a L{DelayedCall} hanging.
386
suite = erroneous.ReactorCleanupTests('test_leftoverPendingCalls')
387
suite.run(self.result)
388
self.failIf(self.result.wasSuccessful())
389
failure = self.result.errors[0][1]
390
self.assertEqual(self.result.successes, 0)
391
self.failUnless(failure.check(util.DirtyReactorAggregateError))
395
class FixtureTest(unittest.TestCase):
397
Tests for broken fixture helper methods (e.g. setUp, tearDown).
401
self.reporter = reporter.Reporter()
402
self.loader = runner.TestLoader()
405
def testBrokenSetUp(self):
407
When setUp fails, the error is recorded in the result object.
409
self.loader.loadClass(erroneous.TestFailureInSetUp).run(self.reporter)
410
self.assert_(len(self.reporter.errors) > 0)
411
self.assert_(isinstance(self.reporter.errors[0][1].value,
412
erroneous.FoolishError))
415
def testBrokenTearDown(self):
417
When tearDown fails, the error is recorded in the result object.
419
suite = self.loader.loadClass(erroneous.TestFailureInTearDown)
420
suite.run(self.reporter)
421
errors = self.reporter.errors
422
self.assert_(len(errors) > 0)
423
self.assert_(isinstance(errors[0][1].value, erroneous.FoolishError))
427
class SuppressionTest(unittest.TestCase):
429
def runTests(self, suite):
430
suite.run(reporter.TestResult())
434
self.loader = runner.TestLoader()
437
def test_suppressMethod(self):
439
A suppression set on a test method prevents warnings emitted by that
440
test method which the suppression matches from being emitted.
442
self.runTests(self.loader.loadMethod(
443
suppression.TestSuppression.testSuppressMethod))
444
warningsShown = self.flushWarnings([
445
suppression.TestSuppression._emit])
447
warningsShown[0]['message'], suppression.CLASS_WARNING_MSG)
449
warningsShown[1]['message'], suppression.MODULE_WARNING_MSG)
450
self.assertEqual(len(warningsShown), 2)
453
def test_suppressClass(self):
455
A suppression set on a L{TestCase} subclass prevents warnings emitted
456
by any test methods defined on that class which match the suppression
459
self.runTests(self.loader.loadMethod(
460
suppression.TestSuppression.testSuppressClass))
461
warningsShown = self.flushWarnings([
462
suppression.TestSuppression._emit])
464
warningsShown[0]['message'], suppression.METHOD_WARNING_MSG)
466
warningsShown[1]['message'], suppression.MODULE_WARNING_MSG)
467
self.assertEqual(len(warningsShown), 2)
470
def test_suppressModule(self):
472
A suppression set on a module prevents warnings emitted by any test
473
mewthods defined in that module which match the suppression from being
476
self.runTests(self.loader.loadMethod(
477
suppression.TestSuppression2.testSuppressModule))
478
warningsShown = self.flushWarnings([
479
suppression.TestSuppression._emit])
481
warningsShown[0]['message'], suppression.METHOD_WARNING_MSG)
483
warningsShown[1]['message'], suppression.CLASS_WARNING_MSG)
484
self.assertEqual(len(warningsShown), 2)
487
def test_overrideSuppressClass(self):
489
The suppression set on a test method completely overrides a suppression
490
with wider scope; if it does not match a warning emitted by that test
491
method, the warning is emitted, even if a wider suppression matches.
493
case = self.loader.loadMethod(
494
suppression.TestSuppression.testOverrideSuppressClass)
496
warningsShown = self.flushWarnings([
497
suppression.TestSuppression._emit])
499
warningsShown[0]['message'], suppression.METHOD_WARNING_MSG)
501
warningsShown[1]['message'], suppression.CLASS_WARNING_MSG)
503
warningsShown[2]['message'], suppression.MODULE_WARNING_MSG)
504
self.assertEqual(len(warningsShown), 3)
510
I provide a few mock tests that log setUp, tearDown, test execution and
511
garbage collection. I'm used to test whether gc.collect gets called.
514
class BasicTest(unittest.TestCase):
520
self._log('tearDown')
522
class ClassTest(unittest.TestCase):
529
self._collectCalled.append(msg)
532
"""Fake gc.collect"""
536
self._collectCalled = []
537
self.BasicTest._log = self.ClassTest._log = self._log
538
self._oldCollect = gc.collect
539
gc.collect = self.collect
542
gc.collect = self._oldCollect
546
class TestGarbageCollectionDefault(GCMixin, unittest.TestCase):
548
def test_collectNotDefault(self):
550
By default, tests should not force garbage collection.
552
test = self.BasicTest('test_foo')
553
result = reporter.TestResult()
555
self.failUnlessEqual(self._collectCalled, ['setUp', 'test', 'tearDown'])
559
class TestGarbageCollection(GCMixin, unittest.TestCase):
561
def test_collectCalled(self):
563
test gc.collect is called before and after each test.
565
test = TestGarbageCollection.BasicTest('test_foo')
566
test = unittest._ForceGarbageCollectionDecorator(test)
567
result = reporter.TestResult()
569
self.failUnlessEqual(
571
['collect', 'setUp', 'test', 'tearDown', 'collect'])
575
class TestUnhandledDeferred(unittest.TestCase):
578
from twisted.trial.test import weird
579
# test_unhandledDeferred creates a cycle. we need explicit control of gc
581
self.test1 = unittest._ForceGarbageCollectionDecorator(
582
weird.TestBleeding('test_unhandledDeferred'))
584
def test_isReported(self):
586
Forcing garbage collection should cause unhandled Deferreds to be
589
result = reporter.TestResult()
591
self.assertEqual(len(result.errors), 1,
592
'Unhandled deferred passed without notice')
594
def test_doesntBleed(self):
596
Forcing garbage collection in the test should mean that there are
597
no unreachable cycles immediately after the test completes.
599
result = reporter.TestResult()
601
self.flushLoggedErrors() # test1 logs errors that get caught be us.
602
# test1 created unreachable cycle.
603
# it & all others should have been collected by now.
605
self.assertEqual(n, 0, 'unreachable cycle still existed')
606
# check that last gc.collect didn't log more errors
607
x = self.flushLoggedErrors()
608
self.assertEqual(len(x), 0, 'Errors logged after gc.collect')
613
self.flushLoggedErrors()
617
class TestAddCleanup(unittest.TestCase):
619
Test the addCleanup method of TestCase.
622
class MockTest(unittest.TestCase):
627
def brokenSetUp(self):
629
raise RuntimeError("Deliberate failure")
631
def skippingSetUp(self):
633
raise unittest.SkipTest("Don't do this")
635
def append(self, thing):
636
self.log.append(thing)
639
self.log.append('tearDown')
642
self.log.append('runTest')
646
unittest.TestCase.setUp(self)
647
self.result = reporter.TestResult()
648
self.test = TestAddCleanup.MockTest()
651
def test_addCleanupCalledIfSetUpFails(self):
653
Callables added with C{addCleanup} are run even if setUp fails.
655
self.test.setUp = self.test.brokenSetUp
656
self.test.addCleanup(self.test.append, 'foo')
657
self.test.run(self.result)
658
self.assertEqual(['setUp', 'foo'], self.test.log)
661
def test_addCleanupCalledIfSetUpSkips(self):
663
Callables added with C{addCleanup} are run even if setUp raises
664
L{SkipTest}. This allows test authors to reliably provide clean up
665
code using C{addCleanup}.
667
self.test.setUp = self.test.skippingSetUp
668
self.test.addCleanup(self.test.append, 'foo')
669
self.test.run(self.result)
670
self.assertEqual(['setUp', 'foo'], self.test.log)
673
def test_addCleanupCalledInReverseOrder(self):
675
Callables added with C{addCleanup} should be called before C{tearDown}
676
in reverse order of addition.
678
self.test.addCleanup(self.test.append, "foo")
679
self.test.addCleanup(self.test.append, 'bar')
680
self.test.run(self.result)
681
self.assertEqual(['setUp', 'runTest', 'bar', 'foo', 'tearDown'],
685
def test_addCleanupWaitsForDeferreds(self):
687
If an added callable returns a L{Deferred}, then the test should wait
688
until that L{Deferred} has fired before running the next cleanup
691
def cleanup(message):
693
reactor.callLater(0, d.callback, message)
694
return d.addCallback(self.test.append)
695
self.test.addCleanup(self.test.append, 'foo')
696
self.test.addCleanup(cleanup, 'bar')
697
self.test.run(self.result)
698
self.assertEqual(['setUp', 'runTest', 'bar', 'foo', 'tearDown'],
702
def test_errorInCleanupIsCaptured(self):
704
Errors raised in cleanup functions should be treated like errors in
705
C{tearDown}. They should be added as errors and fail the test. Skips,
706
todos and failures are all treated as errors.
708
self.test.addCleanup(self.test.fail, 'foo')
709
self.test.run(self.result)
710
self.failIf(self.result.wasSuccessful())
711
self.assertEqual(1, len(self.result.errors))
712
[(test, error)] = self.result.errors
713
self.assertEqual(test, self.test)
714
self.assertEqual(error.getErrorMessage(), 'foo')
717
def test_cleanupsContinueRunningAfterError(self):
719
If a cleanup raises an error then that does not stop the other
720
cleanups from being run.
722
self.test.addCleanup(self.test.append, 'foo')
723
self.test.addCleanup(self.test.fail, 'bar')
724
self.test.run(self.result)
725
self.assertEqual(['setUp', 'runTest', 'foo', 'tearDown'],
727
self.assertEqual(1, len(self.result.errors))
728
[(test, error)] = self.result.errors
729
self.assertEqual(test, self.test)
730
self.assertEqual(error.getErrorMessage(), 'bar')
733
def test_multipleErrorsReported(self):
735
If more than one cleanup fails, then the test should fail with more
738
self.test.addCleanup(self.test.fail, 'foo')
739
self.test.addCleanup(self.test.fail, 'bar')
740
self.test.run(self.result)
741
self.assertEqual(['setUp', 'runTest', 'tearDown'],
743
self.assertEqual(2, len(self.result.errors))
744
[(test1, error1), (test2, error2)] = self.result.errors
745
self.assertEqual(test1, self.test)
746
self.assertEqual(test2, self.test)
747
self.assertEqual(error1.getErrorMessage(), 'bar')
748
self.assertEqual(error2.getErrorMessage(), 'foo')
752
class TestSuiteClearing(unittest.TestCase):
754
Tests for our extension that allows us to clear out a L{TestSuite}.
758
def test_clearSuite(self):
760
Calling L{unittest._clearSuite} on a populated L{TestSuite} removes
763
suite = unittest.TestSuite()
764
suite.addTest(unittest.TestCase())
765
# Double check that the test suite actually has something in it.
766
self.assertEqual(1, suite.countTestCases())
767
unittest._clearSuite(suite)
768
self.assertEqual(0, suite.countTestCases())
771
def test_clearPyunitSuite(self):
773
Calling L{unittest._clearSuite} on a populated standard library
774
L{TestSuite} removes all tests.
776
This test is important since C{_clearSuite} operates by mutating
779
pyunit = __import__('unittest')
780
suite = pyunit.TestSuite()
781
suite.addTest(unittest.TestCase())
782
# Double check that the test suite actually has something in it.
783
self.assertEqual(1, suite.countTestCases())
784
unittest._clearSuite(suite)
785
self.assertEqual(0, suite.countTestCases())
789
class TestTestDecorator(unittest.TestCase):
791
Tests for our test decoration features.
795
def assertTestsEqual(self, observed, expected):
797
Assert that the given decorated tests are equal.
799
self.assertEqual(observed.__class__, expected.__class__,
801
observedOriginal = getattr(observed, '_originalTest', None)
802
expectedOriginal = getattr(expected, '_originalTest', None)
803
self.assertIdentical(observedOriginal, expectedOriginal)
804
if observedOriginal is expectedOriginal is None:
805
self.assertIdentical(observed, expected)
808
def assertSuitesEqual(self, observed, expected):
810
Assert that the given test suites with decorated tests are equal.
812
self.assertEqual(observed.__class__, expected.__class__,
814
self.assertEqual(len(observed._tests), len(expected._tests),
815
"Different number of tests.")
816
for observedTest, expectedTest in zip(observed._tests,
818
if getattr(observedTest, '_tests', None) is not None:
819
self.assertSuitesEqual(observedTest, expectedTest)
821
self.assertTestsEqual(observedTest, expectedTest)
824
def test_usesAdaptedReporterWithRun(self):
826
For decorated tests, C{run} uses a result adapter that preserves the
827
test decoration for calls to C{addError}, C{startTest} and the like.
829
See L{reporter._AdaptedReporter}.
831
test = unittest.TestCase()
832
decoratedTest = unittest.TestDecorator(test)
833
result = LoggingReporter()
834
decoratedTest.run(result)
835
self.assertTestsEqual(result.test, decoratedTest)
838
def test_usesAdaptedReporterWithCall(self):
840
For decorated tests, C{__call__} uses a result adapter that preserves
841
the test decoration for calls to C{addError}, C{startTest} and the
844
See L{reporter._AdaptedReporter}.
846
test = unittest.TestCase()
847
decoratedTest = unittest.TestDecorator(test)
848
result = LoggingReporter()
849
decoratedTest(result)
850
self.assertTestsEqual(result.test, decoratedTest)
853
def test_decorateSingleTest(self):
855
Calling L{decorate} on a single test case returns the test case
856
decorated with the provided decorator.
858
test = unittest.TestCase()
859
decoratedTest = unittest.decorate(test, unittest.TestDecorator)
860
self.assertTestsEqual(unittest.TestDecorator(test), decoratedTest)
863
def test_decorateTestSuite(self):
865
Calling L{decorate} on a test suite will return a test suite with
866
each test decorated with the provided decorator.
868
test = unittest.TestCase()
869
suite = unittest.TestSuite([test])
870
decoratedTest = unittest.decorate(suite, unittest.TestDecorator)
871
self.assertSuitesEqual(
872
decoratedTest, unittest.TestSuite([unittest.TestDecorator(test)]))
875
def test_decorateInPlaceMutatesOriginal(self):
877
Calling L{decorate} on a test suite will mutate the original suite.
879
test = unittest.TestCase()
880
suite = unittest.TestSuite([test])
881
decoratedTest = unittest.decorate(
882
suite, unittest.TestDecorator)
883
self.assertSuitesEqual(
884
decoratedTest, unittest.TestSuite([unittest.TestDecorator(test)]))
885
self.assertSuitesEqual(
886
suite, unittest.TestSuite([unittest.TestDecorator(test)]))
889
def test_decorateTestSuiteReferences(self):
891
When decorating a test suite in-place, the number of references to the
892
test objects in that test suite should stay the same.
894
Previously, L{unittest.decorate} recreated a test suite, so the
895
original suite kept references to the test objects. This test is here
896
to ensure the problem doesn't reappear again.
898
getrefcount = getattr(sys, 'getrefcount', None)
899
if getrefcount is None:
900
raise unittest.SkipTest(
901
"getrefcount not supported on this platform")
902
test = unittest.TestCase()
903
suite = unittest.TestSuite([test])
904
count1 = getrefcount(test)
905
decoratedTest = unittest.decorate(suite, unittest.TestDecorator)
906
count2 = getrefcount(test)
907
self.assertEquals(count1, count2)
910
def test_decorateNestedTestSuite(self):
912
Calling L{decorate} on a test suite with nested suites will return a
913
test suite that maintains the same structure, but with all tests
916
test = unittest.TestCase()
917
suite = unittest.TestSuite([unittest.TestSuite([test])])
918
decoratedTest = unittest.decorate(suite, unittest.TestDecorator)
919
expected = unittest.TestSuite(
920
[unittest.TestSuite([unittest.TestDecorator(test)])])
921
self.assertSuitesEqual(decoratedTest, expected)
924
def test_decorateDecoratedSuite(self):
926
Calling L{decorate} on a test suite with already-decorated tests
927
decorates all of the tests in the suite again.
929
test = unittest.TestCase()
930
decoratedTest = unittest.decorate(test, unittest.TestDecorator)
931
redecoratedTest = unittest.decorate(decoratedTest,
932
unittest.TestDecorator)
933
self.assertTestsEqual(redecoratedTest,
934
unittest.TestDecorator(decoratedTest))
937
def test_decoratePreservesSuite(self):
939
Tests can be in non-standard suites. L{decorate} preserves the
940
non-standard suites when it decorates the tests.
942
test = unittest.TestCase()
943
suite = runner.DestructiveTestSuite([test])
944
decorated = unittest.decorate(suite, unittest.TestDecorator)
945
self.assertSuitesEqual(
947
runner.DestructiveTestSuite([unittest.TestDecorator(test)]))
950
class TestMonkeyPatchSupport(unittest.TestCase):
952
Tests for the patch() helper method in L{unittest.TestCase}.
957
self.originalValue = 'original'
958
self.patchedValue = 'patched'
959
self.objectToPatch = self.originalValue
960
self.test = unittest.TestCase()
963
def test_patch(self):
965
Calling C{patch()} on a test monkey patches the specified object and
968
self.test.patch(self, 'objectToPatch', self.patchedValue)
969
self.assertEqual(self.objectToPatch, self.patchedValue)
972
def test_patchRestoredAfterRun(self):
974
Any monkey patches introduced by a test using C{patch()} are reverted
975
after the test has run.
977
self.test.patch(self, 'objectToPatch', self.patchedValue)
978
self.test.run(reporter.Reporter())
979
self.assertEqual(self.objectToPatch, self.originalValue)
982
def test_revertDuringTest(self):
984
C{patch()} return a L{monkey.MonkeyPatcher} object that can be used to
985
restore the original values before the end of the test.
987
patch = self.test.patch(self, 'objectToPatch', self.patchedValue)
989
self.assertEqual(self.objectToPatch, self.originalValue)
992
def test_revertAndRepatch(self):
994
The returned L{monkey.MonkeyPatcher} object can re-apply the patch
997
patch = self.test.patch(self, 'objectToPatch', self.patchedValue)
1000
self.assertEqual(self.objectToPatch, self.patchedValue)
1003
def test_successivePatches(self):
1005
Successive patches are applied and reverted just like a single patch.
1007
self.test.patch(self, 'objectToPatch', self.patchedValue)
1008
self.assertEqual(self.objectToPatch, self.patchedValue)
1009
self.test.patch(self, 'objectToPatch', 'second value')
1010
self.assertEqual(self.objectToPatch, 'second value')
1011
self.test.run(reporter.Reporter())
1012
self.assertEqual(self.objectToPatch, self.originalValue)
1016
class TestIterateTests(unittest.TestCase):
1018
L{_iterateTests} returns a list of all test cases in a test suite or test
1022
def test_iterateTestCase(self):
1024
L{_iterateTests} on a single test case returns a list containing that
1027
test = unittest.TestCase()
1028
self.assertEqual([test], list(unittest._iterateTests(test)))
1031
def test_iterateSingletonTestSuite(self):
1033
L{_iterateTests} on a test suite that contains a single test case
1034
returns a list containing that test case.
1036
test = unittest.TestCase()
1037
suite = runner.TestSuite([test])
1038
self.assertEqual([test], list(unittest._iterateTests(suite)))
1041
def test_iterateNestedTestSuite(self):
1043
L{_iterateTests} returns tests that are in nested test suites.
1045
test = unittest.TestCase()
1046
suite = runner.TestSuite([runner.TestSuite([test])])
1047
self.assertEqual([test], list(unittest._iterateTests(suite)))
1050
def test_iterateIsLeftToRightDepthFirst(self):
1052
L{_iterateTests} returns tests in left-to-right, depth-first order.
1054
test = unittest.TestCase()
1055
suite = runner.TestSuite([runner.TestSuite([test]), self])
1056
self.assertEqual([test, self], list(unittest._iterateTests(suite)))