311
313
self._formatTime(benchmark_time),
313
315
self.report_success(test)
316
self._cleanupLogFile(test)
314
317
unittest.TestResult.addSuccess(self, test)
318
test._log_contents = ''
316
320
def _testConcluded(self, test):
317
321
"""Common code when a test has finished.
319
323
Called regardless of whether it succeded, failed, etc.
321
self._cleanupLogFile(test)
323
327
def _addKnownFailure(self, test, err):
324
328
self.known_failure_count += 1
1040
1048
self.fail('Unexpected success. Should have failed: %s' % reason)
1050
def assertFileEqual(self, content, path):
1051
"""Fail if path does not contain 'content'."""
1052
self.failUnlessExists(path)
1053
f = file(path, 'rb')
1058
self.assertEqualDiff(content, s)
1060
def failUnlessExists(self, path):
1061
"""Fail unless path or paths, which may be abs or relative, exist."""
1062
if not isinstance(path, basestring):
1064
self.failUnlessExists(p)
1066
self.failUnless(osutils.lexists(path),path+" does not exist")
1068
def failIfExists(self, path):
1069
"""Fail if path or paths, which may be abs or relative, exist."""
1070
if not isinstance(path, basestring):
1072
self.failIfExists(p)
1074
self.failIf(osutils.lexists(path),path+" exists")
1042
1076
def _capture_deprecation_warnings(self, a_callable, *args, **kwargs):
1043
1077
"""A helper for callDeprecated and applyDeprecated.
2084
2130
def build_tree_contents(self, shape):
2085
2131
build_tree_contents(shape)
2087
def assertFileEqual(self, content, path):
2088
"""Fail if path does not contain 'content'."""
2089
self.failUnlessExists(path)
2090
f = file(path, 'rb')
2095
self.assertEqualDiff(content, s)
2097
def failUnlessExists(self, path):
2098
"""Fail unless path or paths, which may be abs or relative, exist."""
2099
if not isinstance(path, basestring):
2101
self.failUnlessExists(p)
2103
self.failUnless(osutils.lexists(path),path+" does not exist")
2105
def failIfExists(self, path):
2106
"""Fail if path or paths, which may be abs or relative, exist."""
2107
if not isinstance(path, basestring):
2109
self.failIfExists(p)
2111
self.failIf(osutils.lexists(path),path+" exists")
2113
2133
def assertInWorkingTree(self, path, root_path='.', tree=None):
2114
2134
"""Assert whether path or paths are in the WorkingTree"""
2115
2135
if tree is None:
2527
2561
list_only=list_only,
2528
2562
random_seed=random_seed,
2529
2563
exclude_pattern=exclude_pattern,
2531
coverage_dir=coverage_dir)
2533
2566
default_transport = old_transport
2569
def load_test_id_list(file_name):
2570
"""Load a test id list from a text file.
2572
The format is one test id by line. No special care is taken to impose
2573
strict rules, these test ids are used to filter the test suite so a test id
2574
that do not match an existing test will do no harm. This allows user to add
2575
comments, leave blank lines, etc.
2579
ftest = open(file_name, 'rt')
2581
if e.errno != errno.ENOENT:
2584
raise errors.NoSuchFile(file_name)
2586
for test_name in ftest.readlines():
2587
test_list.append(test_name.strip())
2592
class TestIdList(object):
2593
"""Test id list to filter a test suite.
2595
Relying on the assumption that test ids are built as:
2596
<module>[.<class>.<method>][(<param>+)], <module> being in python dotted
2597
notation, this class offers methods to :
2598
- avoid building a test suite for modules not refered to in the test list,
2599
- keep only the tests listed from the module test suite.
2602
def __init__(self, test_id_list):
2603
# When a test suite needs to be filtered against us we compare test ids
2604
# for equality, so a simple dict offers a quick and simple solution.
2605
self.tests = dict().fromkeys(test_id_list, True)
2607
# While unittest.TestCase have ids like:
2608
# <module>.<class>.<method>[(<param+)],
2609
# doctest.DocTestCase can have ids like:
2612
# <module>.<function>
2613
# <module>.<class>.<method>
2615
# Since we can't predict a test class from its name only, we settle on
2616
# a simple constraint: a test id always begins with its module name.
2619
for test_id in test_id_list:
2620
parts = test_id.split('.')
2621
mod_name = parts.pop(0)
2622
modules[mod_name] = True
2624
mod_name += '.' + part
2625
modules[mod_name] = True
2626
self.modules = modules
2628
def is_module_name_used(self, module_name):
2629
"""Is there tests for the module or one of its sub modules."""
2630
return self.modules.has_key(module_name)
2632
def test_in(self, test_id):
2633
return self.tests.has_key(test_id)
2636
def test_suite(keep_only=None):
2537
2637
"""Build and return TestSuite for the whole of bzrlib.
2639
:param keep_only: A list of test ids limiting the suite returned.
2539
2641
This function can be replaced if you need to change the default test
2540
2642
suite on a global basis, but it is not encouraged.
2675
2779
suite = TestUtil.TestSuite()
2676
2780
loader = TestUtil.TestLoader()
2677
suite.addTest(loader.loadTestsFromModuleNames(testmod_names))
2782
if keep_only is not None:
2783
id_filter = TestIdList(keep_only)
2785
# modules building their suite with loadTestsFromModuleNames
2786
if keep_only is None:
2787
suite.addTest(loader.loadTestsFromModuleNames(testmod_names))
2789
for mod in [m for m in testmod_names
2790
if id_filter.is_module_name_used(m)]:
2791
mod_suite = loader.loadTestsFromModuleNames([mod])
2792
mod_suite = filter_suite_by_id_list(mod_suite, id_filter)
2793
suite.addTest(mod_suite)
2795
# modules adapted for transport implementations
2678
2796
from bzrlib.tests.test_transport_implementations import TransportTestProviderAdapter
2679
2797
adapter = TransportTestProviderAdapter()
2680
adapt_modules(test_transport_implementations, adapter, loader, suite)
2681
for package in packages_to_test():
2682
suite.addTest(package.test_suite())
2683
for m in MODULES_TO_TEST:
2684
suite.addTest(loader.loadTestsFromModule(m))
2685
for m in MODULES_TO_DOCTEST:
2798
if keep_only is None:
2799
adapt_modules(test_transport_implementations, adapter, loader, suite)
2801
for mod in [m for m in test_transport_implementations
2802
if id_filter.is_module_name_used(m)]:
2803
mod_suite = TestUtil.TestSuite()
2804
adapt_modules([mod], adapter, loader, mod_suite)
2805
mod_suite = filter_suite_by_id_list(mod_suite, id_filter)
2806
suite.addTest(mod_suite)
2808
# modules defining their own test_suite()
2809
for package in [p for p in packages_to_test()
2810
if (keep_only is None
2811
or id_filter.is_module_name_used(p.__name__))]:
2812
pack_suite = package.test_suite()
2813
if keep_only is not None:
2814
pack_suite = filter_suite_by_id_list(pack_suite, id_filter)
2815
suite.addTest(pack_suite)
2817
# XXX: MODULES_TO_TEST should be obsoleted ?
2818
for mod in [m for m in MODULES_TO_TEST
2819
if keep_only is None or id_filter.is_module_name_used(m)]:
2820
mod_suite = loader.loadTestsFromModule(mod)
2821
if keep_only is not None:
2822
mod_suite = filter_suite_by_id_list(mod_suite, id_filter)
2823
suite.addTest(mod_suite)
2825
for mod in MODULES_TO_DOCTEST:
2687
suite.addTest(doctest.DocTestSuite(m))
2827
doc_suite = doctest.DocTestSuite(mod)
2688
2828
except ValueError, e:
2689
print '**failed to get doctest for: %s\n%s' %(m,e)
2829
print '**failed to get doctest for: %s\n%s' % (mod, e)
2831
if keep_only is not None:
2832
# DocTests may use ids which doesn't contain the module name
2833
doc_suite = filter_suite_by_id_list(doc_suite, id_filter)
2834
suite.addTest(doc_suite)
2691
2836
default_encoding = sys.getdefaultencoding()
2692
2837
for name, plugin in bzrlib.plugin.plugins().items():
2694
plugin_suite = plugin.test_suite()
2695
except ImportError, e:
2696
bzrlib.trace.warning(
2697
'Unable to test plugin "%s": %s', name, e)
2699
if plugin_suite is not None:
2700
suite.addTest(plugin_suite)
2838
if keep_only is not None:
2839
if not id_filter.is_module_name_used(plugin.module.__name__):
2841
plugin_suite = plugin.test_suite()
2842
# We used to catch ImportError here and turn it into just a warning,
2843
# but really if you don't have --no-plugins this should be a failure.
2844
# mbp 20080213 - see http://bugs.launchpad.net/bugs/189771
2845
if plugin_suite is not None:
2846
if keep_only is not None:
2847
plugin_suite = filter_suite_by_id_list(plugin_suite,
2849
suite.addTest(plugin_suite)
2701
2850
if default_encoding != sys.getdefaultencoding():
2702
2851
bzrlib.trace.warning(
2703
2852
'Plugin "%s" tried to reset default encoding to: %s', name,