~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_selftest.py

  • Committer: Vincent Ladeuil
  • Date: 2011-09-27 11:48:50 UTC
  • mto: This revision was merged to the branch mainline in revision 6173.
  • Revision ID: v.ladeuil+lp@free.fr-20110927114850-338r2mns0138klv0
Global options respect their hidden attribute

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
"""Tests for the test framework."""
18
18
 
19
19
from cStringIO import StringIO
20
 
import gc
21
20
import doctest
22
21
import os
23
22
import signal
3315
3314
        self.assertLength(1, calls)
3316
3315
 
3317
3316
 
3318
 
class TestUncollectedWarnings(tests.TestCase):
3319
 
    """Check a test case still alive after being run emits a warning"""
3320
 
 
3321
 
    class Test(tests.TestCase):
3322
 
        def test_pass(self):
3323
 
            pass
3324
 
        def test_self_ref(self):
3325
 
            self.also_self = self.test_self_ref
3326
 
        def test_skip(self):
3327
 
            self.skip("Don't need")
3328
 
 
3329
 
    def _get_suite(self):
3330
 
        return TestUtil.TestSuite([
3331
 
            self.Test("test_pass"),
3332
 
            self.Test("test_self_ref"),
3333
 
            self.Test("test_skip"),
3334
 
            ])
3335
 
 
3336
 
    def _inject_stream_into_subunit(self, stream):
3337
 
        """To be overridden by subclasses that run tests out of process"""
3338
 
 
3339
 
    def _run_selftest_with_suite(self, **kwargs):
3340
 
        sio = StringIO()
3341
 
        self._inject_stream_into_subunit(sio)
3342
 
        old_flags = tests.selftest_debug_flags
3343
 
        tests.selftest_debug_flags = old_flags.union(["uncollected_cases"])
3344
 
        gc_on = gc.isenabled()
3345
 
        if gc_on:
3346
 
            gc.disable()
3347
 
        try:
3348
 
            tests.selftest(test_suite_factory=self._get_suite, stream=sio,
3349
 
                stop_on_failure=False, **kwargs)
3350
 
        finally:
3351
 
            if gc_on:
3352
 
                gc.enable()
3353
 
            tests.selftest_debug_flags = old_flags
3354
 
        output = sio.getvalue()
3355
 
        self.assertNotContainsRe(output, "Uncollected test case.*test_pass")
3356
 
        self.assertContainsRe(output, "Uncollected test case.*test_self_ref")
3357
 
        return output
3358
 
 
3359
 
    def test_testsuite(self):
3360
 
        self._run_selftest_with_suite()
3361
 
 
3362
 
    def test_pattern(self):
3363
 
        out = self._run_selftest_with_suite(pattern="test_(?:pass|self_ref)$")
3364
 
        self.assertNotContainsRe(out, "test_skip")
3365
 
 
3366
 
    def test_exclude_pattern(self):
3367
 
        out = self._run_selftest_with_suite(exclude_pattern="test_skip$")
3368
 
        self.assertNotContainsRe(out, "test_skip")
3369
 
 
3370
 
    def test_random_seed(self):
3371
 
        self._run_selftest_with_suite(random_seed="now")
3372
 
 
3373
 
    def test_matching_tests_first(self):
3374
 
        self._run_selftest_with_suite(matching_tests_first=True,
3375
 
            pattern="test_self_ref$")
3376
 
 
3377
 
    def test_starting_with_and_exclude(self):
3378
 
        out = self._run_selftest_with_suite(starting_with=["bt."],
3379
 
            exclude_pattern="test_skip$")
3380
 
        self.assertNotContainsRe(out, "test_skip")
3381
 
 
3382
 
    def test_additonal_decorator(self):
3383
 
        out = self._run_selftest_with_suite(
3384
 
            suite_decorators=[tests.TestDecorator])
3385
 
 
3386
 
 
3387
 
class TestUncollectedWarningsSubunit(TestUncollectedWarnings):
3388
 
    """Check warnings from tests staying alive are emitted with subunit"""
3389
 
 
3390
 
    _test_needs_features = [features.subunit]
3391
 
 
3392
 
    def _run_selftest_with_suite(self, **kwargs):
3393
 
        return TestUncollectedWarnings._run_selftest_with_suite(self,
3394
 
            runner_class=tests.SubUnitBzrRunner, **kwargs)
3395
 
 
3396
 
 
3397
 
class TestUncollectedWarningsForking(TestUncollectedWarnings):
3398
 
    """Check warnings from tests staying alive are emitted when forking"""
3399
 
 
3400
 
    _test_needs_features = [features.subunit]
3401
 
 
3402
 
    def _inject_stream_into_subunit(self, stream):
3403
 
        """Monkey-patch subunit so the extra output goes to stream not stdout
3404
 
 
3405
 
        Some APIs need rewriting so this kind of bogus hackery can be replaced
3406
 
        by passing the stream param from run_tests down into ProtocolTestCase.
3407
 
        """
3408
 
        from subunit import ProtocolTestCase
3409
 
        _original_init = ProtocolTestCase.__init__
3410
 
        def _init_with_passthrough(self, *args, **kwargs):
3411
 
            _original_init(self, *args, **kwargs)
3412
 
            self._passthrough = stream
3413
 
        self.overrideAttr(ProtocolTestCase, "__init__", _init_with_passthrough)
3414
 
 
3415
 
    def _run_selftest_with_suite(self, **kwargs):
3416
 
        # GZ 2011-05-26: Add a PosixSystem feature so this check can go away
3417
 
        if getattr(os, "fork", None) is None:
3418
 
            raise tests.TestNotApplicable("Platform doesn't support forking")
3419
 
        # Make sure the fork code is actually invoked by claiming two cores
3420
 
        self.overrideAttr(osutils, "local_concurrency", lambda: 2)
3421
 
        kwargs.setdefault("suite_decorators", []).append(tests.fork_decorator)
3422
 
        return TestUncollectedWarnings._run_selftest_with_suite(self, **kwargs)
3423
 
 
3424
 
 
3425
3317
class TestEnvironHandling(tests.TestCase):
3426
3318
 
3427
3319
    def test_overrideEnv_None_called_twice_doesnt_leak(self):