~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_selftest.py

  • Committer: Patch Queue Manager
  • Date: 2016-02-01 19:13:13 UTC
  • mfrom: (6614.2.2 trunk)
  • Revision ID: pqm@pqm.ubuntu.com-20160201191313-wdfvmfff1djde6oq
(vila) Release 2.7.0 (Vincent Ladeuil)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005-2011 Canonical Ltd
 
1
# Copyright (C) 2005-2013, 2016 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
1002
1002
            pass
1003
1003
        test = unittest.FunctionTestCase(test_function)
1004
1004
        test.run(result)
1005
 
        self.assertEquals(1, result.calls)
 
1005
        self.assertEqual(1, result.calls)
1006
1006
 
1007
1007
    def test_startTests_only_once(self):
1008
1008
        """With multiple tests startTests should still only be called once"""
1014
1014
            unittest.FunctionTestCase(lambda: None),
1015
1015
            unittest.FunctionTestCase(lambda: None)])
1016
1016
        suite.run(result)
1017
 
        self.assertEquals(1, result.calls)
1018
 
        self.assertEquals(2, result.count)
 
1017
        self.assertEqual(1, result.calls)
 
1018
        self.assertEqual(2, result.count)
1019
1019
 
1020
1020
 
1021
1021
class TestRunner(tests.TestCase):
1732
1732
            test_selftest, '_add_numbers')
1733
1733
        self.assertEqual(test_selftest._add_numbers(2, 10),
1734
1734
            12)
1735
 
        self.assertEquals(calls, [((2, 10), {})])
 
1735
        self.assertEqual(calls, [((2, 10), {})])
1736
1736
 
1737
1737
 
1738
1738
def _add_numbers(a, b):
1912
1912
        self.assertIsInstance(2, int)
1913
1913
        self.assertIsInstance(u'', basestring)
1914
1914
        e = self.assertRaises(AssertionError, self.assertIsInstance, None, int)
