2231
2237
self.transport_readonly_server = HttpServer
2234
def filter_suite_by_re(suite, pattern, exclude_pattern=None,
2235
random_order=False):
2240
def condition_id_re(pattern):
2241
"""Create a condition filter which performs a re check on a test's id.
2243
:param pattern: A regular expression string.
2244
:return: A callable that returns True if the re matches.
2246
filter_re = re.compile(pattern)
2247
def condition(test):
2249
return filter_re.search(test_id)
2253
def condition_isinstance(klass_or_klass_list):
2254
"""Create a condition filter which returns isinstance(param, klass).
2256
:return: A callable which when called with one parameter obj return the
2257
result of isinstance(obj, klass_or_klass_list).
2260
return isinstance(obj, klass_or_klass_list)
2264
def exclude_tests_by_condition(suite, condition):
2265
"""Create a test suite which excludes some tests from suite.
2267
:param suite: The suite to get tests from.
2268
:param condition: A callable whose result evaluates True when called with a
2269
test case which should be excluded from the result.
2270
:return: A suite which contains the tests found in suite that fail
2274
for test in iter_suite_tests(suite):
2275
if not condition(test):
2277
return TestUtil.TestSuite(result)
2280
def filter_suite_by_condition(suite, condition):
2281
"""Create a test suite by filtering another one.
2283
:param suite: The source suite.
2284
:param condition: A callable whose result evaluates True when called with a
2285
test case which should be included in the result.
2286
:return: A suite which contains the tests found in suite that pass
2290
for test in iter_suite_tests(suite):
2293
return TestUtil.TestSuite(result)
2296
def filter_suite_by_re(suite, pattern, exclude_pattern=DEPRECATED_PARAMETER,
2297
random_order=DEPRECATED_PARAMETER):
2236
2298
"""Create a test suite by filtering another one.
2238
2300
:param suite: the source suite
2239
2301
:param pattern: pattern that names must match
2240
:param exclude_pattern: pattern that names must not match, if any
2241
:param random_order: if True, tests in the new suite will be put in
2302
:param exclude_pattern: A pattern that names must not match. This parameter
2303
is deprecated as of bzrlib 1.0. Please use the separate function
2304
exclude_tests_by_re instead.
2305
:param random_order: If True, tests in the new suite will be put in
2306
random order. This parameter is deprecated as of bzrlib 1.0. Please
2307
use the separate function randomize_suite instead.
2243
2308
:returns: the newly created suite
2245
return sort_suite_by_re(suite, pattern, exclude_pattern,
2246
random_order, False)
2310
if deprecated_passed(exclude_pattern):
2311
symbol_versioning.warn(
2312
one_zero % "passing exclude_pattern to filter_suite_by_re",
2313
DeprecationWarning, stacklevel=2)
2314
if exclude_pattern is not None:
2315
suite = exclude_tests_by_re(suite, exclude_pattern)
2316
condition = condition_id_re(pattern)
2317
result_suite = filter_suite_by_condition(suite, condition)
2318
if deprecated_passed(random_order):
2319
symbol_versioning.warn(
2320
one_zero % "passing random_order to filter_suite_by_re",
2321
DeprecationWarning, stacklevel=2)
2323
result_suite = randomize_suite(result_suite)
2327
def exclude_tests_by_re(suite, pattern):
2328
"""Create a test suite which excludes some tests from suite.
2330
:param suite: The suite to get tests from.
2331
:param pattern: A regular expression string. Test ids that match this
2332
pattern will be excluded from the result.
2333
:return: A TestSuite that contains all the tests from suite without the
2334
tests that matched pattern. The order of tests is the same as it was in
2337
return exclude_tests_by_condition(suite, condition_id_re(pattern))
2340
def preserve_input(something):
2341
"""A helper for performing test suite transformation chains.
2343
:param something: Anything you want to preserve.
2349
def randomize_suite(suite):
2350
"""Return a new TestSuite with suite's tests in random order.
2352
The tests in the input suite are flattened into a single suite in order to
2353
accomplish this. Any nested TestSuites are removed to provide global
2356
tests = list(iter_suite_tests(suite))
2357
random.shuffle(tests)
2358
return TestUtil.TestSuite(tests)
2361
@deprecated_function(one_zero)
2249
2362
def sort_suite_by_re(suite, pattern, exclude_pattern=None,
2250
2363
random_order=False, append_rest=True):
2251
"""Create a test suite by sorting another one.
2364
"""DEPRECATED: Create a test suite by sorting another one.
2366
This method has been decomposed into separate helper methods that should be
2368
- filter_suite_by_re
2369
- exclude_tests_by_re
2253
2373
:param suite: the source suite
2254
2374
:param pattern: pattern that names must match in order to go
2255
2375
first in the new suite
2256
2376
:param exclude_pattern: pattern that names must not match, if any
2257
2377
:param random_order: if True, tests in the new suite will be put in
2378
random order (with all tests matching pattern
2259
2380
:param append_rest: if False, pattern is a strict filter and not
2260
2381
just an ordering directive
2261
2382
:returns: the newly created suite
2384
if exclude_pattern is not None:
2385
suite = exclude_tests_by_re(suite, exclude_pattern)
2387
order_changer = randomize_suite
2389
order_changer = preserve_input
2391
suites = map(order_changer, split_suite_by_re(suite, pattern))
2392
return TestUtil.TestSuite(suites)
2394
return order_changer(filter_suite_by_re(suite, pattern))
2397
def split_suite_by_re(suite, pattern):
2398
"""Split a test suite into two by a regular expression.
2400
:param suite: The suite to split.
2401
:param pattern: A regular expression string. Test ids that match this
2402
pattern will be in the first test suite returned, and the others in the
2403
second test suite returned.
2404
:return: A tuple of two test suites, where the first contains tests from
2405
suite matching pattern, and the second contains the remainder from
2406
suite. The order within each output suite is the same as it was in
2265
2411
filter_re = re.compile(pattern)
2266
if exclude_pattern is not None:
2267
exclude_re = re.compile(exclude_pattern)
2268
2412
for test in iter_suite_tests(suite):
2269
2413
test_id = test.id()
2270
if exclude_pattern is None or not exclude_re.search(test_id):
2271
if filter_re.search(test_id):
2276
random.shuffle(first)
2277
random.shuffle(second)
2278
return TestUtil.TestSuite(first + second)
2414
if filter_re.search(test_id):
2415
matched.append(test)
2417
did_not_match.append(test)
2418
return TestUtil.TestSuite(matched), TestUtil.TestSuite(did_not_match)
2281
2421
def run_suite(suite, name='test', verbose=False, pattern=".*",
2317
2458
random.seed(random_seed)
2318
2459
# Customise the list of tests if requested
2319
if pattern != '.*' or exclude_pattern is not None or random_order:
2460
if exclude_pattern is not None:
2461
suite = exclude_tests_by_re(suite, exclude_pattern)
2463
order_changer = randomize_suite
2465
order_changer = preserve_input
2466
if pattern != '.*' or random_order:
2320
2467
if matching_tests_first:
2321
suite = sort_suite_by_re(suite, pattern, exclude_pattern,
2468
suites = map(order_changer, split_suite_by_re(suite, pattern))
2469
suite = TestUtil.TestSuite(suites)
2324
suite = filter_suite_by_re(suite, pattern, exclude_pattern,
2471
suite = order_changer(filter_suite_by_re(suite, pattern))
2473
# Activate code coverage.
2474
if coverage_dir is not None:
2475
tracer = trace.Trace(count=1, trace=0)
2476
sys.settrace(tracer.globaltrace)
2326
2478
result = runner.run(suite)
2480
if coverage_dir is not None:
2482
results = tracer.results()
2483
results.write_results(show_missing=1, summary=False,
2484
coverdir=coverage_dir)
2329
2487
return result.wasStrictlySuccessful()