314
314
self.report_success(test)
315
315
self._cleanupLogFile(test)
316
316
unittest.TestResult.addSuccess(self, test)
317
test._log_contents = ''
318
319
def _testConcluded(self, test):
319
320
"""Common code when a test has finished.
356
357
# seems best to treat this as success from point-of-view of unittest
357
358
# -- it actually does nothing so it barely matters :)
358
359
unittest.TestResult.addSuccess(self, test)
360
test._log_contents = ''
360
362
def printErrorList(self, flavour, errors):
361
363
for test, err in errors:
789
791
_keep_log_file = False
790
792
# record lsprof data when performing benchmark calls.
791
793
_gather_lsprof_in_benchmarks = False
794
attrs_to_keep = ('_testMethodName', '_testMethodDoc',
795
'_log_contents', '_log_file_name', '_benchtime',
796
'_TestCase__testMethodName')
793
798
def __init__(self, methodName='testMethod'):
794
799
super(TestCase, self).__init__(methodName)
810
815
Tests that want to use debug flags can just set them in the
811
816
debug_flags set during setup/teardown.
813
self._preserved_debug_flags = set(debug.debug_flags)
814
debug.debug_flags.clear()
815
self.addCleanup(self._restore_debug_flags)
818
if 'selftest_debug' not in debug.debug_flags:
819
self._preserved_debug_flags = set(debug.debug_flags)
820
debug.debug_flags.clear()
821
self.addCleanup(self._restore_debug_flags)
817
823
def _clear_hooks(self):
818
824
# prevent hooks affecting tests
1282
1288
result.addSuccess(self)
1283
1289
result.stopTest(self)
1285
return unittest.TestCase.run(self, result)
1292
return unittest.TestCase.run(self, result)
1295
absent_attr = object()
1296
for attr_name in self.attrs_to_keep:
1297
attr = getattr(self, attr_name, absent_attr)
1298
if attr is not absent_attr:
1299
saved_attrs[attr_name] = attr
1300
self.__dict__ = saved_attrs
1287
1302
def tearDown(self):
1288
1303
self._runCleanups()
2573
2588
return test_list
2591
def suite_matches_id_list(test_suite, id_list):
2592
"""Warns about tests not appearing or appearing more than once.
2594
:param test_suite: A TestSuite object.
2595
:param test_id_list: The list of test ids that should be found in
2598
:return: (absents, duplicates) absents is a list containing the test found
2599
in id_list but not in test_suite, duplicates is a list containing the
2600
test found multiple times in test_suite.
2602
When using a prefined test id list, it may occurs that some tests do not
2603
exist anymore or that some tests use the same id. This function warns the
2604
tester about potential problems in his workflow (test lists are volatile)
2605
or in the test suite itself (using the same id for several tests does not
2606
help to localize defects).
2608
# Build a dict counting id occurrences
2610
for test in iter_suite_tests(test_suite):
2612
tests[id] = tests.get(id, 0) + 1
2617
occurs = tests.get(id, 0)
2619
not_found.append(id)
2621
duplicates.append(id)
2623
return not_found, duplicates
2576
2626
class TestIdList(object):
2577
2627
"""Test id list to filter a test suite.
2651
2701
'bzrlib.tests.test_deprecated_graph',
2652
2702
'bzrlib.tests.test_diff',
2653
2703
'bzrlib.tests.test_dirstate',
2704
'bzrlib.tests.test_directory_service',
2654
2705
'bzrlib.tests.test_email_message',
2655
2706
'bzrlib.tests.test_errors',
2656
2707
'bzrlib.tests.test_escaped_store',
2708
2759
'bzrlib.tests.test_repository',
2709
2760
'bzrlib.tests.test_revert',
2710
2761
'bzrlib.tests.test_revision',
2711
'bzrlib.tests.test_revisionnamespaces',
2762
'bzrlib.tests.test_revisionspec',
2712
2763
'bzrlib.tests.test_revisiontree',
2713
2764
'bzrlib.tests.test_rio',
2714
2765
'bzrlib.tests.test_sampler',
2741
2792
'bzrlib.tests.test_tsort',
2742
2793
'bzrlib.tests.test_tuned_gzip',
2743
2794
'bzrlib.tests.test_ui',
2795
'bzrlib.tests.test_uncommit',
2744
2796
'bzrlib.tests.test_upgrade',
2745
2797
'bzrlib.tests.test_urlutils',
2746
2798
'bzrlib.tests.test_versionedfile',
2796
2848
pack_suite = filter_suite_by_id_list(pack_suite, id_filter)
2797
2849
suite.addTest(pack_suite)
2799
# XXX: MODULES_TO_TEST should be obsoleted ?
2800
for mod in [m for m in MODULES_TO_TEST
2801
if keep_only is None or id_filter.is_module_name_used(m)]:
2802
mod_suite = loader.loadTestsFromModule(mod)
2803
if keep_only is not None:
2804
mod_suite = filter_suite_by_id_list(mod_suite, id_filter)
2805
suite.addTest(mod_suite)
2807
2851
for mod in MODULES_TO_DOCTEST:
2809
2853
doc_suite = doctest.DocTestSuite(mod)
2816
2860
suite.addTest(doc_suite)
2818
2862
default_encoding = sys.getdefaultencoding()
2819
for name, plugin in [(n, p) for (n, p) in bzrlib.plugin.plugins().items()
2820
if (keep_only is None
2821
or id_filter.is_module_name_used(
2822
p.module.__name__))]:
2824
plugin_suite = plugin.test_suite()
2825
except ImportError, e:
2826
bzrlib.trace.warning(
2827
'Unable to test plugin "%s": %s', name, e)
2829
if plugin_suite is not None:
2830
if keep_only is not None:
2831
plugin_suite = filter_suite_by_id_list(plugin_suite,
2833
suite.addTest(plugin_suite)
2863
for name, plugin in bzrlib.plugin.plugins().items():
2864
if keep_only is not None:
2865
if not id_filter.is_module_name_used(plugin.module.__name__):
2867
plugin_suite = plugin.test_suite()
2868
# We used to catch ImportError here and turn it into just a warning,
2869
# but really if you don't have --no-plugins this should be a failure.
2870
# mbp 20080213 - see http://bugs.launchpad.net/bugs/189771
2871
if plugin_suite is not None:
2872
if keep_only is not None:
2873
plugin_suite = filter_suite_by_id_list(plugin_suite,
2875
suite.addTest(plugin_suite)
2834
2876
if default_encoding != sys.getdefaultencoding():
2835
2877
bzrlib.trace.warning(
2836
2878
'Plugin "%s" tried to reset default encoding to: %s', name,
2837
2879
sys.getdefaultencoding())
2839
2881
sys.setdefaultencoding(default_encoding)
2883
if keep_only is not None:
2884
# Do some sanity checks on the id_list filtering
2885
not_found, duplicates = suite_matches_id_list(suite, keep_only)
2886
for id in not_found:
2887
bzrlib.trace.warning('"%s" not found in the test suite', id)
2888
for id in duplicates:
2889
bzrlib.trace.warning('"%s" is used as an id by several tests', id)
2843
def multiply_tests_from_modules(module_name_list, scenario_iter):
2894
def multiply_tests_from_modules(module_name_list, scenario_iter, loader=None):
2844
2895
"""Adapt all tests in some given modules to given scenarios.
2846
2897
This is the recommended public interface for test parameterization.
2853
2904
:param scenario_iter: Iterable of pairs of (scenario_name,
2854
2905
scenario_param_dict).
2906
:param loader: If provided, will be used instead of a new
2907
bzrlib.tests.TestLoader() instance.
2856
2909
This returns a new TestSuite containing the cross product of
2857
2910
all the tests in all the modules, each repeated for each scenario.
2873
2926
>>> tests[1].param
2876
loader = TestLoader()
2929
# XXX: Isn't load_tests() a better way to provide the same functionality
2930
# without forcing a predefined TestScenarioApplier ? --vila 080215
2932
loader = TestUtil.TestLoader()
2934
suite = loader.suiteClass()
2878
2936
adapter = TestScenarioApplier()
2879
2937
adapter.scenarios = list(scenario_iter)
2880
2938
adapt_modules(module_name_list, adapter, loader, suite)