721
def _report_leaked_threads():
722
bzrlib.trace.warning('%s is leaking threads among %d leaking tests',
723
TestCase._first_thread_leaker_id,
724
TestCase._leaking_threads_tests)
723
727
class TestCase(unittest.TestCase):
724
728
"""Base class for bzr unit tests.
741
745
accidentally overlooked.
748
_active_threads = None
749
_leaking_threads_tests = 0
750
_first_thread_leaker_id = None
744
751
_log_file_name = None
745
752
_log_contents = ''
746
753
_keep_log_file = False
747
754
# record lsprof data when performing benchmark calls.
748
755
_gather_lsprof_in_benchmarks = False
749
attrs_to_keep = ('_testMethodName', '_testMethodDoc',
756
attrs_to_keep = ('id', '_testMethodName', '_testMethodDoc',
750
757
'_log_contents', '_log_file_name', '_benchtime',
751
758
'_TestCase__testMethodName')
763
770
self._benchtime = None
764
771
self._clear_hooks()
765
772
self._clear_debug_flags()
773
TestCase._active_threads = threading.activeCount()
774
self.addCleanup(self._check_leaked_threads)
776
def _check_leaked_threads(self):
777
active = threading.activeCount()
778
leaked_threads = active - TestCase._active_threads
779
TestCase._active_threads = active
781
TestCase._leaking_threads_tests += 1
782
if TestCase._first_thread_leaker_id is None:
783
TestCase._first_thread_leaker_id = self.id()
784
# we're not specifically told when all tests are finished.
785
# This will do. We use a function to avoid keeping a reference
786
# to a TestCase object.
787
atexit.register(_report_leaked_threads)
767
789
def _clear_debug_flags(self):
768
790
"""Prevent externally set debug flags affecting tests.
770
792
Tests that want to use debug flags can just set them in the
771
793
debug_flags set during setup/teardown.
773
if 'selftest_debug' not in debug.debug_flags:
795
if 'allow_debug' not in selftest_debug_flags:
774
796
self._preserved_debug_flags = set(debug.debug_flags)
775
797
debug.debug_flags.clear()
776
798
self.addCleanup(self._restore_debug_flags)
1060
1082
To test that a deprecated method raises an error, do something like
1063
self.assertRaises(errors.ReservedId,
1064
self.applyDeprecated, zero_ninetyone,
1065
br.append_revision, 'current:')
1085
self.assertRaises(errors.ReservedId,
1086
self.applyDeprecated,
1087
deprecated_in((1, 5, 0)),
1067
1091
:param deprecation_format: The deprecation format that the callable
1068
1092
should have been deprecated with. This is the same type as the
1501
1525
elif isinstance(args[0], basestring):
1502
1526
args = list(shlex.split(args[0]))
1504
symbol_versioning.warn(zero_ninetyone %
1505
"passing varargs to run_bzr_subprocess",
1506
DeprecationWarning, stacklevel=3)
1528
raise ValueError("passing varargs to run_bzr_subprocess")
1507
1529
process = self.start_bzr_subprocess(args, env_changes=env_changes,
1508
1530
working_dir=working_dir,
1509
1531
allow_plugins=allow_plugins)
2279
2301
return TestUtil.TestSuite(result)
2282
def filter_suite_by_re(suite, pattern, exclude_pattern=DEPRECATED_PARAMETER,
2283
random_order=DEPRECATED_PARAMETER):
2304
def filter_suite_by_re(suite, pattern):
2284
2305
"""Create a test suite by filtering another one.
2286
2307
:param suite: the source suite
2287
2308
:param pattern: pattern that names must match
2288
:param exclude_pattern: A pattern that names must not match. This parameter
2289
is deprecated as of bzrlib 1.0. Please use the separate function
2290
exclude_tests_by_re instead.
2291
:param random_order: If True, tests in the new suite will be put in
2292
random order. This parameter is deprecated as of bzrlib 1.0. Please
2293
use the separate function randomize_suite instead.
2294
2309
:returns: the newly created suite
2296
if deprecated_passed(exclude_pattern):
2297
symbol_versioning.warn(
2298
one_zero % "passing exclude_pattern to filter_suite_by_re",
2299
DeprecationWarning, stacklevel=2)
2300
if exclude_pattern is not None:
2301
suite = exclude_tests_by_re(suite, exclude_pattern)
2302
2311
condition = condition_id_re(pattern)
2303
2312
result_suite = filter_suite_by_condition(suite, condition)
2304
if deprecated_passed(random_order):
2305
symbol_versioning.warn(
2306
one_zero % "passing random_order to filter_suite_by_re",
2307
DeprecationWarning, stacklevel=2)
2309
result_suite = randomize_suite(result_suite)
2310
2313
return result_suite
2356
2359
return TestUtil.TestSuite(tests)
2359
@deprecated_function(one_zero)
2360
def sort_suite_by_re(suite, pattern, exclude_pattern=None,
2361
random_order=False, append_rest=True):
2362
"""DEPRECATED: Create a test suite by sorting another one.
2364
This method has been decomposed into separate helper methods that should be
2366
- filter_suite_by_re
2367
- exclude_tests_by_re
2371
:param suite: the source suite
2372
:param pattern: pattern that names must match in order to go
2373
first in the new suite
2374
:param exclude_pattern: pattern that names must not match, if any
2375
:param random_order: if True, tests in the new suite will be put in
2376
random order (with all tests matching pattern
2378
:param append_rest: if False, pattern is a strict filter and not
2379
just an ordering directive
2380
:returns: the newly created suite
2382
if exclude_pattern is not None:
2383
suite = exclude_tests_by_re(suite, exclude_pattern)
2385
order_changer = randomize_suite
2387
order_changer = preserve_input
2389
suites = map(order_changer, split_suite_by_re(suite, pattern))
2390
return TestUtil.TestSuite(suites)
2392
return order_changer(filter_suite_by_re(suite, pattern))
2395
2362
def split_suite_by_re(suite, pattern):
2396
2363
"""Split a test suite into two by a regular expression.
2474
2441
return result.wasSuccessful()
2444
# Controlled by "bzr selftest -E=..." option
2445
selftest_debug_flags = set()
2477
2448
def selftest(verbose=False, pattern=".*", stop_on_failure=True,
2478
2449
transport=None,
2479
2450
test_suite_factory=None,
2498
2470
transport = default_transport
2499
2471
old_transport = default_transport
2500
2472
default_transport = transport
2473
global selftest_debug_flags
2474
old_debug_flags = selftest_debug_flags
2475
if debug_flags is not None:
2476
selftest_debug_flags = set(debug_flags)
2502
2478
if load_list is None:
2503
2479
keep_only = None