/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/tests/__init__.py

  • Committer: Robert Collins
  • Date: 2008-04-04 00:43:07 UTC
  • mfrom: (3331 +trunk)
  • mto: This revision was merged to the branch mainline in revision 3333.
  • Revision ID: robertc@robertcollins.net-20080404004307-0whomfhm3yal2rvw
Resolve conflicts.

Show diffs side-by-side

added added

removed removed

Lines of Context:
109
109
 
110
110
default_transport = LocalURLServer
111
111
 
112
 
MODULES_TO_TEST = []
113
112
MODULES_TO_DOCTEST = [
 
113
        bzrlib,
114
114
        bzrlib.timestamp,
115
115
        bzrlib.errors,
116
116
        bzrlib.export,
314
314
        self.report_success(test)
315
315
        self._cleanupLogFile(test)
316
316
        unittest.TestResult.addSuccess(self, test)
 
317
        test._log_contents = ''
317
318
 
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 = ''
359
361
 
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')
792
797
 
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.
812
817
        """
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)
816
822
 
817
823
    def _clear_hooks(self):
818
824
        # prevent hooks affecting tests
1282
1288
                    result.addSuccess(self)
1283
1289
                result.stopTest(self)
1284
1290
                return
1285
 
        return unittest.TestCase.run(self, result)
 
1291
        try:
 
1292
            return unittest.TestCase.run(self, result)
 
1293
        finally:
 
1294
            saved_attrs = {}
 
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
1286
1301
 
1287
1302
    def tearDown(self):
1288
1303
        self._runCleanups()
2573
2588
    return test_list
2574
2589
 
2575
2590
 
 
2591
def suite_matches_id_list(test_suite, id_list):
 
2592
    """Warns about tests not appearing or appearing more than once.
 
2593
 
 
2594
    :param test_suite: A TestSuite object.
 
2595
    :param test_id_list: The list of test ids that should be found in 
 
2596
         test_suite.
 
2597
 
 
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.
 
2601
 
 
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).
 
2607
    """
 
2608
    # Build a dict counting id occurrences
 
2609
    tests = dict()
 
2610
    for test in iter_suite_tests(test_suite):
 
2611
        id = test.id()
 
2612
        tests[id] = tests.get(id, 0) + 1
 
2613
 
 
2614
    not_found = []
 
2615
    duplicates = []
 
2616
    for id in id_list:
 
2617
        occurs = tests.get(id, 0)
 
2618
        if not occurs:
 
2619
            not_found.append(id)
 
2620
        elif occurs > 1:
 
2621
            duplicates.append(id)
 
2622
 
 
2623
    return not_found, duplicates
 
2624
 
 
2625
 
2576
2626
class TestIdList(object):
2577
2627
    """Test id list to filter a test suite.
2578
2628
 
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)
2798
2850
 
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)
2806
 
 
2807
2851
    for mod in MODULES_TO_DOCTEST:
2808
2852
        try:
2809
2853
            doc_suite = doctest.DocTestSuite(mod)
2835
2879
                sys.getdefaultencoding())
2836
2880
            reload(sys)
2837
2881
            sys.setdefaultencoding(default_encoding)
 
2882
 
 
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)
 
2890
 
2838
2891
    return suite
2839
2892
 
2840
2893
 
2841
 
def multiply_tests_from_modules(module_name_list, scenario_iter):
 
2894
def multiply_tests_from_modules(module_name_list, scenario_iter, loader=None):
2842
2895
    """Adapt all tests in some given modules to given scenarios.
2843
2896
 
2844
2897
    This is the recommended public interface for test parameterization.
2850
2903
        modules.
2851
2904
    :param scenario_iter: Iterable of pairs of (scenario_name, 
2852
2905
        scenario_param_dict).
 
2906
    :param loader: If provided, will be used instead of a new 
 
2907
        bzrlib.tests.TestLoader() instance.
2853
2908
 
2854
2909
    This returns a new TestSuite containing the cross product of
2855
2910
    all the tests in all the modules, each repeated for each scenario.
2871
2926
    >>> tests[1].param
2872
2927
    2
2873
2928
    """
2874
 
    loader = TestLoader()
2875
 
    suite = TestSuite()
 
2929
    # XXX: Isn't load_tests() a better way to provide the same functionality
 
2930
    # without forcing a predefined TestScenarioApplier ? --vila 080215
 
2931
    if loader is None:
 
2932
        loader = TestUtil.TestLoader()
 
2933
 
 
2934
    suite = loader.suiteClass()
 
2935
 
2876
2936
    adapter = TestScenarioApplier()
2877
2937
    adapter.scenarios = list(scenario_iter)
2878
2938
    adapt_modules(module_name_list, adapter, loader, suite)