3315
3314
self.assertLength(1, calls)
3318
class TestUncollectedWarnings(tests.TestCase):
3319
"""Check a test case still alive after being run emits a warning"""
3321
class Test(tests.TestCase):
3322
def test_pass(self):
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")
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"),
3336
def _inject_stream_into_subunit(self, stream):
3337
"""To be overridden by subclasses that run tests out of process"""
3339
def _run_selftest_with_suite(self, **kwargs):
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()
3348
tests.selftest(test_suite_factory=self._get_suite, stream=sio,
3349
stop_on_failure=False, **kwargs)
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")
3359
def test_testsuite(self):
3360
self._run_selftest_with_suite()
3362
def test_pattern(self):
3363
out = self._run_selftest_with_suite(pattern="test_(?:pass|self_ref)$")
3364
self.assertNotContainsRe(out, "test_skip")
3366
def test_exclude_pattern(self):
3367
out = self._run_selftest_with_suite(exclude_pattern="test_skip$")
3368
self.assertNotContainsRe(out, "test_skip")
3370
def test_random_seed(self):
3371
self._run_selftest_with_suite(random_seed="now")
3373
def test_matching_tests_first(self):
3374
self._run_selftest_with_suite(matching_tests_first=True,
3375
pattern="test_self_ref$")
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")
3382
def test_additonal_decorator(self):
3383
out = self._run_selftest_with_suite(
3384
suite_decorators=[tests.TestDecorator])
3387
class TestUncollectedWarningsSubunit(TestUncollectedWarnings):
3388
"""Check warnings from tests staying alive are emitted with subunit"""
3390
_test_needs_features = [features.subunit]
3392
def _run_selftest_with_suite(self, **kwargs):
3393
return TestUncollectedWarnings._run_selftest_with_suite(self,
3394
runner_class=tests.SubUnitBzrRunner, **kwargs)
3397
class TestUncollectedWarningsForking(TestUncollectedWarnings):
3398
"""Check warnings from tests staying alive are emitted when forking"""
3400
_test_needs_features = [features.subunit]
3402
def _inject_stream_into_subunit(self, stream):
3403
"""Monkey-patch subunit so the extra output goes to stream not stdout
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.
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)
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)
3425
3317
class TestEnvironHandling(tests.TestCase):
3427
3319
def test_overrideEnv_None_called_twice_doesnt_leak(self):