1915
 
        self.assertEquals(str(e),
 
1915
        self.assertEqual(str(e),
1916
1916
            "None is an instance of <type 'NoneType'> rather than <type 'int'>")
1917
1917
        self.assertRaises(AssertionError, self.assertIsInstance, 23.3, int)
1918
1918
        e = self.assertRaises(AssertionError,
1919
1919
            self.assertIsInstance, None, int, "it's just not")
1920
 
        self.assertEquals(str(e),
 
1920
        self.assertEqual(str(e),
1921
1921
            "None is an instance of <type 'NoneType'> rather than <type 'int'>"
1922
1922
            ": it's just not")
1923
1923
 
1928
1928
    def test_assertEqualDiff(self):
1929
1929
        e = self.assertRaises(AssertionError,
1930
1930
                              self.assertEqualDiff, '', '\n')
1931
 
        self.assertEquals(str(e),
 
1931
        self.assertEqual(str(e),
1932
1932
                          # Don't blink ! The '+' applies to the second string
1933
1933
                          'first string is missing a final newline.\n+ \n')
1934
1934
        e = self.assertRaises(AssertionError,
1935
1935
                              self.assertEqualDiff, '\n', '')
1936
 
        self.assertEquals(str(e),
 
1936
        self.assertEqual(str(e),
1937
1937
                          # Don't blink ! The '-' applies to the second string
1938
1938
                          'second string is missing a final newline.\n- \n')
1939
1939
 
1996
1996
            warnings.warn("this is your last warning")
1997
1997
            return a + b
1998
1998
        wlist, result = self.callCatchWarnings(meth, 1, 2)
1999
 
        self.assertEquals(3, result)
 
1999
        self.assertEqual(3, result)
2000
2000
        # would like just to compare them, but UserWarning doesn't implement
2001
2001
        # eq well
2002
2002
        w0, = wlist
2003
2003
        self.assertIsInstance(w0, UserWarning)
2004
 
        self.assertEquals("this is your last warning", str(w0))
 
2004
        self.assertEqual("this is your last warning", str(w0))
2005
2005
 
2006
2006
 
2007
2007
class TestConvenienceMakers(tests.TestCaseWithTransport):
2028
2028
        tree = self.make_branch_and_tree('t1')
2029
2029
        base = tree.bzrdir.root_transport.base
2030
2030
        self.assertStartsWith(base, 'file://')
2031
 
        self.assertEquals(tree.bzrdir.root_transport,
 
2031
        self.assertEqual(tree.bzrdir.root_transport,
2032
2032
                tree.branch.bzrdir.root_transport)
2033
 
        self.assertEquals(tree.bzrdir.root_transport,
 
2033
        self.assertEqual(tree.bzrdir.root_transport,
2034
2034
                tree.branch.repository.bzrdir.root_transport)
2035
2035
 
2036
2036
 
2300
2300
        self.assertEqual(['rocks'], self.argv)
2301
2301
        self.assertEqual(34, self.retcode)
2302
2302
        self.assertEqual('It sure does!\n', out)
2303
 
        self.assertEquals(out, self.out)
 
2303
        self.assertEqual(out, self.out)
2304
2304
        self.assertEqual('', err)
2305
 
        self.assertEquals(err, self.err)
 
2305
        self.assertEqual(err, self.err)
2306
2306
 
2307
2307
    def test_run_bzr_error_regexes(self):
2308
2308
        self.out = ''
2847
2847
            self.run_bzr, ['assert-fail'])
2848
2848
        # make sure we got the real thing, not an error from somewhere else in
2849
2849
        # the test framework
2850
 
        self.assertEquals('always fails', str(e))
 
2850
        self.assertEqual('always fails', str(e))
2851
2851
        # check that there's no traceback in the test log
2852
2852
        self.assertNotContainsRe(self.get_log(), r'Traceback')
2853
2853
 
2902
2902
    def test_load_tests_from_module_name_smoke_test(self):
2903
2903
        loader = TestUtil.TestLoader()
2904
2904
        suite = loader.loadTestsFromModuleName('bzrlib.tests.test_sampler')
2905
 
        self.assertEquals(['bzrlib.tests.test_sampler.DemoTest.test_nothing'],
 
2905
        self.assertEqual(['bzrlib.tests.test_sampler.DemoTest.test_nothing'],
2906
2906
                          _test_ids(suite))
2907
2907
 
2908
2908
    def test_load_tests_from_module_name_with_bogus_module_name(self):
2937
2937
 
2938
2938
    def test_empty_list(self):
2939
2939
        id_list = self._create_id_list([])
2940
 
        self.assertEquals({}, id_list.tests)
2941
 
        self.assertEquals({}, id_list.modules)
 
2940
        self.assertEqual({}, id_list.tests)
 
2941
        self.assertEqual({}, id_list.modules)
2942
2942
 
2943
2943
    def test_valid_list(self):
2944
2944
        id_list = self._create_id_list(
2971
2971
        test_list = ['bzrlib.tests.test_sampler.DemoTest.test_nothing',
2972
2972
                     'bogus']
2973
2973
        not_found, duplicates = tests.suite_matches_id_list(suite, test_list)
2974
 
        self.assertEquals(['bogus'], not_found)
2975
 
        self.assertEquals([], duplicates)
 
2974
        self.assertEqual(['bogus'], not_found)
 
2975
        self.assertEqual([], duplicates)
2976
2976
 
2977
2977
    def test_suite_matches_id_list_with_duplicates(self):
2978
2978
        loader = TestUtil.TestLoader()
2985
2985
        test_list = ['bzrlib.tests.test_sampler.DemoTest.test_nothing',]
2986
2986
        not_found, duplicates = tests.suite_matches_id_list(
2987
2987
            dupes, test_list)
2988
 
        self.assertEquals([], not_found)
2989
 
        self.assertEquals(['bzrlib.tests.test_sampler.DemoTest.test_nothing'],
 
2988
        self.assertEqual([], not_found)
 
2989
        self.assertEqual(['bzrlib.tests.test_sampler.DemoTest.test_nothing'],
2990
2990
                          duplicates)
2991
2991
 
2992
2992
 
3065
3065
        suite = tests.test_suite(test_list,
3066
3066
                                 ['bzrlib.tests.test_selftest.TestTestSuite'])
3067
3067
        # test_test_suite_list_and_start is not included 
3068
 
        self.assertEquals(test_list, _test_ids(suite))
 
3068
        self.assertEqual(test_list, _test_ids(suite))
3069
3069
 
3070
3070
 
3071
3071
class TestLoadTestIdList(tests.TestCaseInTempDir):
3084
3084
        self._create_test_list_file(test_list_fname,
3085
3085
                                    'mod1.cl1.meth1\nmod2.cl2.meth2\n')
3086
3086
        tlist = tests.load_test_id_list(test_list_fname)
3087
 
        self.assertEquals(2, len(tlist))
3088
 
        self.assertEquals('mod1.cl1.meth1', tlist[0])
3089
 
        self.assertEquals('mod2.cl2.meth2', tlist[1])
 
3087
        self.assertEqual(2, len(tlist))
 
3088
        self.assertEqual('mod1.cl1.meth1', tlist[0])
 
3089
        self.assertEqual('mod2.cl2.meth2', tlist[1])
3090
3090
 
3091
3091
    def test_load_dirty_file(self):
3092
3092
        test_list_fname = 'test.list'
3094
3094
                                    '  mod1.cl1.meth1\n\nmod2.cl2.meth2  \n'
3095
3095
                                    'bar baz\n')
3096
3096
        tlist = tests.load_test_id_list(test_list_fname)
3097
 
        self.assertEquals(4, len(tlist))
3098
 
        self.assertEquals('mod1.cl1.meth1', tlist[0])
3099
 
        self.assertEquals('', tlist[1])
3100
 
        self.assertEquals('mod2.cl2.meth2', tlist[2])
3101
 
        self.assertEquals('bar baz', tlist[3])
 
3097
        self.assertEqual(4, len(tlist))
 
3098
        self.assertEqual('mod1.cl1.meth1', tlist[0])
 
3099
        self.assertEqual('', tlist[1])
 
3100
        self.assertEqual('mod2.cl2.meth2', tlist[2])
 
3101
        self.assertEqual('bar baz', tlist[3])
3102
3102
 
3103
3103
 
3104
3104
class TestFilteredByModuleTestLoader(tests.TestCase):
3112
3112
        test_list = ['bzrlib.tests.test_sampler.DemoTest.test_nothing']
3113
3113
        loader = self._create_loader(test_list)
3114
3114
        suite = loader.loadTestsFromModuleName('bzrlib.tests.test_sampler')
3115
 
        self.assertEquals(test_list, _test_ids(suite))
 
3115
        self.assertEqual(test_list, _test_ids(suite))
3116
3116
 
3117
3117
    def test_exclude_tests(self):
3118
3118
        test_list = ['bogus']
3119
3119
        loader = self._create_loader(test_list)
3120
3120
        suite = loader.loadTestsFromModuleName('bzrlib.tests.test_sampler')
3121
 
        self.assertEquals([], _test_ids(suite))
 
3121
        self.assertEqual([], _test_ids(suite))
3122
3122
 
3123
3123
 
3124
3124
class TestFilteredByNameStartTestLoader(tests.TestCase):
3134
3134
        loader = self._create_loader('bzrlib.tests.test_samp')
3135
3135
 
3136
3136
        suite = loader.loadTestsFromModuleName('bzrlib.tests.test_sampler')
3137
 
        self.assertEquals(test_list, _test_ids(suite))
 
3137
        self.assertEqual(test_list, _test_ids(suite))
3138
3138
 
3139
3139
    def test_load_tests_inside_module(self):
3140
3140
        test_list = ['bzrlib.tests.test_sampler.DemoTest.test_nothing']
3141
3141
        loader = self._create_loader('bzrlib.tests.test_sampler.Demo')
3142
3142
 
3143
3143
        suite = loader.loadTestsFromModuleName('bzrlib.tests.test_sampler')
3144
 
        self.assertEquals(test_list, _test_ids(suite))
 
3144
        self.assertEqual(test_list, _test_ids(suite))
3145
3145
 
3146
3146
    def test_exclude_tests(self):
3147
3147
        test_list = ['bogus']
3148
3148
        loader = self._create_loader('bogus')
3149
3149
 
3150
3150
        suite = loader.loadTestsFromModuleName('bzrlib.tests.test_sampler')
3151
 
        self.assertEquals([], _test_ids(suite))
 
3151
        self.assertEqual([], _test_ids(suite))
3152
3152
 
3153
3153
 
3154
3154
class TestTestPrefixRegistry(tests.TestCase):
3160
3160
    def test_register_new_prefix(self):
3161
3161
        tpr = self._get_registry()
3162
3162
        tpr.register('foo', 'fff.ooo.ooo')
3163
 
        self.assertEquals('fff.ooo.ooo', tpr.get('foo'))
 
3163
        self.assertEqual('fff.ooo.ooo', tpr.get('foo'))
3164
3164
 
3165
3165
    def test_register_existing_prefix(self):
3166
3166
        tpr = self._get_registry()
3167
3167
        tpr.register('bar', 'bbb.aaa.rrr')
3168
3168
        tpr.register('bar', 'bBB.aAA.rRR')
3169
 
        self.assertEquals('bbb.aaa.rrr', tpr.get('bar'))
 
3169
        self.assertEqual('bbb.aaa.rrr', tpr.get('bar'))
3170
3170
        self.assertThat(self.get_log(),
3171
3171
            DocTestMatches("...bar...bbb.aaa.rrr...BB.aAA.rRR",
3172
3172
                           doctest.ELLIPSIS))
3178
3178
    def test_resolve_prefix(self):
3179
3179
        tpr = self._get_registry()
3180
3180
        tpr.register('bar', 'bb.aa.rr')
3181
 
        self.assertEquals('bb.aa.rr', tpr.resolve_alias('bar'))
 
3181
        self.assertEqual('bb.aa.rr', tpr.resolve_alias('bar'))
3182
3182
 
3183
3183
    def test_resolve_unknown_alias(self):
3184
3184
        tpr = self._get_registry()
3187
3187
 
3188
3188
    def test_predefined_prefixes(self):
3189
3189
        tpr = tests.test_prefix_alias_registry
3190
 
        self.assertEquals('bzrlib', tpr.resolve_alias('bzrlib'))
3191
 
        self.assertEquals('bzrlib.doc', tpr.resolve_alias('bd'))
3192
 
        self.assertEquals('bzrlib.utils', tpr.resolve_alias('bu'))
3193
 
        self.assertEquals('bzrlib.tests', tpr.resolve_alias('bt'))
3194
 
        self.assertEquals('bzrlib.tests.blackbox', tpr.resolve_alias('bb'))
3195
 
        self.assertEquals('bzrlib.plugins', tpr.resolve_alias('bp'))
 
3190
        self.assertEqual('bzrlib', tpr.resolve_alias('bzrlib'))
 
3191
        self.assertEqual('bzrlib.doc', tpr.resolve_alias('bd'))
 
3192
        self.assertEqual('bzrlib.utils', tpr.resolve_alias('bu'))
 
3193
        self.assertEqual('bzrlib.tests', tpr.resolve_alias('bt'))
 
3194
        self.assertEqual('bzrlib.tests.blackbox', tpr.resolve_alias('bb'))
 
3195
        self.assertEqual('bzrlib.plugins', tpr.resolve_alias('bp'))
3196
3196
 
3197
3197
 
3198
3198
class TestThreadLeakDetection(tests.TestCase):
3521
3521
            def test_me(self):
3522
3522
                # The first call save the 42 value
3523
3523
                self.overrideEnv('MYVAR', None)
3524
 
                self.assertEquals(None, os.environ.get('MYVAR'))
 
3524
                self.assertEqual(None, os.environ.get('MYVAR'))
3525
3525
                # Make sure we can call it twice
3526
3526
                self.overrideEnv('MYVAR', None)
3527
 
                self.assertEquals(None, os.environ.get('MYVAR'))
 
3527
                self.assertEqual(None, os.environ.get('MYVAR'))
3528
3528
        output = StringIO()
3529
3529
        result = tests.TextTestResult(output, 0, 1)
3530
3530
        Test('test_me').run(result)
3531
3531
        if not result.wasStrictlySuccessful():
3532
3532
            self.fail(output.getvalue())
3533
3533
        # We get our value back
3534
 
        self.assertEquals('42', os.environ.get('MYVAR'))
 
3534
        self.assertEqual('42', os.environ.get('MYVAR'))
3535
3535
 
3536
3536
 
3537
3537
class TestIsolatedEnv(tests.TestCase):
3553
3553
        # Make sure we know the definition of BZR_HOME: not part of os.environ
3554
3554
        # for tests.TestCase.
3555
3555
        self.assertTrue('BZR_HOME' in tests.isolated_environ)
3556
 
        self.assertEquals(None, tests.isolated_environ['BZR_HOME'])
 
3556
        self.assertEqual(None, tests.isolated_environ['BZR_HOME'])
3557
3557
        # Being part of isolated_environ, BZR_HOME should not appear here
3558
3558
        self.assertFalse('BZR_HOME' in os.environ)
3559
3559
        # Make sure we know the definition of LINES: part of os.environ for
3560
3560
        # tests.TestCase
3561
3561
        self.assertTrue('LINES' in tests.isolated_environ)
3562
 
        self.assertEquals('25', tests.isolated_environ['LINES'])
3563
 
        self.assertEquals('25', os.environ['LINES'])
 
3562
        self.assertEqual('25', tests.isolated_environ['LINES'])
 
3563
        self.assertEqual('25', os.environ['LINES'])
3564
3564
 
3565
3565
    def test_injecting_unknown_variable(self):
3566
3566
        # BZR_HOME is known to be absent from os.environ
3567
3567
        test = self.ScratchMonkey('test_me')
3568
3568
        tests.override_os_environ(test, {'BZR_HOME': 'foo'})
3569
 
        self.assertEquals('foo', os.environ['BZR_HOME'])
 
3569
        self.assertEqual('foo', os.environ['BZR_HOME'])
3570
3570
        tests.restore_os_environ(test)
3571
3571
        self.assertFalse('BZR_HOME' in os.environ)
3572
3572
 
3574
3574
        test = self.ScratchMonkey('test_me')
3575
3575
        # LINES is known to be present in os.environ
3576
3576
        tests.override_os_environ(test, {'LINES': '42'})
3577
 
        self.assertEquals('42', os.environ['LINES'])
 
3577
        self.assertEqual('42', os.environ['LINES'])
3578
3578
        tests.restore_os_environ(test)
3579
 
        self.assertEquals('25', os.environ['LINES'])
 
3579
        self.assertEqual('25', os.environ['LINES'])
3580
3580
 
3581
3581
    def test_deleting_variable(self):
3582
3582
        test = self.ScratchMonkey('test_me')
3584
3584
        tests.override_os_environ(test, {'LINES': None})
3585
3585
        self.assertTrue('LINES' not in os.environ)
3586
3586
        tests.restore_os_environ(test)
3587
 
        self.assertEquals('25', os.environ['LINES'])
 
3587
        self.assertEqual('25', os.environ['LINES'])
3588
3588
 
3589
3589
 
3590
3590
class TestDocTestSuiteIsolation(tests.TestCase):
3677
3677
        # test at the command level without loading the whole test suite
3678
3678
        out, err = self.run_bzr(('selftest', '--list') + selftest_args)
3679
3679
        actual = out.splitlines()
3680
 
        self.assertEquals(expected, actual)
 
3680
        self.assertEqual(expected, actual)
3681
3681
 
3682
3682
    def test_full_list(self):
3683
3683
        self.assertTestList(['a', 'b', 'c'])
3718
3718
        test.run(result)
3719
3719
        self.assertTrue(hasattr(test, '_counters'))
3720
3720
        self.assertTrue(test._counters.has_key('myhook'))
3721
 
        self.assertEquals(expected_calls, test._counters['myhook'])
 
3721
        self.assertEqual(expected_calls, test._counters['myhook'])
3722
3722
 
3723
3723
    def test_no_hook(self):
3724
3724
        self.assertHookCalls(0, 'no_hook')