~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_selftest.py

  • Committer: Robert Collins
  • Date: 2006-05-09 10:33:08 UTC
  • mto: (1704.1.1 integration)
  • mto: This revision was merged to the branch mainline in revision 1706.
  • Revision ID: robertc@robertcollins.net-20060509103308-0e6bf1a36cb93d51
Test and correct the problem with nested test logs breaking further in-test logs.

Show diffs side-by-side

added added

removed removed

Lines of Context:
13
13
# along with this program; if not, write to the Free Software
14
14
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
15
15
 
16
 
"""Tests for the test framework
17
 
"""
 
16
"""Tests for the test framework."""
18
17
 
19
18
import os
20
19
import sys
68
67
        self.failUnlessExists(filename)
69
68
 
70
69
 
71
 
class TestSkippedTest(TestCase):
72
 
    """Try running a test which is skipped, make sure it's reported properly."""
73
 
 
74
 
    def test_skipped_test(self):
75
 
        # skipping_test must be hidden in here so it's not run as a real test
76
 
        def skipping_test():
77
 
            raise TestSkipped('test intentionally skipped')
78
 
        runner = TextTestRunner(stream=self._log_file, keep_output=True)
79
 
        test = unittest.FunctionTestCase(skipping_test)
80
 
        old_root = TestCaseInTempDir.TEST_ROOT
81
 
        try:
82
 
            TestCaseInTempDir.TEST_ROOT = None
83
 
            result = runner.run(test)
84
 
        finally:
85
 
            TestCaseInTempDir.TEST_ROOT = old_root
86
 
        self.assertTrue(result.wasSuccessful())
87
 
 
88
 
 
89
70
class TestTransportProviderAdapter(TestCase):
90
71
    """A group of tests that test the transport implementation adaption core.
91
72
 
465
446
    def dummy_test(self):
466
447
        pass
467
448
 
 
449
    def run_test_runner(self, testrunner, test):
 
450
        """Run suite in testrunner, saving global state and restoring it.
 
451
 
 
452
        This current saves and restores:
 
453
        TestCaseInTempDir.TEST_ROOT
 
454
        
 
455
        There should be no tests in this file that use bzrlib.tests.TextTestRunner
 
456
        without using this convenience method, because of our use of global state.
 
457
        """
 
458
        old_root = TestCaseInTempDir.TEST_ROOT
 
459
        try:
 
460
            TestCaseInTempDir.TEST_ROOT = None
 
461
            return testrunner.run(test)
 
462
        finally:
 
463
            TestCaseInTempDir.TEST_ROOT = old_root
 
464
 
468
465
    def test_accepts_and_uses_pb_parameter(self):
469
466
        test = TestRunner('dummy_test')
470
467
        mypb = MockProgress()
471
468
        self.assertEqual([], mypb.calls)
472
 
        runner = TextTestRunner(stream=self._log_file, pb=mypb, keep_output=True)
473
 
        result = runner.run(test)
 
469
        runner = TextTestRunner(stream=self._log_file, pb=mypb)
 
470
        result = self.run_test_runner(runner, test)
474
471
        self.assertEqual(1, result.testsRun)
475
472
        self.assertEqual(('update', 'Running tests', 0, 1), mypb.calls[0])
476
473
        self.assertEqual(('update', '...dummy_test', 0, None), mypb.calls[1])
479
476
        self.assertEqual(('clear',), mypb.calls[4])
480
477
        self.assertEqual(5, len(mypb.calls))
481
478
 
 
479
    def test_skipped_test(self):
 
480
        # run a test that is skipped, and check the suite as a whole still
 
481
        # succeeds.
 
482
        # skipping_test must be hidden in here so it's not run as a real test
 
483
        def skipping_test():
 
484
            raise TestSkipped('test intentionally skipped')
 
485
        runner = TextTestRunner(stream=self._log_file, keep_output=True)
 
486
        test = unittest.FunctionTestCase(skipping_test)
 
487
        result = self.run_test_runner(runner, test)
 
488
        self.assertTrue(result.wasSuccessful())
 
489
 
 
490
 
 
491
class TestTestCase(TestCase):
 
492
    """Tests that test the core bzrlib TestCase."""
 
493
 
 
494
    def inner_test(self):
 
495
        # the inner child test
 
496
        note("inner_test")
 
497
 
 
498
    def outer_child(self):
 
499
        # the outer child test
 
500
        note("outer_start")
 
501
        self.inner_test = TestTestCase("inner_child")
 
502
        result = bzrlib.tests._MyResult(self._log_file,
 
503
                                        descriptions=0,
 
504
                                        verbosity=1)
 
505
        self.inner_test.run(result)
 
506
        note("outer finish")
 
507
 
 
508
    def test_trace_nesting(self):
 
509
        # this tests that each test case nests its trace facility correctly.
 
510
        # we do this by running a test case manually. That test case (A)
 
511
        # should setup a new log, log content to it, setup a child case (B),
 
512
        # which should log independently, then case (A) should log a trailer
 
513
        # and return.
 
514
        # we do two nested children so that we can verify the state of the 
 
515
        # logs after the outer child finishes is correct, which a bad clean
 
516
        # up routine in tearDown might trigger a fault in our test with only
 
517
        # one child, we should instead see the bad result inside our test with
 
518
        # the two children.
 
519
        # the outer child test
 
520
        original_trace = bzrlib.trace._trace_file
 
521
        outer_test = TestTestCase("outer_child")
 
522
        result = bzrlib.tests._MyResult(self._log_file,
 
523
                                        descriptions=0,
 
524
                                        verbosity=1)
 
525
        outer_test.run(result)
 
526
        self.assertEqual(original_trace, bzrlib.trace._trace_file)
 
527
        
482
528
 
483
529
class TestExtraAssertions(TestCase):
484
530
    """Tests for new test assertions in bzrlib test suite"""