1
# Copyright (C) 2005, 2006, 2007, 2008 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
"""Tests for the test framework."""
21
from StringIO import StringIO
40
from bzrlib.progress import _BaseProgressBar
41
from bzrlib.repofmt import (
45
from bzrlib.symbol_versioning import (
50
from bzrlib.tests import (
57
TestCaseWithMemoryTransport,
58
TestCaseWithTransport,
67
exclude_tests_by_condition,
69
filter_suite_by_condition,
75
split_suite_by_condition,
80
from bzrlib.tests.test_sftp_transport import TestCaseWithSFTPServer
81
from bzrlib.tests.TestUtil import _load_module_by_name
82
from bzrlib.trace import note
83
from bzrlib.transport.memory import MemoryServer, MemoryTransport
84
from bzrlib.version import _get_bzr_source_tree
87
def _test_ids(test_suite):
88
"""Get the ids for the tests in a test suite."""
89
return [t.id() for t in iter_suite_tests(test_suite)]
92
class SelftestTests(TestCase):
94
def test_import_tests(self):
95
mod = _load_module_by_name('bzrlib.tests.test_selftest')
96
self.assertEqual(mod.SelftestTests, SelftestTests)
98
def test_import_test_failure(self):
99
self.assertRaises(ImportError,
100
_load_module_by_name,
101
'bzrlib.no-name-yet')
103
class MetaTestLog(TestCase):
105
def test_logging(self):
106
"""Test logs are captured when a test fails."""
107
self.log('a test message')
108
self._log_file.flush()
109
self.assertContainsRe(self._get_log(keep_log_file=True),
113
class TestUnicodeFilename(TestCase):
115
def test_probe_passes(self):
116
"""UnicodeFilename._probe passes."""
117
# We can't test much more than that because the behaviour depends
119
tests.UnicodeFilename._probe()
122
class TestTreeShape(TestCaseInTempDir):
124
def test_unicode_paths(self):
125
self.requireFeature(tests.UnicodeFilename)
127
filename = u'hell\u00d8'
128
self.build_tree_contents([(filename, 'contents of hello')])
129
self.failUnlessExists(filename)
132
class TestTransportProviderAdapter(TestCase):
133
"""A group of tests that test the transport implementation adaption core.
135
This is a meta test that the tests are applied to all available
138
This will be generalised in the future which is why it is in this
139
test file even though it is specific to transport tests at the moment.
142
def test_get_transport_permutations(self):
143
# this checks that get_test_permutations defined by the module is
144
# called by the adapter get_transport_test_permutations method.
145
class MockModule(object):
146
def get_test_permutations(self):
147
return sample_permutation
148
sample_permutation = [(1,2), (3,4)]
149
from bzrlib.tests.test_transport_implementations \
150
import TransportTestProviderAdapter
151
adapter = TransportTestProviderAdapter()
152
self.assertEqual(sample_permutation,
153
adapter.get_transport_test_permutations(MockModule()))
155
def test_adapter_checks_all_modules(self):
156
# this checks that the adapter returns as many permutations as there
157
# are in all the registered transport modules - we assume if this
158
# matches its probably doing the right thing especially in combination
159
# with the tests for setting the right classes below.
160
from bzrlib.tests.test_transport_implementations \
161
import TransportTestProviderAdapter
162
from bzrlib.transport import _get_transport_modules
163
modules = _get_transport_modules()
164
permutation_count = 0
165
for module in modules:
167
permutation_count += len(reduce(getattr,
168
(module + ".get_test_permutations").split('.')[1:],
169
__import__(module))())
170
except errors.DependencyNotPresent:
172
input_test = TestTransportProviderAdapter(
173
"test_adapter_sets_transport_class")
174
adapter = TransportTestProviderAdapter()
175
self.assertEqual(permutation_count,
176
len(list(iter(adapter.adapt(input_test)))))
178
def test_adapter_sets_transport_class(self):
179
# Check that the test adapter inserts a transport and server into the
182
# This test used to know about all the possible transports and the
183
# order they were returned but that seems overly brittle (mbp
185
from bzrlib.tests.test_transport_implementations \
186
import TransportTestProviderAdapter
187
scenarios = TransportTestProviderAdapter().scenarios
188
# there are at least that many builtin transports
189
self.assertTrue(len(scenarios) > 6)
190
one_scenario = scenarios[0]
191
self.assertIsInstance(one_scenario[0], str)
192
self.assertTrue(issubclass(one_scenario[1]["transport_class"],
193
bzrlib.transport.Transport))
194
self.assertTrue(issubclass(one_scenario[1]["transport_server"],
195
bzrlib.transport.Server))
198
class TestBranchProviderAdapter(TestCase):
199
"""A group of tests that test the branch implementation test adapter."""
201
def test_constructor(self):
202
# check that constructor parameters are passed through to the adapted
204
from bzrlib.tests.branch_implementations import BranchTestProviderAdapter
207
formats = [("c", "C"), ("d", "D")]
208
adapter = BranchTestProviderAdapter(server1, server2, formats)
209
self.assertEqual(2, len(adapter.scenarios))
212
{'branch_format': 'c',
213
'bzrdir_format': 'C',
214
'transport_readonly_server': 'b',
215
'transport_server': 'a'}),
217
{'branch_format': 'd',
218
'bzrdir_format': 'D',
219
'transport_readonly_server': 'b',
220
'transport_server': 'a'})],
224
class TestBzrDirProviderAdapter(TestCase):
225
"""A group of tests that test the bzr dir implementation test adapter."""
227
def test_adapted_tests(self):
228
# check that constructor parameters are passed through to the adapted
230
from bzrlib.tests.bzrdir_implementations import BzrDirTestProviderAdapter
235
adapter = BzrDirTestProviderAdapter(vfs_factory,
236
server1, server2, formats)
239
{'bzrdir_format': 'c',
240
'transport_readonly_server': 'b',
241
'transport_server': 'a',
242
'vfs_transport_factory': 'v'}),
244
{'bzrdir_format': 'd',
245
'transport_readonly_server': 'b',
246
'transport_server': 'a',
247
'vfs_transport_factory': 'v'})],
251
class TestRepositoryParameterisation(TestCase):
252
"""A group of tests that test the repository implementation test adapter."""
254
def test_formats_to_scenarios(self):
255
"""The adapter can generate all the scenarios needed."""
256
from bzrlib.tests.per_repository import formats_to_scenarios
257
formats = [("(c)", remote.RemoteRepositoryFormat()),
258
("(d)", repository.format_registry.get(
259
'Bazaar pack repository format 1 (needs bzr 0.92)\n'))]
260
no_vfs_scenarios = formats_to_scenarios(formats, "server", "readonly",
262
vfs_scenarios = formats_to_scenarios(formats, "server", "readonly",
263
vfs_transport_factory="vfs")
264
# no_vfs generate scenarios without vfs_transport_factory
266
('RemoteRepositoryFormat(c)',
267
{'bzrdir_format': remote.RemoteBzrDirFormat(),
268
'repository_format': remote.RemoteRepositoryFormat(),
269
'transport_readonly_server': 'readonly',
270
'transport_server': 'server'}),
271
('RepositoryFormatKnitPack1(d)',
272
{'bzrdir_format': bzrdir.BzrDirMetaFormat1(),
273
'repository_format': pack_repo.RepositoryFormatKnitPack1(),
274
'transport_readonly_server': 'readonly',
275
'transport_server': 'server'})],
278
('RemoteRepositoryFormat(c)',
279
{'bzrdir_format': remote.RemoteBzrDirFormat(),
280
'repository_format': remote.RemoteRepositoryFormat(),
281
'transport_readonly_server': 'readonly',
282
'transport_server': 'server',
283
'vfs_transport_factory': 'vfs'}),
284
('RepositoryFormatKnitPack1(d)',
285
{'bzrdir_format': bzrdir.BzrDirMetaFormat1(),
286
'repository_format': pack_repo.RepositoryFormatKnitPack1(),
287
'transport_readonly_server': 'readonly',
288
'transport_server': 'server',
289
'vfs_transport_factory': 'vfs'})],
293
class TestTestScenarioApplier(TestCase):
294
"""Tests for the test adaption facilities."""
296
def test_adapt_applies_scenarios(self):
297
from bzrlib.tests.per_repository import TestScenarioApplier
298
input_test = TestTestScenarioApplier("test_adapt_test_to_scenario")
299
adapter = TestScenarioApplier()
300
adapter.scenarios = [("1", "dict"), ("2", "settings")]
302
def capture_call(test, scenario):
303
calls.append((test, scenario))
305
adapter.adapt_test_to_scenario = capture_call
306
adapter.adapt(input_test)
307
self.assertEqual([(input_test, ("1", "dict")),
308
(input_test, ("2", "settings"))], calls)
310
def test_adapt_test_to_scenario(self):
311
from bzrlib.tests.per_repository import TestScenarioApplier
312
input_test = TestTestScenarioApplier("test_adapt_test_to_scenario")
313
adapter = TestScenarioApplier()
314
# setup two adapted tests
315
adapted_test1 = adapter.adapt_test_to_scenario(input_test,
317
{"bzrdir_format":"bzr_format",
318
"repository_format":"repo_fmt",
319
"transport_server":"transport_server",
320
"transport_readonly_server":"readonly-server"}))
321
adapted_test2 = adapter.adapt_test_to_scenario(input_test,
322
("new id 2", {"bzrdir_format":None}))
323
# input_test should have been altered.
324
self.assertRaises(AttributeError, getattr, input_test, "bzrdir_format")
325
# the new tests are mutually incompatible, ensuring it has
326
# made new ones, and unspecified elements in the scenario
327
# should not have been altered.
328
self.assertEqual("bzr_format", adapted_test1.bzrdir_format)
329
self.assertEqual("repo_fmt", adapted_test1.repository_format)
330
self.assertEqual("transport_server", adapted_test1.transport_server)
331
self.assertEqual("readonly-server",
332
adapted_test1.transport_readonly_server)
334
"bzrlib.tests.test_selftest.TestTestScenarioApplier."
335
"test_adapt_test_to_scenario(new id)",
337
self.assertEqual(None, adapted_test2.bzrdir_format)
339
"bzrlib.tests.test_selftest.TestTestScenarioApplier."
340
"test_adapt_test_to_scenario(new id 2)",
344
class TestInterRepositoryProviderAdapter(TestCase):
345
"""A group of tests that test the InterRepository test adapter."""
347
def test_adapted_tests(self):
348
# check that constructor parameters are passed through to the adapted
350
from bzrlib.tests.interrepository_implementations import \
351
InterRepositoryTestProviderAdapter
354
formats = [(str, "C1", "C2"), (int, "D1", "D2")]
355
adapter = InterRepositoryTestProviderAdapter(server1, server2, formats)
358
{'interrepo_class': str,
359
'repository_format': 'C1',
360
'repository_format_to': 'C2',
361
'transport_readonly_server': 'b',
362
'transport_server': 'a'}),
364
{'interrepo_class': int,
365
'repository_format': 'D1',
366
'repository_format_to': 'D2',
367
'transport_readonly_server': 'b',
368
'transport_server': 'a'})],
369
adapter.formats_to_scenarios(formats))
372
class TestWorkingTreeProviderAdapter(TestCase):
373
"""A group of tests that test the workingtree implementation test adapter."""
375
def test_scenarios(self):
376
# check that constructor parameters are passed through to the adapted
378
from bzrlib.tests.workingtree_implementations \
379
import WorkingTreeTestProviderAdapter
382
formats = [workingtree.WorkingTreeFormat2(),
383
workingtree.WorkingTreeFormat3(),]
384
adapter = WorkingTreeTestProviderAdapter(server1, server2, formats)
386
('WorkingTreeFormat2',
387
{'bzrdir_format': formats[0]._matchingbzrdir,
388
'transport_readonly_server': 'b',
389
'transport_server': 'a',
390
'workingtree_format': formats[0]}),
391
('WorkingTreeFormat3',
392
{'bzrdir_format': formats[1]._matchingbzrdir,
393
'transport_readonly_server': 'b',
394
'transport_server': 'a',
395
'workingtree_format': formats[1]})],
399
class TestTreeProviderAdapter(TestCase):
400
"""Test the setup of tree_implementation tests."""
402
def test_adapted_tests(self):
403
# the tree implementation adapter is meant to setup one instance for
404
# each working tree format, and one additional instance that will
405
# use the default wt format, but create a revision tree for the tests.
406
# this means that the wt ones should have the workingtree_to_test_tree
407
# attribute set to 'return_parameter' and the revision one set to
408
# revision_tree_from_workingtree.
410
from bzrlib.tests.tree_implementations import (
411
TreeTestProviderAdapter,
413
revision_tree_from_workingtree
415
input_test = TestTreeProviderAdapter(
416
"test_adapted_tests")
419
formats = [workingtree.WorkingTreeFormat2(),
420
workingtree.WorkingTreeFormat3(),]
421
adapter = TreeTestProviderAdapter(server1, server2, formats)
422
suite = adapter.adapt(input_test)
423
tests = list(iter(suite))
424
# XXX We should not have tests fail as we add more scenarios
426
self.assertEqual(7, len(tests))
427
# this must match the default format setp up in
428
# TreeTestProviderAdapter.adapt
429
default_format = workingtree.WorkingTreeFormat3
430
self.assertEqual(tests[0].workingtree_format, formats[0])
431
self.assertEqual(tests[0].bzrdir_format, formats[0]._matchingbzrdir)
432
self.assertEqual(tests[0].transport_server, server1)
433
self.assertEqual(tests[0].transport_readonly_server, server2)
434
self.assertEqual(tests[0]._workingtree_to_test_tree, return_parameter)
435
self.assertEqual(tests[1].workingtree_format, formats[1])
436
self.assertEqual(tests[1].bzrdir_format, formats[1]._matchingbzrdir)
437
self.assertEqual(tests[1].transport_server, server1)
438
self.assertEqual(tests[1].transport_readonly_server, server2)
439
self.assertEqual(tests[1]._workingtree_to_test_tree, return_parameter)
440
self.assertIsInstance(tests[2].workingtree_format, default_format)
441
#self.assertEqual(tests[2].bzrdir_format,
442
# default_format._matchingbzrdir)
443
self.assertEqual(tests[2].transport_server, server1)
444
self.assertEqual(tests[2].transport_readonly_server, server2)
445
self.assertEqual(tests[2]._workingtree_to_test_tree,
446
revision_tree_from_workingtree)
449
class TestInterTreeProviderAdapter(TestCase):
450
"""A group of tests that test the InterTreeTestAdapter."""
452
def test_adapted_tests(self):
453
# check that constructor parameters are passed through to the adapted
455
# for InterTree tests we want the machinery to bring up two trees in
456
# each instance: the base one, and the one we are interacting with.
457
# because each optimiser can be direction specific, we need to test
458
# each optimiser in its chosen direction.
459
# unlike the TestProviderAdapter we dont want to automatically add a
460
# parameterized one for WorkingTree - the optimisers will tell us what
462
from bzrlib.tests.tree_implementations import (
464
revision_tree_from_workingtree
466
from bzrlib.tests.intertree_implementations import (
467
InterTreeTestProviderAdapter,
469
from bzrlib.workingtree import WorkingTreeFormat2, WorkingTreeFormat3
470
input_test = TestInterTreeProviderAdapter(
471
"test_adapted_tests")
474
format1 = WorkingTreeFormat2()
475
format2 = WorkingTreeFormat3()
476
formats = [("1", str, format1, format2, "converter1"),
477
("2", int, format2, format1, "converter2")]
478
adapter = InterTreeTestProviderAdapter(server1, server2, formats)
479
suite = adapter.adapt(input_test)
480
tests = list(iter(suite))
481
self.assertEqual(2, len(tests))
482
self.assertEqual(tests[0].intertree_class, formats[0][1])
483
self.assertEqual(tests[0].workingtree_format, formats[0][2])
484
self.assertEqual(tests[0].workingtree_format_to, formats[0][3])
485
self.assertEqual(tests[0].mutable_trees_to_test_trees, formats[0][4])
486
self.assertEqual(tests[0]._workingtree_to_test_tree, return_parameter)
487
self.assertEqual(tests[0].transport_server, server1)
488
self.assertEqual(tests[0].transport_readonly_server, server2)
489
self.assertEqual(tests[1].intertree_class, formats[1][1])
490
self.assertEqual(tests[1].workingtree_format, formats[1][2])
491
self.assertEqual(tests[1].workingtree_format_to, formats[1][3])
492
self.assertEqual(tests[1].mutable_trees_to_test_trees, formats[1][4])
493
self.assertEqual(tests[1]._workingtree_to_test_tree, return_parameter)
494
self.assertEqual(tests[1].transport_server, server1)
495
self.assertEqual(tests[1].transport_readonly_server, server2)
498
class TestTestCaseInTempDir(TestCaseInTempDir):
500
def test_home_is_not_working(self):
501
self.assertNotEqual(self.test_dir, self.test_home_dir)
502
cwd = osutils.getcwd()
503
self.assertIsSameRealPath(self.test_dir, cwd)
504
self.assertIsSameRealPath(self.test_home_dir, os.environ['HOME'])
506
def test_assertEqualStat_equal(self):
507
from bzrlib.tests.test_dirstate import _FakeStat
508
self.build_tree(["foo"])
509
real = os.lstat("foo")
510
fake = _FakeStat(real.st_size, real.st_mtime, real.st_ctime,
511
real.st_dev, real.st_ino, real.st_mode)
512
self.assertEqualStat(real, fake)
514
def test_assertEqualStat_notequal(self):
515
self.build_tree(["foo", "bar"])
516
self.assertRaises(AssertionError, self.assertEqualStat,
517
os.lstat("foo"), os.lstat("bar"))
520
class TestTestCaseWithMemoryTransport(TestCaseWithMemoryTransport):
522
def test_home_is_non_existant_dir_under_root(self):
523
"""The test_home_dir for TestCaseWithMemoryTransport is missing.
525
This is because TestCaseWithMemoryTransport is for tests that do not
526
need any disk resources: they should be hooked into bzrlib in such a
527
way that no global settings are being changed by the test (only a
528
few tests should need to do that), and having a missing dir as home is
529
an effective way to ensure that this is the case.
531
self.assertIsSameRealPath(
532
self.TEST_ROOT + "/MemoryTransportMissingHomeDir",
534
self.assertIsSameRealPath(self.test_home_dir, os.environ['HOME'])
536
def test_cwd_is_TEST_ROOT(self):
537
self.assertIsSameRealPath(self.test_dir, self.TEST_ROOT)
538
cwd = osutils.getcwd()
539
self.assertIsSameRealPath(self.test_dir, cwd)
541
def test_make_branch_and_memory_tree(self):
542
"""In TestCaseWithMemoryTransport we should not make the branch on disk.
544
This is hard to comprehensively robustly test, so we settle for making
545
a branch and checking no directory was created at its relpath.
547
tree = self.make_branch_and_memory_tree('dir')
548
# Guard against regression into MemoryTransport leaking
549
# files to disk instead of keeping them in memory.
550
self.failIf(osutils.lexists('dir'))
551
self.assertIsInstance(tree, memorytree.MemoryTree)
553
def test_make_branch_and_memory_tree_with_format(self):
554
"""make_branch_and_memory_tree should accept a format option."""
555
format = bzrdir.BzrDirMetaFormat1()
556
format.repository_format = weaverepo.RepositoryFormat7()
557
tree = self.make_branch_and_memory_tree('dir', format=format)
558
# Guard against regression into MemoryTransport leaking
559
# files to disk instead of keeping them in memory.
560
self.failIf(osutils.lexists('dir'))
561
self.assertIsInstance(tree, memorytree.MemoryTree)
562
self.assertEqual(format.repository_format.__class__,
563
tree.branch.repository._format.__class__)
565
def test_make_branch_builder(self):
566
builder = self.make_branch_builder('dir')
567
self.assertIsInstance(builder, branchbuilder.BranchBuilder)
568
# Guard against regression into MemoryTransport leaking
569
# files to disk instead of keeping them in memory.
570
self.failIf(osutils.lexists('dir'))
572
def test_make_branch_builder_with_format(self):
573
# Use a repo layout that doesn't conform to a 'named' layout, to ensure
574
# that the format objects are used.
575
format = bzrdir.BzrDirMetaFormat1()
576
repo_format = weaverepo.RepositoryFormat7()
577
format.repository_format = repo_format
578
builder = self.make_branch_builder('dir', format=format)
579
the_branch = builder.get_branch()
580
# Guard against regression into MemoryTransport leaking
581
# files to disk instead of keeping them in memory.
582
self.failIf(osutils.lexists('dir'))
583
self.assertEqual(format.repository_format.__class__,
584
the_branch.repository._format.__class__)
585
self.assertEqual(repo_format.get_format_string(),
586
self.get_transport().get_bytes(
587
'dir/.bzr/repository/format'))
589
def test_make_branch_builder_with_format_name(self):
590
builder = self.make_branch_builder('dir', format='knit')
591
the_branch = builder.get_branch()
592
# Guard against regression into MemoryTransport leaking
593
# files to disk instead of keeping them in memory.
594
self.failIf(osutils.lexists('dir'))
595
dir_format = bzrdir.format_registry.make_bzrdir('knit')
596
self.assertEqual(dir_format.repository_format.__class__,
597
the_branch.repository._format.__class__)
598
self.assertEqual('Bazaar-NG Knit Repository Format 1',
599
self.get_transport().get_bytes(
600
'dir/.bzr/repository/format'))
602
def test_safety_net(self):
603
"""No test should modify the safety .bzr directory.
605
We just test that the _check_safety_net private method raises
606
AssertionError, it's easier than building a test suite with the same
609
# Oops, a commit in the current directory (i.e. without local .bzr
610
# directory) will crawl up the hierarchy to find a .bzr directory.
611
self.run_bzr(['commit', '-mfoo', '--unchanged'])
612
# But we have a safety net in place.
613
self.assertRaises(AssertionError, self._check_safety_net)
616
class TestTestCaseWithTransport(TestCaseWithTransport):
617
"""Tests for the convenience functions TestCaseWithTransport introduces."""
619
def test_get_readonly_url_none(self):
620
from bzrlib.transport import get_transport
621
from bzrlib.transport.memory import MemoryServer
622
from bzrlib.transport.readonly import ReadonlyTransportDecorator
623
self.vfs_transport_factory = MemoryServer
624
self.transport_readonly_server = None
625
# calling get_readonly_transport() constructs a decorator on the url
627
url = self.get_readonly_url()
628
url2 = self.get_readonly_url('foo/bar')
629
t = get_transport(url)
630
t2 = get_transport(url2)
631
self.failUnless(isinstance(t, ReadonlyTransportDecorator))
632
self.failUnless(isinstance(t2, ReadonlyTransportDecorator))
633
self.assertEqual(t2.base[:-1], t.abspath('foo/bar'))
635
def test_get_readonly_url_http(self):
636
from bzrlib.tests.http_server import HttpServer
637
from bzrlib.transport import get_transport
638
from bzrlib.transport.local import LocalURLServer
639
from bzrlib.transport.http import HttpTransportBase
640
self.transport_server = LocalURLServer
641
self.transport_readonly_server = HttpServer
642
# calling get_readonly_transport() gives us a HTTP server instance.
643
url = self.get_readonly_url()
644
url2 = self.get_readonly_url('foo/bar')
645
# the transport returned may be any HttpTransportBase subclass
646
t = get_transport(url)
647
t2 = get_transport(url2)
648
self.failUnless(isinstance(t, HttpTransportBase))
649
self.failUnless(isinstance(t2, HttpTransportBase))
650
self.assertEqual(t2.base[:-1], t.abspath('foo/bar'))
652
def test_is_directory(self):
653
"""Test assertIsDirectory assertion"""
654
t = self.get_transport()
655
self.build_tree(['a_dir/', 'a_file'], transport=t)
656
self.assertIsDirectory('a_dir', t)
657
self.assertRaises(AssertionError, self.assertIsDirectory, 'a_file', t)
658
self.assertRaises(AssertionError, self.assertIsDirectory, 'not_here', t)
660
def test_make_branch_builder(self):
661
builder = self.make_branch_builder('dir')
662
rev_id = builder.build_commit()
663
self.failUnlessExists('dir')
664
a_dir = bzrdir.BzrDir.open('dir')
665
self.assertRaises(errors.NoWorkingTree, a_dir.open_workingtree)
666
a_branch = a_dir.open_branch()
667
builder_branch = builder.get_branch()
668
self.assertEqual(a_branch.base, builder_branch.base)
669
self.assertEqual((1, rev_id), builder_branch.last_revision_info())
670
self.assertEqual((1, rev_id), a_branch.last_revision_info())
673
class TestTestCaseTransports(TestCaseWithTransport):
676
super(TestTestCaseTransports, self).setUp()
677
self.vfs_transport_factory = MemoryServer
679
def test_make_bzrdir_preserves_transport(self):
680
t = self.get_transport()
681
result_bzrdir = self.make_bzrdir('subdir')
682
self.assertIsInstance(result_bzrdir.transport,
684
# should not be on disk, should only be in memory
685
self.failIfExists('subdir')
688
class TestChrootedTest(ChrootedTestCase):
690
def test_root_is_root(self):
691
from bzrlib.transport import get_transport
692
t = get_transport(self.get_readonly_url())
694
self.assertEqual(url, t.clone('..').base)
697
class MockProgress(_BaseProgressBar):
698
"""Progress-bar standin that records calls.
700
Useful for testing pb using code.
704
_BaseProgressBar.__init__(self)
708
self.calls.append(('tick',))
710
def update(self, msg=None, current=None, total=None):
711
self.calls.append(('update', msg, current, total))
714
self.calls.append(('clear',))
716
def note(self, msg, *args):
717
self.calls.append(('note', msg, args))
720
class TestTestResult(TestCase):
722
def check_timing(self, test_case, expected_re):
723
result = bzrlib.tests.TextTestResult(self._log_file,
727
test_case.run(result)
728
timed_string = result._testTimeString(test_case)
729
self.assertContainsRe(timed_string, expected_re)
731
def test_test_reporting(self):
732
class ShortDelayTestCase(TestCase):
733
def test_short_delay(self):
735
def test_short_benchmark(self):
736
self.time(time.sleep, 0.003)
737
self.check_timing(ShortDelayTestCase('test_short_delay'),
739
# if a benchmark time is given, we want a x of y style result.
740
self.check_timing(ShortDelayTestCase('test_short_benchmark'),
741
r"^ +[0-9]+ms/ +[0-9]+ms$")
743
def test_unittest_reporting_unittest_class(self):
744
# getting the time from a non-bzrlib test works ok
745
class ShortDelayTestCase(unittest.TestCase):
746
def test_short_delay(self):
748
self.check_timing(ShortDelayTestCase('test_short_delay'),
751
def test_assigned_benchmark_file_stores_date(self):
753
result = bzrlib.tests.TextTestResult(self._log_file,
758
output_string = output.getvalue()
759
# if you are wondering about the regexp please read the comment in
760
# test_bench_history (bzrlib.tests.test_selftest.TestRunner)
761
# XXX: what comment? -- Andrew Bennetts
762
self.assertContainsRe(output_string, "--date [0-9.]+")
764
def test_benchhistory_records_test_times(self):
765
result_stream = StringIO()
766
result = bzrlib.tests.TextTestResult(
770
bench_history=result_stream
773
# we want profile a call and check that its test duration is recorded
774
# make a new test instance that when run will generate a benchmark
775
example_test_case = TestTestResult("_time_hello_world_encoding")
776
# execute the test, which should succeed and record times
777
example_test_case.run(result)
778
lines = result_stream.getvalue().splitlines()
779
self.assertEqual(2, len(lines))
780
self.assertContainsRe(lines[1],
781
" *[0-9]+ms bzrlib.tests.test_selftest.TestTestResult"
782
"._time_hello_world_encoding")
784
def _time_hello_world_encoding(self):
785
"""Profile two sleep calls
787
This is used to exercise the test framework.
789
self.time(unicode, 'hello', errors='replace')
790
self.time(unicode, 'world', errors='replace')
792
def test_lsprofiling(self):
793
"""Verbose test result prints lsprof statistics from test cases."""
794
self.requireFeature(test_lsprof.LSProfFeature)
795
result_stream = StringIO()
796
result = bzrlib.tests.VerboseTestResult(
797
unittest._WritelnDecorator(result_stream),
801
# we want profile a call of some sort and check it is output by
802
# addSuccess. We dont care about addError or addFailure as they
803
# are not that interesting for performance tuning.
804
# make a new test instance that when run will generate a profile
805
example_test_case = TestTestResult("_time_hello_world_encoding")
806
example_test_case._gather_lsprof_in_benchmarks = True
807
# execute the test, which should succeed and record profiles
808
example_test_case.run(result)
809
# lsprofile_something()
810
# if this worked we want
811
# LSProf output for <built in function unicode> (['hello'], {'errors': 'replace'})
812
# CallCount Recursive Total(ms) Inline(ms) module:lineno(function)
813
# (the lsprof header)
814
# ... an arbitrary number of lines
815
# and the function call which is time.sleep.
816
# 1 0 ??? ??? ???(sleep)
817
# and then repeated but with 'world', rather than 'hello'.
818
# this should appear in the output stream of our test result.
819
output = result_stream.getvalue()
820
self.assertContainsRe(output,
821
r"LSProf output for <type 'unicode'>\(\('hello',\), {'errors': 'replace'}\)")
822
self.assertContainsRe(output,
823
r" *CallCount *Recursive *Total\(ms\) *Inline\(ms\) *module:lineno\(function\)\n")
824
self.assertContainsRe(output,
825
r"( +1 +0 +0\.\d+ +0\.\d+ +<method 'disable' of '_lsprof\.Profiler' objects>\n)?")
826
self.assertContainsRe(output,
827
r"LSProf output for <type 'unicode'>\(\('world',\), {'errors': 'replace'}\)\n")
829
def test_known_failure(self):
830
"""A KnownFailure being raised should trigger several result actions."""
831
class InstrumentedTestResult(ExtendedTestResult):
833
def report_test_start(self, test): pass
834
def report_known_failure(self, test, err):
835
self._call = test, err
836
result = InstrumentedTestResult(None, None, None, None)
838
raise KnownFailure('failed!')
839
test = unittest.FunctionTestCase(test_function)
841
# it should invoke 'report_known_failure'.
842
self.assertEqual(2, len(result._call))
843
self.assertEqual(test, result._call[0])
844
self.assertEqual(KnownFailure, result._call[1][0])
845
self.assertIsInstance(result._call[1][1], KnownFailure)
846
# we dont introspec the traceback, if the rest is ok, it would be
847
# exceptional for it not to be.
848
# it should update the known_failure_count on the object.
849
self.assertEqual(1, result.known_failure_count)
850
# the result should be successful.
851
self.assertTrue(result.wasSuccessful())
853
def test_verbose_report_known_failure(self):
854
# verbose test output formatting
855
result_stream = StringIO()
856
result = bzrlib.tests.VerboseTestResult(
857
unittest._WritelnDecorator(result_stream),
861
test = self.get_passing_test()
862
result.startTest(test)
863
prefix = len(result_stream.getvalue())
864
# the err parameter has the shape:
865
# (class, exception object, traceback)
866
# KnownFailures dont get their tracebacks shown though, so we
868
err = (KnownFailure, KnownFailure('foo'), None)
869
result.report_known_failure(test, err)
870
output = result_stream.getvalue()[prefix:]
871
lines = output.splitlines()
872
self.assertContainsRe(lines[0], r'XFAIL *\d+ms$')
873
self.assertEqual(lines[1], ' foo')
874
self.assertEqual(2, len(lines))
876
def test_text_report_known_failure(self):
877
# text test output formatting
879
result = bzrlib.tests.TextTestResult(
885
test = self.get_passing_test()
886
# this seeds the state to handle reporting the test.
887
result.startTest(test)
888
# the err parameter has the shape:
889
# (class, exception object, traceback)
890
# KnownFailures dont get their tracebacks shown though, so we
892
err = (KnownFailure, KnownFailure('foo'), None)
893
result.report_known_failure(test, err)
896
('update', '[1 in 0s] passing_test', None, None),
897
('note', 'XFAIL: %s\n%s\n', ('passing_test', err[1]))
900
# known_failures should be printed in the summary, so if we run a test
901
# after there are some known failures, the update prefix should match
903
result.known_failure_count = 3
907
('update', '[2 in 0s] passing_test', None, None),
911
def get_passing_test(self):
912
"""Return a test object that can't be run usefully."""
915
return unittest.FunctionTestCase(passing_test)
917
def test_add_not_supported(self):
918
"""Test the behaviour of invoking addNotSupported."""
919
class InstrumentedTestResult(ExtendedTestResult):
920
def report_test_start(self, test): pass
921
def report_unsupported(self, test, feature):
922
self._call = test, feature
923
result = InstrumentedTestResult(None, None, None, None)
924
test = SampleTestCase('_test_pass')
926
result.startTest(test)
927
result.addNotSupported(test, feature)
928
# it should invoke 'report_unsupported'.
929
self.assertEqual(2, len(result._call))
930
self.assertEqual(test, result._call[0])
931
self.assertEqual(feature, result._call[1])
932
# the result should be successful.
933
self.assertTrue(result.wasSuccessful())
934
# it should record the test against a count of tests not run due to
936
self.assertEqual(1, result.unsupported['Feature'])
937
# and invoking it again should increment that counter
938
result.addNotSupported(test, feature)
939
self.assertEqual(2, result.unsupported['Feature'])
941
def test_verbose_report_unsupported(self):
942
# verbose test output formatting
943
result_stream = StringIO()
944
result = bzrlib.tests.VerboseTestResult(
945
unittest._WritelnDecorator(result_stream),
949
test = self.get_passing_test()
951
result.startTest(test)
952
prefix = len(result_stream.getvalue())
953
result.report_unsupported(test, feature)
954
output = result_stream.getvalue()[prefix:]
955
lines = output.splitlines()
956
self.assertEqual(lines, ['NODEP 0ms', " The feature 'Feature' is not available."])
958
def test_text_report_unsupported(self):
959
# text test output formatting
961
result = bzrlib.tests.TextTestResult(
967
test = self.get_passing_test()
969
# this seeds the state to handle reporting the test.
970
result.startTest(test)
971
result.report_unsupported(test, feature)
972
# no output on unsupported features
974
[('update', '[1 in 0s] passing_test', None, None)
977
# the number of missing features should be printed in the progress
978
# summary, so check for that.
979
result.unsupported = {'foo':0, 'bar':0}
983
('update', '[2 in 0s, 2 missing] passing_test', None, None),
987
def test_unavailable_exception(self):
988
"""An UnavailableFeature being raised should invoke addNotSupported."""
989
class InstrumentedTestResult(ExtendedTestResult):
991
def report_test_start(self, test): pass
992
def addNotSupported(self, test, feature):
993
self._call = test, feature
994
result = InstrumentedTestResult(None, None, None, None)
997
raise UnavailableFeature(feature)
998
test = unittest.FunctionTestCase(test_function)
1000
# it should invoke 'addNotSupported'.
1001
self.assertEqual(2, len(result._call))
1002
self.assertEqual(test, result._call[0])
1003
self.assertEqual(feature, result._call[1])
1004
# and not count as an error
1005
self.assertEqual(0, result.error_count)
1007
def test_strict_with_unsupported_feature(self):
1008
result = bzrlib.tests.TextTestResult(self._log_file, descriptions=0,
1010
test = self.get_passing_test()
1011
feature = "Unsupported Feature"
1012
result.addNotSupported(test, feature)
1013
self.assertFalse(result.wasStrictlySuccessful())
1014
self.assertEqual(None, result._extractBenchmarkTime(test))
1016
def test_strict_with_known_failure(self):
1017
result = bzrlib.tests.TextTestResult(self._log_file, descriptions=0,
1019
test = self.get_passing_test()
1020
err = (KnownFailure, KnownFailure('foo'), None)
1021
result._addKnownFailure(test, err)
1022
self.assertFalse(result.wasStrictlySuccessful())
1023
self.assertEqual(None, result._extractBenchmarkTime(test))
1025
def test_strict_with_success(self):
1026
result = bzrlib.tests.TextTestResult(self._log_file, descriptions=0,
1028
test = self.get_passing_test()
1029
result.addSuccess(test)
1030
self.assertTrue(result.wasStrictlySuccessful())
1031
self.assertEqual(None, result._extractBenchmarkTime(test))
1034
class TestUnicodeFilenameFeature(TestCase):
1036
def test_probe_passes(self):
1037
"""UnicodeFilenameFeature._probe passes."""
1038
# We can't test much more than that because the behaviour depends
1040
tests.UnicodeFilenameFeature._probe()
1043
class TestRunner(TestCase):
1045
def dummy_test(self):
1048
def run_test_runner(self, testrunner, test):
1049
"""Run suite in testrunner, saving global state and restoring it.
1051
This current saves and restores:
1052
TestCaseInTempDir.TEST_ROOT
1054
There should be no tests in this file that use bzrlib.tests.TextTestRunner
1055
without using this convenience method, because of our use of global state.
1057
old_root = TestCaseInTempDir.TEST_ROOT
1059
TestCaseInTempDir.TEST_ROOT = None
1060
return testrunner.run(test)
1062
TestCaseInTempDir.TEST_ROOT = old_root
1064
def test_known_failure_failed_run(self):
1065
# run a test that generates a known failure which should be printed in
1066
# the final output when real failures occur.
1067
def known_failure_test():
1068
raise KnownFailure('failed')
1069
test = unittest.TestSuite()
1070
test.addTest(unittest.FunctionTestCase(known_failure_test))
1072
raise AssertionError('foo')
1073
test.addTest(unittest.FunctionTestCase(failing_test))
1075
runner = TextTestRunner(stream=stream)
1076
result = self.run_test_runner(runner, test)
1077
lines = stream.getvalue().splitlines()
1080
'======================================================================',
1081
'FAIL: unittest.FunctionTestCase (failing_test)',
1082
'----------------------------------------------------------------------',
1083
'Traceback (most recent call last):',
1084
' raise AssertionError(\'foo\')',
1085
'AssertionError: foo',
1087
'----------------------------------------------------------------------',
1089
'FAILED (failures=1, known_failure_count=1)'],
1090
lines[0:5] + lines[6:10] + lines[11:])
1092
def test_known_failure_ok_run(self):
1093
# run a test that generates a known failure which should be printed in the final output.
1094
def known_failure_test():
1095
raise KnownFailure('failed')
1096
test = unittest.FunctionTestCase(known_failure_test)
1098
runner = TextTestRunner(stream=stream)
1099
result = self.run_test_runner(runner, test)
1100
self.assertContainsRe(stream.getvalue(),
1103
'Ran 1 test in .*\n'
1105
'OK \\(known_failures=1\\)\n')
1107
def test_skipped_test(self):
1108
# run a test that is skipped, and check the suite as a whole still
1110
# skipping_test must be hidden in here so it's not run as a real test
1111
def skipping_test():
1112
raise TestSkipped('test intentionally skipped')
1114
runner = TextTestRunner(stream=self._log_file)
1115
test = unittest.FunctionTestCase(skipping_test)
1116
result = self.run_test_runner(runner, test)
1117
self.assertTrue(result.wasSuccessful())
1119
def test_skipped_from_setup(self):
1121
class SkippedSetupTest(TestCase):
1124
calls.append('setUp')
1125
self.addCleanup(self.cleanup)
1126
raise TestSkipped('skipped setup')
1128
def test_skip(self):
1129
self.fail('test reached')
1132
calls.append('cleanup')
1134
runner = TextTestRunner(stream=self._log_file)
1135
test = SkippedSetupTest('test_skip')
1136
result = self.run_test_runner(runner, test)
1137
self.assertTrue(result.wasSuccessful())
1138
# Check if cleanup was called the right number of times.
1139
self.assertEqual(['setUp', 'cleanup'], calls)
1141
def test_skipped_from_test(self):
1143
class SkippedTest(TestCase):
1146
calls.append('setUp')
1147
self.addCleanup(self.cleanup)
1149
def test_skip(self):
1150
raise TestSkipped('skipped test')
1153
calls.append('cleanup')
1155
runner = TextTestRunner(stream=self._log_file)
1156
test = SkippedTest('test_skip')
1157
result = self.run_test_runner(runner, test)
1158
self.assertTrue(result.wasSuccessful())
1159
# Check if cleanup was called the right number of times.
1160
self.assertEqual(['setUp', 'cleanup'], calls)
1162
def test_not_applicable(self):
1163
# run a test that is skipped because it's not applicable
1164
def not_applicable_test():
1165
from bzrlib.tests import TestNotApplicable
1166
raise TestNotApplicable('this test never runs')
1168
runner = TextTestRunner(stream=out, verbosity=2)
1169
test = unittest.FunctionTestCase(not_applicable_test)
1170
result = self.run_test_runner(runner, test)
1171
self._log_file.write(out.getvalue())
1172
self.assertTrue(result.wasSuccessful())
1173
self.assertTrue(result.wasStrictlySuccessful())
1174
self.assertContainsRe(out.getvalue(),
1175
r'(?m)not_applicable_test * N/A')
1176
self.assertContainsRe(out.getvalue(),
1177
r'(?m)^ this test never runs')
1179
def test_not_applicable_demo(self):
1180
# just so you can see it in the test output
1181
raise TestNotApplicable('this test is just a demonstation')
1183
def test_unsupported_features_listed(self):
1184
"""When unsupported features are encountered they are detailed."""
1185
class Feature1(Feature):
1186
def _probe(self): return False
1187
class Feature2(Feature):
1188
def _probe(self): return False
1189
# create sample tests
1190
test1 = SampleTestCase('_test_pass')
1191
test1._test_needs_features = [Feature1()]
1192
test2 = SampleTestCase('_test_pass')
1193
test2._test_needs_features = [Feature2()]
1194
test = unittest.TestSuite()
1198
runner = TextTestRunner(stream=stream)
1199
result = self.run_test_runner(runner, test)
1200
lines = stream.getvalue().splitlines()
1203
"Missing feature 'Feature1' skipped 1 tests.",
1204
"Missing feature 'Feature2' skipped 1 tests.",
1208
def test_bench_history(self):
1209
# tests that the running the benchmark produces a history file
1210
# containing a timestamp and the revision id of the bzrlib source which
1212
workingtree = _get_bzr_source_tree()
1213
test = TestRunner('dummy_test')
1215
runner = TextTestRunner(stream=self._log_file, bench_history=output)
1216
result = self.run_test_runner(runner, test)
1217
output_string = output.getvalue()
1218
self.assertContainsRe(output_string, "--date [0-9.]+")
1219
if workingtree is not None:
1220
revision_id = workingtree.get_parent_ids()[0]
1221
self.assertEndsWith(output_string.rstrip(), revision_id)
1223
def assertLogDeleted(self, test):
1224
log = test._get_log()
1225
self.assertEqual("DELETED log file to reduce memory footprint", log)
1226
self.assertEqual('', test._log_contents)
1227
self.assertIs(None, test._log_file_name)
1229
def test_success_log_deleted(self):
1230
"""Successful tests have their log deleted"""
1232
class LogTester(TestCase):
1234
def test_success(self):
1235
self.log('this will be removed\n')
1237
sio = cStringIO.StringIO()
1238
runner = TextTestRunner(stream=sio)
1239
test = LogTester('test_success')
1240
result = self.run_test_runner(runner, test)
1242
self.assertLogDeleted(test)
1244
def test_skipped_log_deleted(self):
1245
"""Skipped tests have their log deleted"""
1247
class LogTester(TestCase):
1249
def test_skipped(self):
1250
self.log('this will be removed\n')
1251
raise tests.TestSkipped()
1253
sio = cStringIO.StringIO()
1254
runner = TextTestRunner(stream=sio)
1255
test = LogTester('test_skipped')
1256
result = self.run_test_runner(runner, test)
1258
self.assertLogDeleted(test)
1260
def test_not_aplicable_log_deleted(self):
1261
"""Not applicable tests have their log deleted"""
1263
class LogTester(TestCase):
1265
def test_not_applicable(self):
1266
self.log('this will be removed\n')
1267
raise tests.TestNotApplicable()
1269
sio = cStringIO.StringIO()
1270
runner = TextTestRunner(stream=sio)
1271
test = LogTester('test_not_applicable')
1272
result = self.run_test_runner(runner, test)
1274
self.assertLogDeleted(test)
1276
def test_known_failure_log_deleted(self):
1277
"""Know failure tests have their log deleted"""
1279
class LogTester(TestCase):
1281
def test_known_failure(self):
1282
self.log('this will be removed\n')
1283
raise tests.KnownFailure()
1285
sio = cStringIO.StringIO()
1286
runner = TextTestRunner(stream=sio)
1287
test = LogTester('test_known_failure')
1288
result = self.run_test_runner(runner, test)
1290
self.assertLogDeleted(test)
1292
def test_fail_log_kept(self):
1293
"""Failed tests have their log kept"""
1295
class LogTester(TestCase):
1297
def test_fail(self):
1298
self.log('this will be kept\n')
1299
self.fail('this test fails')
1301
sio = cStringIO.StringIO()
1302
runner = TextTestRunner(stream=sio)
1303
test = LogTester('test_fail')
1304
result = self.run_test_runner(runner, test)
1306
text = sio.getvalue()
1307
self.assertContainsRe(text, 'this will be kept')
1308
self.assertContainsRe(text, 'this test fails')
1310
log = test._get_log()
1311
self.assertContainsRe(log, 'this will be kept')
1312
self.assertEqual(log, test._log_contents)
1314
def test_error_log_kept(self):
1315
"""Tests with errors have their log kept"""
1317
class LogTester(TestCase):
1319
def test_error(self):
1320
self.log('this will be kept\n')
1321
raise ValueError('random exception raised')
1323
sio = cStringIO.StringIO()
1324
runner = TextTestRunner(stream=sio)
1325
test = LogTester('test_error')
1326
result = self.run_test_runner(runner, test)
1328
text = sio.getvalue()
1329
self.assertContainsRe(text, 'this will be kept')
1330
self.assertContainsRe(text, 'random exception raised')
1332
log = test._get_log()
1333
self.assertContainsRe(log, 'this will be kept')
1334
self.assertEqual(log, test._log_contents)
1337
class SampleTestCase(TestCase):
1339
def _test_pass(self):
1342
class _TestException(Exception):
1345
class TestTestCase(TestCase):
1346
"""Tests that test the core bzrlib TestCase."""
1348
def test_debug_flags_sanitised(self):
1349
"""The bzrlib debug flags should be sanitised by setUp."""
1350
if 'allow_debug' in tests.selftest_debug_flags:
1351
raise TestNotApplicable(
1352
'-Eallow_debug option prevents debug flag sanitisation')
1353
# we could set something and run a test that will check
1354
# it gets santised, but this is probably sufficient for now:
1355
# if someone runs the test with -Dsomething it will error.
1356
self.assertEqual(set(), bzrlib.debug.debug_flags)
1358
def change_selftest_debug_flags(self, new_flags):
1359
orig_selftest_flags = tests.selftest_debug_flags
1360
self.addCleanup(self._restore_selftest_debug_flags, orig_selftest_flags)
1361
tests.selftest_debug_flags = set(new_flags)
1363
def _restore_selftest_debug_flags(self, flags):
1364
tests.selftest_debug_flags = flags
1366
def test_allow_debug_flag(self):
1367
"""The -Eallow_debug flag prevents bzrlib.debug.debug_flags from being
1368
sanitised (i.e. cleared) before running a test.
1370
self.change_selftest_debug_flags(set(['allow_debug']))
1371
bzrlib.debug.debug_flags = set(['a-flag'])
1372
class TestThatRecordsFlags(TestCase):
1373
def test_foo(nested_self):
1374
self.flags = set(bzrlib.debug.debug_flags)
1375
test = TestThatRecordsFlags('test_foo')
1376
test.run(self.make_test_result())
1377
self.assertEqual(set(['a-flag']), self.flags)
1379
def test_debug_flags_restored(self):
1380
"""The bzrlib debug flags should be restored to their original state
1381
after the test was run, even if allow_debug is set.
1383
self.change_selftest_debug_flags(set(['allow_debug']))
1384
# Now run a test that modifies debug.debug_flags.
1385
bzrlib.debug.debug_flags = set(['original-state'])
1386
class TestThatModifiesFlags(TestCase):
1388
bzrlib.debug.debug_flags = set(['modified'])
1389
test = TestThatModifiesFlags('test_foo')
1390
test.run(self.make_test_result())
1391
self.assertEqual(set(['original-state']), bzrlib.debug.debug_flags)
1393
def make_test_result(self):
1394
return bzrlib.tests.TextTestResult(
1395
self._log_file, descriptions=0, verbosity=1)
1397
def inner_test(self):
1398
# the inner child test
1401
def outer_child(self):
1402
# the outer child test
1404
self.inner_test = TestTestCase("inner_child")
1405
result = self.make_test_result()
1406
self.inner_test.run(result)
1407
note("outer finish")
1409
def test_trace_nesting(self):
1410
# this tests that each test case nests its trace facility correctly.
1411
# we do this by running a test case manually. That test case (A)
1412
# should setup a new log, log content to it, setup a child case (B),
1413
# which should log independently, then case (A) should log a trailer
1415
# we do two nested children so that we can verify the state of the
1416
# logs after the outer child finishes is correct, which a bad clean
1417
# up routine in tearDown might trigger a fault in our test with only
1418
# one child, we should instead see the bad result inside our test with
1420
# the outer child test
1421
original_trace = bzrlib.trace._trace_file
1422
outer_test = TestTestCase("outer_child")
1423
result = self.make_test_result()
1424
outer_test.run(result)
1425
self.assertEqual(original_trace, bzrlib.trace._trace_file)
1427
def method_that_times_a_bit_twice(self):
1428
# call self.time twice to ensure it aggregates
1429
self.time(time.sleep, 0.007)
1430
self.time(time.sleep, 0.007)
1432
def test_time_creates_benchmark_in_result(self):
1433
"""Test that the TestCase.time() method accumulates a benchmark time."""
1434
sample_test = TestTestCase("method_that_times_a_bit_twice")
1435
output_stream = StringIO()
1436
result = bzrlib.tests.VerboseTestResult(
1437
unittest._WritelnDecorator(output_stream),
1440
num_tests=sample_test.countTestCases())
1441
sample_test.run(result)
1442
self.assertContainsRe(
1443
output_stream.getvalue(),
1444
r"\d+ms/ +\d+ms\n$")
1446
def test_hooks_sanitised(self):
1447
"""The bzrlib hooks should be sanitised by setUp."""
1448
# Note this test won't fail with hooks that the core library doesn't
1449
# use - but it trigger with a plugin that adds hooks, so its still a
1450
# useful warning in that case.
1451
self.assertEqual(bzrlib.branch.BranchHooks(),
1452
bzrlib.branch.Branch.hooks)
1453
self.assertEqual(bzrlib.smart.server.SmartServerHooks(),
1454
bzrlib.smart.server.SmartTCPServer.hooks)
1455
self.assertEqual(bzrlib.commands.CommandHooks(),
1456
bzrlib.commands.Command.hooks)
1458
def test__gather_lsprof_in_benchmarks(self):
1459
"""When _gather_lsprof_in_benchmarks is on, accumulate profile data.
1461
Each self.time() call is individually and separately profiled.
1463
self.requireFeature(test_lsprof.LSProfFeature)
1464
# overrides the class member with an instance member so no cleanup
1466
self._gather_lsprof_in_benchmarks = True
1467
self.time(time.sleep, 0.000)
1468
self.time(time.sleep, 0.003)
1469
self.assertEqual(2, len(self._benchcalls))
1470
self.assertEqual((time.sleep, (0.000,), {}), self._benchcalls[0][0])
1471
self.assertEqual((time.sleep, (0.003,), {}), self._benchcalls[1][0])
1472
self.assertIsInstance(self._benchcalls[0][1], bzrlib.lsprof.Stats)
1473
self.assertIsInstance(self._benchcalls[1][1], bzrlib.lsprof.Stats)
1475
def test_knownFailure(self):
1476
"""Self.knownFailure() should raise a KnownFailure exception."""
1477
self.assertRaises(KnownFailure, self.knownFailure, "A Failure")
1479
def test_requireFeature_available(self):
1480
"""self.requireFeature(available) is a no-op."""
1481
class Available(Feature):
1482
def _probe(self):return True
1483
feature = Available()
1484
self.requireFeature(feature)
1486
def test_requireFeature_unavailable(self):
1487
"""self.requireFeature(unavailable) raises UnavailableFeature."""
1488
class Unavailable(Feature):
1489
def _probe(self):return False
1490
feature = Unavailable()
1491
self.assertRaises(UnavailableFeature, self.requireFeature, feature)
1493
def test_run_no_parameters(self):
1494
test = SampleTestCase('_test_pass')
1497
def test_run_enabled_unittest_result(self):
1498
"""Test we revert to regular behaviour when the test is enabled."""
1499
test = SampleTestCase('_test_pass')
1500
class EnabledFeature(object):
1501
def available(self):
1503
test._test_needs_features = [EnabledFeature()]
1504
result = unittest.TestResult()
1506
self.assertEqual(1, result.testsRun)
1507
self.assertEqual([], result.errors)
1508
self.assertEqual([], result.failures)
1510
def test_run_disabled_unittest_result(self):
1511
"""Test our compatability for disabled tests with unittest results."""
1512
test = SampleTestCase('_test_pass')
1513
class DisabledFeature(object):
1514
def available(self):
1516
test._test_needs_features = [DisabledFeature()]
1517
result = unittest.TestResult()
1519
self.assertEqual(1, result.testsRun)
1520
self.assertEqual([], result.errors)
1521
self.assertEqual([], result.failures)
1523
def test_run_disabled_supporting_result(self):
1524
"""Test disabled tests behaviour with support aware results."""
1525
test = SampleTestCase('_test_pass')
1526
class DisabledFeature(object):
1527
def available(self):
1529
the_feature = DisabledFeature()
1530
test._test_needs_features = [the_feature]
1531
class InstrumentedTestResult(unittest.TestResult):
1533
unittest.TestResult.__init__(self)
1535
def startTest(self, test):
1536
self.calls.append(('startTest', test))
1537
def stopTest(self, test):
1538
self.calls.append(('stopTest', test))
1539
def addNotSupported(self, test, feature):
1540
self.calls.append(('addNotSupported', test, feature))
1541
result = InstrumentedTestResult()
1544
('startTest', test),
1545
('addNotSupported', test, the_feature),
1550
def test_assert_list_raises_on_generator(self):
1551
def generator_which_will_raise():
1552
# This will not raise until after the first yield
1554
raise _TestException()
1556
e = self.assertListRaises(_TestException, generator_which_will_raise)
1557
self.assertIsInstance(e, _TestException)
1559
e = self.assertListRaises(Exception, generator_which_will_raise)
1560
self.assertIsInstance(e, _TestException)
1562
def test_assert_list_raises_on_plain(self):
1563
def plain_exception():
1564
raise _TestException()
1567
e = self.assertListRaises(_TestException, plain_exception)
1568
self.assertIsInstance(e, _TestException)
1570
e = self.assertListRaises(Exception, plain_exception)
1571
self.assertIsInstance(e, _TestException)
1573
def test_assert_list_raises_assert_wrong_exception(self):
1574
class _NotTestException(Exception):
1577
def wrong_exception():
1578
raise _NotTestException()
1580
def wrong_exception_generator():
1583
raise _NotTestException()
1585
# Wrong exceptions are not intercepted
1586
self.assertRaises(_NotTestException,
1587
self.assertListRaises, _TestException, wrong_exception)
1588
self.assertRaises(_NotTestException,
1589
self.assertListRaises, _TestException, wrong_exception_generator)
1591
def test_assert_list_raises_no_exception(self):
1595
def success_generator():
1599
self.assertRaises(AssertionError,
1600
self.assertListRaises, _TestException, success)
1602
self.assertRaises(AssertionError,
1603
self.assertListRaises, _TestException, success_generator)
1606
@symbol_versioning.deprecated_function(zero_eleven)
1607
def sample_deprecated_function():
1608
"""A deprecated function to test applyDeprecated with."""
1612
def sample_undeprecated_function(a_param):
1613
"""A undeprecated function to test applyDeprecated with."""
1616
class ApplyDeprecatedHelper(object):
1617
"""A helper class for ApplyDeprecated tests."""
1619
@symbol_versioning.deprecated_method(zero_eleven)
1620
def sample_deprecated_method(self, param_one):
1621
"""A deprecated method for testing with."""
1624
def sample_normal_method(self):
1625
"""A undeprecated method."""
1627
@symbol_versioning.deprecated_method(zero_ten)
1628
def sample_nested_deprecation(self):
1629
return sample_deprecated_function()
1632
class TestExtraAssertions(TestCase):
1633
"""Tests for new test assertions in bzrlib test suite"""
1635
def test_assert_isinstance(self):
1636
self.assertIsInstance(2, int)
1637
self.assertIsInstance(u'', basestring)
1638
self.assertRaises(AssertionError, self.assertIsInstance, None, int)
1639
self.assertRaises(AssertionError, self.assertIsInstance, 23.3, int)
1641
def test_assertEndsWith(self):
1642
self.assertEndsWith('foo', 'oo')
1643
self.assertRaises(AssertionError, self.assertEndsWith, 'o', 'oo')
1645
def test_applyDeprecated_not_deprecated(self):
1646
sample_object = ApplyDeprecatedHelper()
1647
# calling an undeprecated callable raises an assertion
1648
self.assertRaises(AssertionError, self.applyDeprecated, zero_eleven,
1649
sample_object.sample_normal_method)
1650
self.assertRaises(AssertionError, self.applyDeprecated, zero_eleven,
1651
sample_undeprecated_function, "a param value")
1652
# calling a deprecated callable (function or method) with the wrong
1653
# expected deprecation fails.
1654
self.assertRaises(AssertionError, self.applyDeprecated, zero_ten,
1655
sample_object.sample_deprecated_method, "a param value")
1656
self.assertRaises(AssertionError, self.applyDeprecated, zero_ten,
1657
sample_deprecated_function)
1658
# calling a deprecated callable (function or method) with the right
1659
# expected deprecation returns the functions result.
1660
self.assertEqual("a param value", self.applyDeprecated(zero_eleven,
1661
sample_object.sample_deprecated_method, "a param value"))
1662
self.assertEqual(2, self.applyDeprecated(zero_eleven,
1663
sample_deprecated_function))
1664
# calling a nested deprecation with the wrong deprecation version
1665
# fails even if a deeper nested function was deprecated with the
1667
self.assertRaises(AssertionError, self.applyDeprecated,
1668
zero_eleven, sample_object.sample_nested_deprecation)
1669
# calling a nested deprecation with the right deprecation value
1670
# returns the calls result.
1671
self.assertEqual(2, self.applyDeprecated(zero_ten,
1672
sample_object.sample_nested_deprecation))
1674
def test_callDeprecated(self):
1675
def testfunc(be_deprecated, result=None):
1676
if be_deprecated is True:
1677
symbol_versioning.warn('i am deprecated', DeprecationWarning,
1680
result = self.callDeprecated(['i am deprecated'], testfunc, True)
1681
self.assertIs(None, result)
1682
result = self.callDeprecated([], testfunc, False, 'result')
1683
self.assertEqual('result', result)
1684
self.callDeprecated(['i am deprecated'], testfunc, be_deprecated=True)
1685
self.callDeprecated([], testfunc, be_deprecated=False)
1688
class TestWarningTests(TestCase):
1689
"""Tests for calling methods that raise warnings."""
1691
def test_callCatchWarnings(self):
1693
warnings.warn("this is your last warning")
1695
wlist, result = self.callCatchWarnings(meth, 1, 2)
1696
self.assertEquals(3, result)
1697
# would like just to compare them, but UserWarning doesn't implement
1700
self.assertIsInstance(w0, UserWarning)
1701
self.assertEquals("this is your last warning", str(w0))
1704
class TestConvenienceMakers(TestCaseWithTransport):
1705
"""Test for the make_* convenience functions."""
1707
def test_make_branch_and_tree_with_format(self):
1708
# we should be able to supply a format to make_branch_and_tree
1709
self.make_branch_and_tree('a', format=bzrlib.bzrdir.BzrDirMetaFormat1())
1710
self.make_branch_and_tree('b', format=bzrlib.bzrdir.BzrDirFormat6())
1711
self.assertIsInstance(bzrlib.bzrdir.BzrDir.open('a')._format,
1712
bzrlib.bzrdir.BzrDirMetaFormat1)
1713
self.assertIsInstance(bzrlib.bzrdir.BzrDir.open('b')._format,
1714
bzrlib.bzrdir.BzrDirFormat6)
1716
def test_make_branch_and_memory_tree(self):
1717
# we should be able to get a new branch and a mutable tree from
1718
# TestCaseWithTransport
1719
tree = self.make_branch_and_memory_tree('a')
1720
self.assertIsInstance(tree, bzrlib.memorytree.MemoryTree)
1723
class TestSFTPMakeBranchAndTree(TestCaseWithSFTPServer):
1725
def test_make_tree_for_sftp_branch(self):
1726
"""Transports backed by local directories create local trees."""
1728
tree = self.make_branch_and_tree('t1')
1729
base = tree.bzrdir.root_transport.base
1730
self.failIf(base.startswith('sftp'),
1731
'base %r is on sftp but should be local' % base)
1732
self.assertEquals(tree.bzrdir.root_transport,
1733
tree.branch.bzrdir.root_transport)
1734
self.assertEquals(tree.bzrdir.root_transport,
1735
tree.branch.repository.bzrdir.root_transport)
1738
class TestSelftest(TestCase):
1739
"""Tests of bzrlib.tests.selftest."""
1741
def test_selftest_benchmark_parameter_invokes_test_suite__benchmark__(self):
1744
factory_called.append(True)
1748
self.apply_redirected(out, err, None, bzrlib.tests.selftest,
1749
test_suite_factory=factory)
1750
self.assertEqual([True], factory_called)
1753
class TestKnownFailure(TestCase):
1755
def test_known_failure(self):
1756
"""Check that KnownFailure is defined appropriately."""
1757
# a KnownFailure is an assertion error for compatability with unaware
1759
self.assertIsInstance(KnownFailure(""), AssertionError)
1761
def test_expect_failure(self):
1763
self.expectFailure("Doomed to failure", self.assertTrue, False)
1764
except KnownFailure, e:
1765
self.assertEqual('Doomed to failure', e.args[0])
1767
self.expectFailure("Doomed to failure", self.assertTrue, True)
1768
except AssertionError, e:
1769
self.assertEqual('Unexpected success. Should have failed:'
1770
' Doomed to failure', e.args[0])
1772
self.fail('Assertion not raised')
1775
class TestFeature(TestCase):
1777
def test_caching(self):
1778
"""Feature._probe is called by the feature at most once."""
1779
class InstrumentedFeature(Feature):
1781
Feature.__init__(self)
1784
self.calls.append('_probe')
1786
feature = InstrumentedFeature()
1788
self.assertEqual(['_probe'], feature.calls)
1790
self.assertEqual(['_probe'], feature.calls)
1792
def test_named_str(self):
1793
"""Feature.__str__ should thunk to feature_name()."""
1794
class NamedFeature(Feature):
1795
def feature_name(self):
1797
feature = NamedFeature()
1798
self.assertEqual('symlinks', str(feature))
1800
def test_default_str(self):
1801
"""Feature.__str__ should default to __class__.__name__."""
1802
class NamedFeature(Feature):
1804
feature = NamedFeature()
1805
self.assertEqual('NamedFeature', str(feature))
1808
class TestUnavailableFeature(TestCase):
1810
def test_access_feature(self):
1812
exception = UnavailableFeature(feature)
1813
self.assertIs(feature, exception.args[0])
1816
class TestSelftestFiltering(TestCase):
1819
self.suite = TestUtil.TestSuite()
1820
self.loader = TestUtil.TestLoader()
1821
self.suite.addTest(self.loader.loadTestsFromModuleNames([
1822
'bzrlib.tests.test_selftest']))
1823
self.all_names = _test_ids(self.suite)
1825
def test_condition_id_re(self):
1826
test_name = ('bzrlib.tests.test_selftest.TestSelftestFiltering.'
1827
'test_condition_id_re')
1828
filtered_suite = filter_suite_by_condition(self.suite,
1829
condition_id_re('test_condition_id_re'))
1830
self.assertEqual([test_name], _test_ids(filtered_suite))
1832
def test_condition_id_in_list(self):
1833
test_names = ['bzrlib.tests.test_selftest.TestSelftestFiltering.'
1834
'test_condition_id_in_list']
1835
id_list = tests.TestIdList(test_names)
1836
filtered_suite = filter_suite_by_condition(
1837
self.suite, tests.condition_id_in_list(id_list))
1838
my_pattern = 'TestSelftestFiltering.*test_condition_id_in_list'
1839
re_filtered = filter_suite_by_re(self.suite, my_pattern)
1840
self.assertEqual(_test_ids(re_filtered), _test_ids(filtered_suite))
1842
def test_condition_id_startswith(self):
1843
klass = 'bzrlib.tests.test_selftest.TestSelftestFiltering.'
1844
start1 = klass + 'test_condition_id_starts'
1845
start2 = klass + 'test_condition_id_in'
1846
test_names = [ klass + 'test_condition_id_in_list',
1847
klass + 'test_condition_id_startswith',
1849
filtered_suite = filter_suite_by_condition(
1850
self.suite, tests.condition_id_startswith([start1, start2]))
1851
self.assertEqual(test_names, _test_ids(filtered_suite))
1853
def test_condition_isinstance(self):
1854
filtered_suite = filter_suite_by_condition(self.suite,
1855
condition_isinstance(self.__class__))
1856
class_pattern = 'bzrlib.tests.test_selftest.TestSelftestFiltering.'
1857
re_filtered = filter_suite_by_re(self.suite, class_pattern)
1858
self.assertEqual(_test_ids(re_filtered), _test_ids(filtered_suite))
1860
def test_exclude_tests_by_condition(self):
1861
excluded_name = ('bzrlib.tests.test_selftest.TestSelftestFiltering.'
1862
'test_exclude_tests_by_condition')
1863
filtered_suite = exclude_tests_by_condition(self.suite,
1864
lambda x:x.id() == excluded_name)
1865
self.assertEqual(len(self.all_names) - 1,
1866
filtered_suite.countTestCases())
1867
self.assertFalse(excluded_name in _test_ids(filtered_suite))
1868
remaining_names = list(self.all_names)
1869
remaining_names.remove(excluded_name)
1870
self.assertEqual(remaining_names, _test_ids(filtered_suite))
1872
def test_exclude_tests_by_re(self):
1873
self.all_names = _test_ids(self.suite)
1874
filtered_suite = exclude_tests_by_re(self.suite, 'exclude_tests_by_re')
1875
excluded_name = ('bzrlib.tests.test_selftest.TestSelftestFiltering.'
1876
'test_exclude_tests_by_re')
1877
self.assertEqual(len(self.all_names) - 1,
1878
filtered_suite.countTestCases())
1879
self.assertFalse(excluded_name in _test_ids(filtered_suite))
1880
remaining_names = list(self.all_names)
1881
remaining_names.remove(excluded_name)
1882
self.assertEqual(remaining_names, _test_ids(filtered_suite))
1884
def test_filter_suite_by_condition(self):
1885
test_name = ('bzrlib.tests.test_selftest.TestSelftestFiltering.'
1886
'test_filter_suite_by_condition')
1887
filtered_suite = filter_suite_by_condition(self.suite,
1888
lambda x:x.id() == test_name)
1889
self.assertEqual([test_name], _test_ids(filtered_suite))
1891
def test_filter_suite_by_re(self):
1892
filtered_suite = filter_suite_by_re(self.suite, 'test_filter_suite_by_r')
1893
filtered_names = _test_ids(filtered_suite)
1894
self.assertEqual(filtered_names, ['bzrlib.tests.test_selftest.'
1895
'TestSelftestFiltering.test_filter_suite_by_re'])
1897
def test_filter_suite_by_id_list(self):
1898
test_list = ['bzrlib.tests.test_selftest.'
1899
'TestSelftestFiltering.test_filter_suite_by_id_list']
1900
filtered_suite = tests.filter_suite_by_id_list(
1901
self.suite, tests.TestIdList(test_list))
1902
filtered_names = _test_ids(filtered_suite)
1905
['bzrlib.tests.test_selftest.'
1906
'TestSelftestFiltering.test_filter_suite_by_id_list'])
1908
def test_filter_suite_by_id_startswith(self):
1909
# By design this test may fail if another test is added whose name also
1910
# begins with one of the start value used.
1911
klass = 'bzrlib.tests.test_selftest.TestSelftestFiltering.'
1912
start1 = klass + 'test_filter_suite_by_id_starts'
1913
start2 = klass + 'test_filter_suite_by_id_li'
1914
test_list = [klass + 'test_filter_suite_by_id_list',
1915
klass + 'test_filter_suite_by_id_startswith',
1917
filtered_suite = tests.filter_suite_by_id_startswith(
1918
self.suite, [start1, start2])
1921
_test_ids(filtered_suite),
1924
def test_preserve_input(self):
1925
# NB: Surely this is something in the stdlib to do this?
1926
self.assertTrue(self.suite is preserve_input(self.suite))
1927
self.assertTrue("@#$" is preserve_input("@#$"))
1929
def test_randomize_suite(self):
1930
randomized_suite = randomize_suite(self.suite)
1931
# randomizing should not add or remove test names.
1932
self.assertEqual(set(_test_ids(self.suite)),
1933
set(_test_ids(randomized_suite)))
1934
# Technically, this *can* fail, because random.shuffle(list) can be
1935
# equal to list. Trying multiple times just pushes the frequency back.
1936
# As its len(self.all_names)!:1, the failure frequency should be low
1937
# enough to ignore. RBC 20071021.
1938
# It should change the order.
1939
self.assertNotEqual(self.all_names, _test_ids(randomized_suite))
1940
# But not the length. (Possibly redundant with the set test, but not
1942
self.assertEqual(len(self.all_names), len(_test_ids(randomized_suite)))
1944
def test_split_suit_by_condition(self):
1945
self.all_names = _test_ids(self.suite)
1946
condition = condition_id_re('test_filter_suite_by_r')
1947
split_suite = split_suite_by_condition(self.suite, condition)
1948
filtered_name = ('bzrlib.tests.test_selftest.TestSelftestFiltering.'
1949
'test_filter_suite_by_re')
1950
self.assertEqual([filtered_name], _test_ids(split_suite[0]))
1951
self.assertFalse(filtered_name in _test_ids(split_suite[1]))
1952
remaining_names = list(self.all_names)
1953
remaining_names.remove(filtered_name)
1954
self.assertEqual(remaining_names, _test_ids(split_suite[1]))
1956
def test_split_suit_by_re(self):
1957
self.all_names = _test_ids(self.suite)
1958
split_suite = split_suite_by_re(self.suite, 'test_filter_suite_by_r')
1959
filtered_name = ('bzrlib.tests.test_selftest.TestSelftestFiltering.'
1960
'test_filter_suite_by_re')
1961
self.assertEqual([filtered_name], _test_ids(split_suite[0]))
1962
self.assertFalse(filtered_name in _test_ids(split_suite[1]))
1963
remaining_names = list(self.all_names)
1964
remaining_names.remove(filtered_name)
1965
self.assertEqual(remaining_names, _test_ids(split_suite[1]))
1968
class TestCheckInventoryShape(TestCaseWithTransport):
1970
def test_check_inventory_shape(self):
1971
files = ['a', 'b/', 'b/c']
1972
tree = self.make_branch_and_tree('.')
1973
self.build_tree(files)
1977
self.check_inventory_shape(tree.inventory, files)
1982
class TestBlackboxSupport(TestCase):
1983
"""Tests for testsuite blackbox features."""
1985
def test_run_bzr_failure_not_caught(self):
1986
# When we run bzr in blackbox mode, we want any unexpected errors to
1987
# propagate up to the test suite so that it can show the error in the
1988
# usual way, and we won't get a double traceback.
1989
e = self.assertRaises(
1991
self.run_bzr, ['assert-fail'])
1992
# make sure we got the real thing, not an error from somewhere else in
1993
# the test framework
1994
self.assertEquals('always fails', str(e))
1995
# check that there's no traceback in the test log
1996
self.assertNotContainsRe(self._get_log(keep_log_file=True),
1999
def test_run_bzr_user_error_caught(self):
2000
# Running bzr in blackbox mode, normal/expected/user errors should be
2001
# caught in the regular way and turned into an error message plus exit
2003
out, err = self.run_bzr(["log", "/nonexistantpath"], retcode=3)
2004
self.assertEqual(out, '')
2005
self.assertContainsRe(err,
2006
'bzr: ERROR: Not a branch: ".*nonexistantpath/".\n')
2009
class TestTestLoader(TestCase):
2010
"""Tests for the test loader."""
2012
def _get_loader_and_module(self):
2013
"""Gets a TestLoader and a module with one test in it."""
2014
loader = TestUtil.TestLoader()
2016
class Stub(TestCase):
2019
class MyModule(object):
2021
MyModule.a_class = Stub
2023
return loader, module
2025
def test_module_no_load_tests_attribute_loads_classes(self):
2026
loader, module = self._get_loader_and_module()
2027
self.assertEqual(1, loader.loadTestsFromModule(module).countTestCases())
2029
def test_module_load_tests_attribute_gets_called(self):
2030
loader, module = self._get_loader_and_module()
2031
# 'self' is here because we're faking the module with a class. Regular
2032
# load_tests do not need that :)
2033
def load_tests(self, standard_tests, module, loader):
2034
result = loader.suiteClass()
2035
for test in iter_suite_tests(standard_tests):
2036
result.addTests([test, test])
2038
# add a load_tests() method which multiplies the tests from the module.
2039
module.__class__.load_tests = load_tests
2040
self.assertEqual(2, loader.loadTestsFromModule(module).countTestCases())
2042
def test_load_tests_from_module_name_smoke_test(self):
2043
loader = TestUtil.TestLoader()
2044
suite = loader.loadTestsFromModuleName('bzrlib.tests.test_sampler')
2045
self.assertEquals(['bzrlib.tests.test_sampler.DemoTest.test_nothing'],
2048
def test_load_tests_from_module_name_with_bogus_module_name(self):
2049
loader = TestUtil.TestLoader()
2050
self.assertRaises(ImportError, loader.loadTestsFromModuleName, 'bogus')
2053
class TestTestIdList(tests.TestCase):
2055
def _create_id_list(self, test_list):
2056
return tests.TestIdList(test_list)
2058
def _create_suite(self, test_id_list):
2060
class Stub(TestCase):
2064
def _create_test_id(id):
2067
suite = TestUtil.TestSuite()
2068
for id in test_id_list:
2069
t = Stub('test_foo')
2070
t.id = _create_test_id(id)
2074
def _test_ids(self, test_suite):
2075
"""Get the ids for the tests in a test suite."""
2076
return [t.id() for t in iter_suite_tests(test_suite)]
2078
def test_empty_list(self):
2079
id_list = self._create_id_list([])
2080
self.assertEquals({}, id_list.tests)
2081
self.assertEquals({}, id_list.modules)
2083
def test_valid_list(self):
2084
id_list = self._create_id_list(
2085
['mod1.cl1.meth1', 'mod1.cl1.meth2',
2086
'mod1.func1', 'mod1.cl2.meth2',
2088
'mod1.submod2.cl1.meth1', 'mod1.submod2.cl2.meth2',
2090
self.assertTrue(id_list.refers_to('mod1'))
2091
self.assertTrue(id_list.refers_to('mod1.submod1'))
2092
self.assertTrue(id_list.refers_to('mod1.submod2'))
2093
self.assertTrue(id_list.includes('mod1.cl1.meth1'))
2094
self.assertTrue(id_list.includes('mod1.submod1'))
2095
self.assertTrue(id_list.includes('mod1.func1'))
2097
def test_bad_chars_in_params(self):
2098
id_list = self._create_id_list(['mod1.cl1.meth1(xx.yy)'])
2099
self.assertTrue(id_list.refers_to('mod1'))
2100
self.assertTrue(id_list.includes('mod1.cl1.meth1(xx.yy)'))
2102
def test_module_used(self):
2103
id_list = self._create_id_list(['mod.class.meth'])
2104
self.assertTrue(id_list.refers_to('mod'))
2105
self.assertTrue(id_list.refers_to('mod.class'))
2106
self.assertTrue(id_list.refers_to('mod.class.meth'))
2108
def test_test_suite(self):
2109
# This test is slow, so we do a single test with one test in each
2113
'bzrlib.tests.blackbox.test_branch.TestBranch.test_branch',
2114
'bzrlib.tests.test_selftest.TestTestIdList.test_test_suite',
2115
# transport implementations
2116
'bzrlib.tests.test_transport_implementations.TransportTests'
2117
'.test_abspath(LocalURLServer)',
2118
# modules_to_doctest
2119
'bzrlib.timestamp.format_highres_date',
2120
# plugins can't be tested that way since selftest may be run with
2123
suite = tests.test_suite(test_list)
2124
self.assertEquals(test_list, _test_ids(suite))
2126
def test_test_suite_matches_id_list_with_unknown(self):
2127
loader = TestUtil.TestLoader()
2128
suite = loader.loadTestsFromModuleName('bzrlib.tests.test_sampler')
2129
test_list = ['bzrlib.tests.test_sampler.DemoTest.test_nothing',
2131
not_found, duplicates = tests.suite_matches_id_list(suite, test_list)
2132
self.assertEquals(['bogus'], not_found)
2133
self.assertEquals([], duplicates)
2135
def test_suite_matches_id_list_with_duplicates(self):
2136
loader = TestUtil.TestLoader()
2137
suite = loader.loadTestsFromModuleName('bzrlib.tests.test_sampler')
2138
dupes = loader.suiteClass()
2139
for test in iter_suite_tests(suite):
2141
dupes.addTest(test) # Add it again
2143
test_list = ['bzrlib.tests.test_sampler.DemoTest.test_nothing',]
2144
not_found, duplicates = tests.suite_matches_id_list(
2146
self.assertEquals([], not_found)
2147
self.assertEquals(['bzrlib.tests.test_sampler.DemoTest.test_nothing'],
2151
class TestLoadTestIdList(tests.TestCaseInTempDir):
2153
def _create_test_list_file(self, file_name, content):
2154
fl = open(file_name, 'wt')
2158
def test_load_unknown(self):
2159
self.assertRaises(errors.NoSuchFile,
2160
tests.load_test_id_list, 'i_do_not_exist')
2162
def test_load_test_list(self):
2163
test_list_fname = 'test.list'
2164
self._create_test_list_file(test_list_fname,
2165
'mod1.cl1.meth1\nmod2.cl2.meth2\n')
2166
tlist = tests.load_test_id_list(test_list_fname)
2167
self.assertEquals(2, len(tlist))
2168
self.assertEquals('mod1.cl1.meth1', tlist[0])
2169
self.assertEquals('mod2.cl2.meth2', tlist[1])
2171
def test_load_dirty_file(self):
2172
test_list_fname = 'test.list'
2173
self._create_test_list_file(test_list_fname,
2174
' mod1.cl1.meth1\n\nmod2.cl2.meth2 \n'
2176
tlist = tests.load_test_id_list(test_list_fname)
2177
self.assertEquals(4, len(tlist))
2178
self.assertEquals('mod1.cl1.meth1', tlist[0])
2179
self.assertEquals('', tlist[1])
2180
self.assertEquals('mod2.cl2.meth2', tlist[2])
2181
self.assertEquals('bar baz', tlist[3])
2184
class TestFilteredByModuleTestLoader(tests.TestCase):
2186
def _create_loader(self, test_list):
2187
id_filter = tests.TestIdList(test_list)
2188
loader = TestUtil.FilteredByModuleTestLoader(id_filter.refers_to)
2191
def test_load_tests(self):
2192
test_list = ['bzrlib.tests.test_sampler.DemoTest.test_nothing']
2193
loader = self._create_loader(test_list)
2195
suite = loader.loadTestsFromModuleName('bzrlib.tests.test_sampler')
2196
self.assertEquals(test_list, _test_ids(suite))
2198
def test_exclude_tests(self):
2199
test_list = ['bogus']
2200
loader = self._create_loader(test_list)
2202
suite = loader.loadTestsFromModuleName('bzrlib.tests.test_sampler')
2203
self.assertEquals([], _test_ids(suite))
2206
class TestFilteredByNameStartTestLoader(tests.TestCase):
2208
def _create_loader(self, name_start):
2209
def needs_module(name):
2210
return name.startswith(name_start) or name_start.startswith(name)
2211
loader = TestUtil.FilteredByModuleTestLoader(needs_module)
2214
def test_load_tests(self):
2215
test_list = ['bzrlib.tests.test_sampler.DemoTest.test_nothing']
2216
loader = self._create_loader('bzrlib.tests.test_samp')
2218
suite = loader.loadTestsFromModuleName('bzrlib.tests.test_sampler')
2219
self.assertEquals(test_list, _test_ids(suite))
2221
def test_load_tests_inside_module(self):
2222
test_list = ['bzrlib.tests.test_sampler.DemoTest.test_nothing']
2223
loader = self._create_loader('bzrlib.tests.test_sampler.Demo')
2225
suite = loader.loadTestsFromModuleName('bzrlib.tests.test_sampler')
2226
self.assertEquals(test_list, _test_ids(suite))
2228
def test_exclude_tests(self):
2229
test_list = ['bogus']
2230
loader = self._create_loader('bogus')
2232
suite = loader.loadTestsFromModuleName('bzrlib.tests.test_sampler')
2233
self.assertEquals([], _test_ids(suite))
2236
class TestTestPrefixRegistry(tests.TestCase):
2238
def _get_registry(self):
2239
tp_registry = tests.TestPrefixAliasRegistry()
2242
def test_register_new_prefix(self):
2243
tpr = self._get_registry()
2244
tpr.register('foo', 'fff.ooo.ooo')
2245
self.assertEquals('fff.ooo.ooo', tpr.get('foo'))
2247
def test_register_existing_prefix(self):
2248
tpr = self._get_registry()
2249
tpr.register('bar', 'bbb.aaa.rrr')
2250
tpr.register('bar', 'bBB.aAA.rRR')
2251
self.assertEquals('bbb.aaa.rrr', tpr.get('bar'))
2252
self.assertContainsRe(self._get_log(keep_log_file=True),
2253
r'.*bar.*bbb.aaa.rrr.*bBB.aAA.rRR')
2255
def test_get_unknown_prefix(self):
2256
tpr = self._get_registry()
2257
self.assertRaises(KeyError, tpr.get, 'I am not a prefix')
2259
def test_resolve_prefix(self):
2260
tpr = self._get_registry()
2261
tpr.register('bar', 'bb.aa.rr')
2262
self.assertEquals('bb.aa.rr', tpr.resolve_alias('bar'))
2264
def test_resolve_unknown_alias(self):
2265
tpr = self._get_registry()
2266
self.assertRaises(errors.BzrCommandError,
2267
tpr.resolve_alias, 'I am not a prefix')
2269
def test_predefined_prefixes(self):
2270
tpr = tests.test_prefix_alias_registry
2271
self.assertEquals('bzrlib', tpr.resolve_alias('bzrlib'))
2272
self.assertEquals('bzrlib.doc', tpr.resolve_alias('bd'))
2273
self.assertEquals('bzrlib.utils', tpr.resolve_alias('bu'))
2274
self.assertEquals('bzrlib.tests', tpr.resolve_alias('bt'))
2275
self.assertEquals('bzrlib.tests.blackbox', tpr.resolve_alias('bb'))
2276
self.assertEquals('bzrlib.plugins', tpr.resolve_alias('bp'))
2279
class TestRunSuite(TestCase):
2281
def test_runner_class(self):
2282
"""run_suite accepts and uses a runner_class keyword argument."""
2283
class Stub(TestCase):
2286
suite = Stub("test_foo")
2288
class MyRunner(TextTestRunner):
2289
def run(self, test):
2291
return ExtendedTestResult(self.stream, self.descriptions,
2293
run_suite(suite, runner_class=MyRunner)
2294
self.assertEqual(calls, [suite])