1
# Copyright (C) 2005, 2006, 2007 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
37
from bzrlib.progress import _BaseProgressBar
38
from bzrlib.repofmt import weaverepo
39
from bzrlib.symbol_versioning import (
44
from bzrlib.tests import (
51
TestCaseWithMemoryTransport,
52
TestCaseWithTransport,
61
exclude_tests_by_condition,
63
filter_suite_by_condition,
68
split_suite_by_condition,
73
from bzrlib.tests.test_sftp_transport import TestCaseWithSFTPServer
74
from bzrlib.tests.TestUtil import _load_module_by_name
75
from bzrlib.trace import note
76
from bzrlib.transport.memory import MemoryServer, MemoryTransport
77
from bzrlib.version import _get_bzr_source_tree
80
def _test_ids(test_suite):
81
"""Get the ids for the tests in a test suite."""
82
return [t.id() for t in iter_suite_tests(test_suite)]
85
class SelftestTests(TestCase):
87
def test_import_tests(self):
88
mod = _load_module_by_name('bzrlib.tests.test_selftest')
89
self.assertEqual(mod.SelftestTests, SelftestTests)
91
def test_import_test_failure(self):
92
self.assertRaises(ImportError,
96
class MetaTestLog(TestCase):
98
def test_logging(self):
99
"""Test logs are captured when a test fails."""
100
self.log('a test message')
101
self._log_file.flush()
102
self.assertContainsRe(self._get_log(keep_log_file=True),
106
class TestTreeShape(TestCaseInTempDir):
108
def test_unicode_paths(self):
109
filename = u'hell\u00d8'
111
self.build_tree_contents([(filename, 'contents of hello')])
112
except UnicodeEncodeError:
113
raise TestSkipped("can't build unicode working tree in "
114
"filesystem encoding %s" % sys.getfilesystemencoding())
115
self.failUnlessExists(filename)
118
class TestTransportProviderAdapter(TestCase):
119
"""A group of tests that test the transport implementation adaption core.
121
This is a meta test that the tests are applied to all available
124
This will be generalised in the future which is why it is in this
125
test file even though it is specific to transport tests at the moment.
128
def test_get_transport_permutations(self):
129
# this checks that get_test_permutations defined by the module is
130
# called by the adapter get_transport_test_permutations method.
131
class MockModule(object):
132
def get_test_permutations(self):
133
return sample_permutation
134
sample_permutation = [(1,2), (3,4)]
135
from bzrlib.tests.test_transport_implementations \
136
import TransportTestProviderAdapter
137
adapter = TransportTestProviderAdapter()
138
self.assertEqual(sample_permutation,
139
adapter.get_transport_test_permutations(MockModule()))
141
def test_adapter_checks_all_modules(self):
142
# this checks that the adapter returns as many permutations as there
143
# are in all the registered transport modules - we assume if this
144
# matches its probably doing the right thing especially in combination
145
# with the tests for setting the right classes below.
146
from bzrlib.tests.test_transport_implementations \
147
import TransportTestProviderAdapter
148
from bzrlib.transport import _get_transport_modules
149
modules = _get_transport_modules()
150
permutation_count = 0
151
for module in modules:
153
permutation_count += len(reduce(getattr,
154
(module + ".get_test_permutations").split('.')[1:],
155
__import__(module))())
156
except errors.DependencyNotPresent:
158
input_test = TestTransportProviderAdapter(
159
"test_adapter_sets_transport_class")
160
adapter = TransportTestProviderAdapter()
161
self.assertEqual(permutation_count,
162
len(list(iter(adapter.adapt(input_test)))))
164
def test_adapter_sets_transport_class(self):
165
# Check that the test adapter inserts a transport and server into the
168
# This test used to know about all the possible transports and the
169
# order they were returned but that seems overly brittle (mbp
171
from bzrlib.tests.test_transport_implementations \
172
import TransportTestProviderAdapter
173
scenarios = TransportTestProviderAdapter().scenarios
174
# there are at least that many builtin transports
175
self.assertTrue(len(scenarios) > 6)
176
one_scenario = scenarios[0]
177
self.assertIsInstance(one_scenario[0], str)
178
self.assertTrue(issubclass(one_scenario[1]["transport_class"],
179
bzrlib.transport.Transport))
180
self.assertTrue(issubclass(one_scenario[1]["transport_server"],
181
bzrlib.transport.Server))
184
class TestBranchProviderAdapter(TestCase):
185
"""A group of tests that test the branch implementation test adapter."""
187
def test_constructor(self):
188
# check that constructor parameters are passed through to the adapted
190
from bzrlib.tests.branch_implementations import BranchTestProviderAdapter
193
formats = [("c", "C"), ("d", "D")]
194
adapter = BranchTestProviderAdapter(server1, server2, formats)
195
self.assertEqual(2, len(adapter.scenarios))
198
{'branch_format': 'c',
199
'bzrdir_format': 'C',
200
'transport_readonly_server': 'b',
201
'transport_server': 'a'}),
203
{'branch_format': 'd',
204
'bzrdir_format': 'D',
205
'transport_readonly_server': 'b',
206
'transport_server': 'a'})],
210
class TestBzrDirProviderAdapter(TestCase):
211
"""A group of tests that test the bzr dir implementation test adapter."""
213
def test_adapted_tests(self):
214
# check that constructor parameters are passed through to the adapted
216
from bzrlib.tests.bzrdir_implementations import BzrDirTestProviderAdapter
221
adapter = BzrDirTestProviderAdapter(vfs_factory,
222
server1, server2, formats)
225
{'bzrdir_format': 'c',
226
'transport_readonly_server': 'b',
227
'transport_server': 'a',
228
'vfs_transport_factory': 'v'}),
230
{'bzrdir_format': 'd',
231
'transport_readonly_server': 'b',
232
'transport_server': 'a',
233
'vfs_transport_factory': 'v'})],
237
class TestRepositoryProviderAdapter(TestCase):
238
"""A group of tests that test the repository implementation test adapter."""
240
def test_constructor(self):
241
# check that constructor parameters are passed through to the
243
from bzrlib.tests.repository_implementations import RepositoryTestProviderAdapter
246
formats = [("c", "C"), ("d", "D")]
247
adapter = RepositoryTestProviderAdapter(server1, server2, formats)
250
{'bzrdir_format': 'C',
251
'repository_format': 'c',
252
'transport_readonly_server': 'b',
253
'transport_server': 'a'}),
255
{'bzrdir_format': 'D',
256
'repository_format': 'd',
257
'transport_readonly_server': 'b',
258
'transport_server': 'a'})],
261
def test_setting_vfs_transport(self):
262
"""The vfs_transport_factory can be set optionally."""
263
from bzrlib.tests.repository_implementations import RepositoryTestProviderAdapter
264
formats = [("a", "b"), ("c", "d")]
265
adapter = RepositoryTestProviderAdapter(None, None, formats,
266
vfs_transport_factory="vfs")
269
{'bzrdir_format': 'b',
270
'repository_format': 'a',
271
'transport_readonly_server': None,
272
'transport_server': None,
273
'vfs_transport_factory': 'vfs'}),
275
{'bzrdir_format': 'd',
276
'repository_format': 'c',
277
'transport_readonly_server': None,
278
'transport_server': None,
279
'vfs_transport_factory': 'vfs'})],
282
def test_formats_to_scenarios(self):
283
"""The adapter can generate all the scenarios needed."""
284
from bzrlib.tests.repository_implementations import RepositoryTestProviderAdapter
285
no_vfs_adapter = RepositoryTestProviderAdapter("server", "readonly",
287
vfs_adapter = RepositoryTestProviderAdapter("server", "readonly",
288
[], vfs_transport_factory="vfs")
289
# no_vfs generate scenarios without vfs_transport_factor
290
formats = [("c", "C"), (1, "D")]
293
{'bzrdir_format': 'C',
294
'repository_format': 'c',
295
'transport_readonly_server': 'readonly',
296
'transport_server': 'server'}),
298
{'bzrdir_format': 'D',
299
'repository_format': 1,
300
'transport_readonly_server': 'readonly',
301
'transport_server': 'server'})],
302
no_vfs_adapter.formats_to_scenarios(formats))
305
{'bzrdir_format': 'C',
306
'repository_format': 'c',
307
'transport_readonly_server': 'readonly',
308
'transport_server': 'server',
309
'vfs_transport_factory': 'vfs'}),
311
{'bzrdir_format': 'D',
312
'repository_format': 1,
313
'transport_readonly_server': 'readonly',
314
'transport_server': 'server',
315
'vfs_transport_factory': 'vfs'})],
316
vfs_adapter.formats_to_scenarios(formats))
319
class TestTestScenarioApplier(TestCase):
320
"""Tests for the test adaption facilities."""
322
def test_adapt_applies_scenarios(self):
323
from bzrlib.tests.repository_implementations import TestScenarioApplier
324
input_test = TestTestScenarioApplier("test_adapt_test_to_scenario")
325
adapter = TestScenarioApplier()
326
adapter.scenarios = [("1", "dict"), ("2", "settings")]
328
def capture_call(test, scenario):
329
calls.append((test, scenario))
331
adapter.adapt_test_to_scenario = capture_call
332
adapter.adapt(input_test)
333
self.assertEqual([(input_test, ("1", "dict")),
334
(input_test, ("2", "settings"))], calls)
336
def test_adapt_test_to_scenario(self):
337
from bzrlib.tests.repository_implementations import TestScenarioApplier
338
input_test = TestTestScenarioApplier("test_adapt_test_to_scenario")
339
adapter = TestScenarioApplier()
340
# setup two adapted tests
341
adapted_test1 = adapter.adapt_test_to_scenario(input_test,
343
{"bzrdir_format":"bzr_format",
344
"repository_format":"repo_fmt",
345
"transport_server":"transport_server",
346
"transport_readonly_server":"readonly-server"}))
347
adapted_test2 = adapter.adapt_test_to_scenario(input_test,
348
("new id 2", {"bzrdir_format":None}))
349
# input_test should have been altered.
350
self.assertRaises(AttributeError, getattr, input_test, "bzrdir_format")
351
# the new tests are mutually incompatible, ensuring it has
352
# made new ones, and unspecified elements in the scenario
353
# should not have been altered.
354
self.assertEqual("bzr_format", adapted_test1.bzrdir_format)
355
self.assertEqual("repo_fmt", adapted_test1.repository_format)
356
self.assertEqual("transport_server", adapted_test1.transport_server)
357
self.assertEqual("readonly-server",
358
adapted_test1.transport_readonly_server)
360
"bzrlib.tests.test_selftest.TestTestScenarioApplier."
361
"test_adapt_test_to_scenario(new id)",
363
self.assertEqual(None, adapted_test2.bzrdir_format)
365
"bzrlib.tests.test_selftest.TestTestScenarioApplier."
366
"test_adapt_test_to_scenario(new id 2)",
370
class TestInterRepositoryProviderAdapter(TestCase):
371
"""A group of tests that test the InterRepository test adapter."""
373
def test_adapted_tests(self):
374
# check that constructor parameters are passed through to the adapted
376
from bzrlib.tests.interrepository_implementations import \
377
InterRepositoryTestProviderAdapter
380
formats = [(str, "C1", "C2"), (int, "D1", "D2")]
381
adapter = InterRepositoryTestProviderAdapter(server1, server2, formats)
384
{'interrepo_class': str,
385
'repository_format': 'C1',
386
'repository_format_to': 'C2',
387
'transport_readonly_server': 'b',
388
'transport_server': 'a'}),
390
{'interrepo_class': int,
391
'repository_format': 'D1',
392
'repository_format_to': 'D2',
393
'transport_readonly_server': 'b',
394
'transport_server': 'a'})],
395
adapter.formats_to_scenarios(formats))
398
class TestInterVersionedFileProviderAdapter(TestCase):
399
"""A group of tests that test the InterVersionedFile test adapter."""
401
def test_scenarios(self):
402
# check that constructor parameters are passed through to the adapted
404
from bzrlib.tests.interversionedfile_implementations \
405
import InterVersionedFileTestProviderAdapter
408
formats = [(str, "C1", "C2"), (int, "D1", "D2")]
409
adapter = InterVersionedFileTestProviderAdapter(server1, server2, formats)
412
{'interversionedfile_class':str,
413
'transport_readonly_server': 'b',
414
'transport_server': 'a',
415
'versionedfile_factory': 'C1',
416
'versionedfile_factory_to': 'C2'}),
418
{'interversionedfile_class': int,
419
'transport_readonly_server': 'b',
420
'transport_server': 'a',
421
'versionedfile_factory': 'D1',
422
'versionedfile_factory_to': 'D2'})],
426
class TestRevisionStoreProviderAdapter(TestCase):
427
"""A group of tests that test the RevisionStore test adapter."""
429
def test_scenarios(self):
430
# check that constructor parameters are passed through to the adapted
432
from bzrlib.tests.revisionstore_implementations \
433
import RevisionStoreTestProviderAdapter
434
# revision stores need a store factory - i.e. RevisionKnit
435
#, a readonly and rw transport
439
store_factories = ["c", "d"]
440
adapter = RevisionStoreTestProviderAdapter(server1, server2, store_factories)
443
{'store_factory': 'c',
444
'transport_readonly_server': 'b',
445
'transport_server': 'a'}),
447
{'store_factory': 'd',
448
'transport_readonly_server': 'b',
449
'transport_server': 'a'})],
453
class TestWorkingTreeProviderAdapter(TestCase):
454
"""A group of tests that test the workingtree implementation test adapter."""
456
def test_scenarios(self):
457
# check that constructor parameters are passed through to the adapted
459
from bzrlib.tests.workingtree_implementations \
460
import WorkingTreeTestProviderAdapter
463
formats = [("c", "C"), ("d", "D")]
464
adapter = WorkingTreeTestProviderAdapter(server1, server2, formats)
467
{'bzrdir_format': 'C',
468
'transport_readonly_server': 'b',
469
'transport_server': 'a',
470
'workingtree_format': 'c'}),
472
{'bzrdir_format': 'D',
473
'transport_readonly_server': 'b',
474
'transport_server': 'a',
475
'workingtree_format': 'd'})],
479
class TestTreeProviderAdapter(TestCase):
480
"""Test the setup of tree_implementation tests."""
482
def test_adapted_tests(self):
483
# the tree implementation adapter is meant to setup one instance for
484
# each working tree format, and one additional instance that will
485
# use the default wt format, but create a revision tree for the tests.
486
# this means that the wt ones should have the workingtree_to_test_tree
487
# attribute set to 'return_parameter' and the revision one set to
488
# revision_tree_from_workingtree.
490
from bzrlib.tests.tree_implementations import (
491
TreeTestProviderAdapter,
493
revision_tree_from_workingtree
495
from bzrlib.workingtree import WorkingTreeFormat, WorkingTreeFormat3
496
input_test = TestTreeProviderAdapter(
497
"test_adapted_tests")
500
formats = [("c", "C"), ("d", "D")]
501
adapter = TreeTestProviderAdapter(server1, server2, formats)
502
suite = adapter.adapt(input_test)
503
tests = list(iter(suite))
504
# XXX We should not have tests fail as we add more scenarios
506
self.assertEqual(5, len(tests))
507
# this must match the default format setp up in
508
# TreeTestProviderAdapter.adapt
509
default_format = WorkingTreeFormat3
510
self.assertEqual(tests[0].workingtree_format, formats[0][0])
511
self.assertEqual(tests[0].bzrdir_format, formats[0][1])
512
self.assertEqual(tests[0].transport_server, server1)
513
self.assertEqual(tests[0].transport_readonly_server, server2)
514
self.assertEqual(tests[0]._workingtree_to_test_tree, return_parameter)
515
self.assertEqual(tests[1].workingtree_format, formats[1][0])
516
self.assertEqual(tests[1].bzrdir_format, formats[1][1])
517
self.assertEqual(tests[1].transport_server, server1)
518
self.assertEqual(tests[1].transport_readonly_server, server2)
519
self.assertEqual(tests[1]._workingtree_to_test_tree, return_parameter)
520
self.assertIsInstance(tests[2].workingtree_format, default_format)
521
#self.assertEqual(tests[2].bzrdir_format,
522
# default_format._matchingbzrdir)
523
self.assertEqual(tests[2].transport_server, server1)
524
self.assertEqual(tests[2].transport_readonly_server, server2)
525
self.assertEqual(tests[2]._workingtree_to_test_tree,
526
revision_tree_from_workingtree)
529
class TestInterTreeProviderAdapter(TestCase):
530
"""A group of tests that test the InterTreeTestAdapter."""
532
def test_adapted_tests(self):
533
# check that constructor parameters are passed through to the adapted
535
# for InterTree tests we want the machinery to bring up two trees in
536
# each instance: the base one, and the one we are interacting with.
537
# because each optimiser can be direction specific, we need to test
538
# each optimiser in its chosen direction.
539
# unlike the TestProviderAdapter we dont want to automatically add a
540
# parameterized one for WorkingTree - the optimisers will tell us what
542
from bzrlib.tests.tree_implementations import (
544
revision_tree_from_workingtree
546
from bzrlib.tests.intertree_implementations import (
547
InterTreeTestProviderAdapter,
549
from bzrlib.workingtree import WorkingTreeFormat2, WorkingTreeFormat3
550
input_test = TestInterTreeProviderAdapter(
551
"test_adapted_tests")
554
format1 = WorkingTreeFormat2()
555
format2 = WorkingTreeFormat3()
556
formats = [(str, format1, format2, "converter1"),
557
(int, format2, format1, "converter2")]
558
adapter = InterTreeTestProviderAdapter(server1, server2, formats)
559
suite = adapter.adapt(input_test)
560
tests = list(iter(suite))
561
self.assertEqual(2, len(tests))
562
self.assertEqual(tests[0].intertree_class, formats[0][0])
563
self.assertEqual(tests[0].workingtree_format, formats[0][1])
564
self.assertEqual(tests[0].workingtree_format_to, formats[0][2])
565
self.assertEqual(tests[0].mutable_trees_to_test_trees, formats[0][3])
566
self.assertEqual(tests[0]._workingtree_to_test_tree, return_parameter)
567
self.assertEqual(tests[0].transport_server, server1)
568
self.assertEqual(tests[0].transport_readonly_server, server2)
569
self.assertEqual(tests[1].intertree_class, formats[1][0])
570
self.assertEqual(tests[1].workingtree_format, formats[1][1])
571
self.assertEqual(tests[1].workingtree_format_to, formats[1][2])
572
self.assertEqual(tests[1].mutable_trees_to_test_trees, formats[1][3])
573
self.assertEqual(tests[1]._workingtree_to_test_tree, return_parameter)
574
self.assertEqual(tests[1].transport_server, server1)
575
self.assertEqual(tests[1].transport_readonly_server, server2)
578
class TestTestCaseInTempDir(TestCaseInTempDir):
580
def test_home_is_not_working(self):
581
self.assertNotEqual(self.test_dir, self.test_home_dir)
582
cwd = osutils.getcwd()
583
self.assertIsSameRealPath(self.test_dir, cwd)
584
self.assertIsSameRealPath(self.test_home_dir, os.environ['HOME'])
587
class TestTestCaseWithMemoryTransport(TestCaseWithMemoryTransport):
589
def test_home_is_non_existant_dir_under_root(self):
590
"""The test_home_dir for TestCaseWithMemoryTransport is missing.
592
This is because TestCaseWithMemoryTransport is for tests that do not
593
need any disk resources: they should be hooked into bzrlib in such a
594
way that no global settings are being changed by the test (only a
595
few tests should need to do that), and having a missing dir as home is
596
an effective way to ensure that this is the case.
598
self.assertIsSameRealPath(
599
self.TEST_ROOT + "/MemoryTransportMissingHomeDir",
601
self.assertIsSameRealPath(self.test_home_dir, os.environ['HOME'])
603
def test_cwd_is_TEST_ROOT(self):
604
self.assertIsSameRealPath(self.test_dir, self.TEST_ROOT)
605
cwd = osutils.getcwd()
606
self.assertIsSameRealPath(self.test_dir, cwd)
608
def test_make_branch_and_memory_tree(self):
609
"""In TestCaseWithMemoryTransport we should not make the branch on disk.
611
This is hard to comprehensively robustly test, so we settle for making
612
a branch and checking no directory was created at its relpath.
614
tree = self.make_branch_and_memory_tree('dir')
615
# Guard against regression into MemoryTransport leaking
616
# files to disk instead of keeping them in memory.
617
self.failIf(osutils.lexists('dir'))
618
self.assertIsInstance(tree, memorytree.MemoryTree)
620
def test_make_branch_and_memory_tree_with_format(self):
621
"""make_branch_and_memory_tree should accept a format option."""
622
format = bzrdir.BzrDirMetaFormat1()
623
format.repository_format = weaverepo.RepositoryFormat7()
624
tree = self.make_branch_and_memory_tree('dir', format=format)
625
# Guard against regression into MemoryTransport leaking
626
# files to disk instead of keeping them in memory.
627
self.failIf(osutils.lexists('dir'))
628
self.assertIsInstance(tree, memorytree.MemoryTree)
629
self.assertEqual(format.repository_format.__class__,
630
tree.branch.repository._format.__class__)
632
def test_safety_net(self):
633
"""No test should modify the safety .bzr directory.
635
We just test that the _check_safety_net private method raises
636
AssertionError, it's easier than building a test suite with the same
639
# Oops, a commit in the current directory (i.e. without local .bzr
640
# directory) will crawl up the hierarchy to find a .bzr directory.
641
self.run_bzr(['commit', '-mfoo', '--unchanged'])
642
# But we have a safety net in place.
643
self.assertRaises(AssertionError, self._check_safety_net)
646
class TestTestCaseWithTransport(TestCaseWithTransport):
647
"""Tests for the convenience functions TestCaseWithTransport introduces."""
649
def test_get_readonly_url_none(self):
650
from bzrlib.transport import get_transport
651
from bzrlib.transport.memory import MemoryServer
652
from bzrlib.transport.readonly import ReadonlyTransportDecorator
653
self.vfs_transport_factory = MemoryServer
654
self.transport_readonly_server = None
655
# calling get_readonly_transport() constructs a decorator on the url
657
url = self.get_readonly_url()
658
url2 = self.get_readonly_url('foo/bar')
659
t = get_transport(url)
660
t2 = get_transport(url2)
661
self.failUnless(isinstance(t, ReadonlyTransportDecorator))
662
self.failUnless(isinstance(t2, ReadonlyTransportDecorator))
663
self.assertEqual(t2.base[:-1], t.abspath('foo/bar'))
665
def test_get_readonly_url_http(self):
666
from bzrlib.tests.http_server import HttpServer
667
from bzrlib.transport import get_transport
668
from bzrlib.transport.local import LocalURLServer
669
from bzrlib.transport.http import HttpTransportBase
670
self.transport_server = LocalURLServer
671
self.transport_readonly_server = HttpServer
672
# calling get_readonly_transport() gives us a HTTP server instance.
673
url = self.get_readonly_url()
674
url2 = self.get_readonly_url('foo/bar')
675
# the transport returned may be any HttpTransportBase subclass
676
t = get_transport(url)
677
t2 = get_transport(url2)
678
self.failUnless(isinstance(t, HttpTransportBase))
679
self.failUnless(isinstance(t2, HttpTransportBase))
680
self.assertEqual(t2.base[:-1], t.abspath('foo/bar'))
682
def test_is_directory(self):
683
"""Test assertIsDirectory assertion"""
684
t = self.get_transport()
685
self.build_tree(['a_dir/', 'a_file'], transport=t)
686
self.assertIsDirectory('a_dir', t)
687
self.assertRaises(AssertionError, self.assertIsDirectory, 'a_file', t)
688
self.assertRaises(AssertionError, self.assertIsDirectory, 'not_here', t)
691
class TestTestCaseTransports(TestCaseWithTransport):
694
super(TestTestCaseTransports, self).setUp()
695
self.vfs_transport_factory = MemoryServer
697
def test_make_bzrdir_preserves_transport(self):
698
t = self.get_transport()
699
result_bzrdir = self.make_bzrdir('subdir')
700
self.assertIsInstance(result_bzrdir.transport,
702
# should not be on disk, should only be in memory
703
self.failIfExists('subdir')
706
class TestChrootedTest(ChrootedTestCase):
708
def test_root_is_root(self):
709
from bzrlib.transport import get_transport
710
t = get_transport(self.get_readonly_url())
712
self.assertEqual(url, t.clone('..').base)
715
class MockProgress(_BaseProgressBar):
716
"""Progress-bar standin that records calls.
718
Useful for testing pb using code.
722
_BaseProgressBar.__init__(self)
726
self.calls.append(('tick',))
728
def update(self, msg=None, current=None, total=None):
729
self.calls.append(('update', msg, current, total))
732
self.calls.append(('clear',))
734
def note(self, msg, *args):
735
self.calls.append(('note', msg, args))
738
class TestTestResult(TestCase):
740
def check_timing(self, test_case, expected_re):
741
result = bzrlib.tests.TextTestResult(self._log_file,
745
test_case.run(result)
746
timed_string = result._testTimeString(test_case)
747
self.assertContainsRe(timed_string, expected_re)
749
def test_test_reporting(self):
750
class ShortDelayTestCase(TestCase):
751
def test_short_delay(self):
753
def test_short_benchmark(self):
754
self.time(time.sleep, 0.003)
755
self.check_timing(ShortDelayTestCase('test_short_delay'),
757
# if a benchmark time is given, we want a x of y style result.
758
self.check_timing(ShortDelayTestCase('test_short_benchmark'),
759
r"^ +[0-9]+ms/ +[0-9]+ms$")
761
def test_unittest_reporting_unittest_class(self):
762
# getting the time from a non-bzrlib test works ok
763
class ShortDelayTestCase(unittest.TestCase):
764
def test_short_delay(self):
766
self.check_timing(ShortDelayTestCase('test_short_delay'),
769
def test_assigned_benchmark_file_stores_date(self):
771
result = bzrlib.tests.TextTestResult(self._log_file,
776
output_string = output.getvalue()
777
# if you are wondering about the regexp please read the comment in
778
# test_bench_history (bzrlib.tests.test_selftest.TestRunner)
779
# XXX: what comment? -- Andrew Bennetts
780
self.assertContainsRe(output_string, "--date [0-9.]+")
782
def test_benchhistory_records_test_times(self):
783
result_stream = StringIO()
784
result = bzrlib.tests.TextTestResult(
788
bench_history=result_stream
791
# we want profile a call and check that its test duration is recorded
792
# make a new test instance that when run will generate a benchmark
793
example_test_case = TestTestResult("_time_hello_world_encoding")
794
# execute the test, which should succeed and record times
795
example_test_case.run(result)
796
lines = result_stream.getvalue().splitlines()
797
self.assertEqual(2, len(lines))
798
self.assertContainsRe(lines[1],
799
" *[0-9]+ms bzrlib.tests.test_selftest.TestTestResult"
800
"._time_hello_world_encoding")
802
def _time_hello_world_encoding(self):
803
"""Profile two sleep calls
805
This is used to exercise the test framework.
807
self.time(unicode, 'hello', errors='replace')
808
self.time(unicode, 'world', errors='replace')
810
def test_lsprofiling(self):
811
"""Verbose test result prints lsprof statistics from test cases."""
812
self.requireFeature(test_lsprof.LSProfFeature)
813
result_stream = StringIO()
814
result = bzrlib.tests.VerboseTestResult(
815
unittest._WritelnDecorator(result_stream),
819
# we want profile a call of some sort and check it is output by
820
# addSuccess. We dont care about addError or addFailure as they
821
# are not that interesting for performance tuning.
822
# make a new test instance that when run will generate a profile
823
example_test_case = TestTestResult("_time_hello_world_encoding")
824
example_test_case._gather_lsprof_in_benchmarks = True
825
# execute the test, which should succeed and record profiles
826
example_test_case.run(result)
827
# lsprofile_something()
828
# if this worked we want
829
# LSProf output for <built in function unicode> (['hello'], {'errors': 'replace'})
830
# CallCount Recursive Total(ms) Inline(ms) module:lineno(function)
831
# (the lsprof header)
832
# ... an arbitrary number of lines
833
# and the function call which is time.sleep.
834
# 1 0 ??? ??? ???(sleep)
835
# and then repeated but with 'world', rather than 'hello'.
836
# this should appear in the output stream of our test result.
837
output = result_stream.getvalue()
838
self.assertContainsRe(output,
839
r"LSProf output for <type 'unicode'>\(\('hello',\), {'errors': 'replace'}\)")
840
self.assertContainsRe(output,
841
r" *CallCount *Recursive *Total\(ms\) *Inline\(ms\) *module:lineno\(function\)\n")
842
self.assertContainsRe(output,
843
r"( +1 +0 +0\.\d+ +0\.\d+ +<method 'disable' of '_lsprof\.Profiler' objects>\n)?")
844
self.assertContainsRe(output,
845
r"LSProf output for <type 'unicode'>\(\('world',\), {'errors': 'replace'}\)\n")
847
def test_known_failure(self):
848
"""A KnownFailure being raised should trigger several result actions."""
849
class InstrumentedTestResult(ExtendedTestResult):
851
def report_test_start(self, test): pass
852
def report_known_failure(self, test, err):
853
self._call = test, err
854
result = InstrumentedTestResult(None, None, None, None)
856
raise KnownFailure('failed!')
857
test = unittest.FunctionTestCase(test_function)
859
# it should invoke 'report_known_failure'.
860
self.assertEqual(2, len(result._call))
861
self.assertEqual(test, result._call[0])
862
self.assertEqual(KnownFailure, result._call[1][0])
863
self.assertIsInstance(result._call[1][1], KnownFailure)
864
# we dont introspec the traceback, if the rest is ok, it would be
865
# exceptional for it not to be.
866
# it should update the known_failure_count on the object.
867
self.assertEqual(1, result.known_failure_count)
868
# the result should be successful.
869
self.assertTrue(result.wasSuccessful())
871
def test_verbose_report_known_failure(self):
872
# verbose test output formatting
873
result_stream = StringIO()
874
result = bzrlib.tests.VerboseTestResult(
875
unittest._WritelnDecorator(result_stream),
879
test = self.get_passing_test()
880
result.startTest(test)
881
prefix = len(result_stream.getvalue())
882
# the err parameter has the shape:
883
# (class, exception object, traceback)
884
# KnownFailures dont get their tracebacks shown though, so we
886
err = (KnownFailure, KnownFailure('foo'), None)
887
result.report_known_failure(test, err)
888
output = result_stream.getvalue()[prefix:]
889
lines = output.splitlines()
890
self.assertContainsRe(lines[0], r'XFAIL *\d+ms$')
891
self.assertEqual(lines[1], ' foo')
892
self.assertEqual(2, len(lines))
894
def test_text_report_known_failure(self):
895
# text test output formatting
897
result = bzrlib.tests.TextTestResult(
903
test = self.get_passing_test()
904
# this seeds the state to handle reporting the test.
905
result.startTest(test)
906
# the err parameter has the shape:
907
# (class, exception object, traceback)
908
# KnownFailures dont get their tracebacks shown though, so we
910
err = (KnownFailure, KnownFailure('foo'), None)
911
result.report_known_failure(test, err)
914
('update', '[1 in 0s] passing_test', None, None),
915
('note', 'XFAIL: %s\n%s\n', ('passing_test', err[1]))
918
# known_failures should be printed in the summary, so if we run a test
919
# after there are some known failures, the update prefix should match
921
result.known_failure_count = 3
925
('update', '[2 in 0s] passing_test', None, None),
929
def get_passing_test(self):
930
"""Return a test object that can't be run usefully."""
933
return unittest.FunctionTestCase(passing_test)
935
def test_add_not_supported(self):
936
"""Test the behaviour of invoking addNotSupported."""
937
class InstrumentedTestResult(ExtendedTestResult):
938
def report_test_start(self, test): pass
939
def report_unsupported(self, test, feature):
940
self._call = test, feature
941
result = InstrumentedTestResult(None, None, None, None)
942
test = SampleTestCase('_test_pass')
944
result.startTest(test)
945
result.addNotSupported(test, feature)
946
# it should invoke 'report_unsupported'.
947
self.assertEqual(2, len(result._call))
948
self.assertEqual(test, result._call[0])
949
self.assertEqual(feature, result._call[1])
950
# the result should be successful.
951
self.assertTrue(result.wasSuccessful())
952
# it should record the test against a count of tests not run due to
954
self.assertEqual(1, result.unsupported['Feature'])
955
# and invoking it again should increment that counter
956
result.addNotSupported(test, feature)
957
self.assertEqual(2, result.unsupported['Feature'])
959
def test_verbose_report_unsupported(self):
960
# verbose test output formatting
961
result_stream = StringIO()
962
result = bzrlib.tests.VerboseTestResult(
963
unittest._WritelnDecorator(result_stream),
967
test = self.get_passing_test()
969
result.startTest(test)
970
prefix = len(result_stream.getvalue())
971
result.report_unsupported(test, feature)
972
output = result_stream.getvalue()[prefix:]
973
lines = output.splitlines()
974
self.assertEqual(lines, ['NODEP 0ms', " The feature 'Feature' is not available."])
976
def test_text_report_unsupported(self):
977
# text test output formatting
979
result = bzrlib.tests.TextTestResult(
985
test = self.get_passing_test()
987
# this seeds the state to handle reporting the test.
988
result.startTest(test)
989
result.report_unsupported(test, feature)
990
# no output on unsupported features
992
[('update', '[1 in 0s] passing_test', None, None)
995
# the number of missing features should be printed in the progress
996
# summary, so check for that.
997
result.unsupported = {'foo':0, 'bar':0}
1001
('update', '[2 in 0s, 2 missing] passing_test', None, None),
1005
def test_unavailable_exception(self):
1006
"""An UnavailableFeature being raised should invoke addNotSupported."""
1007
class InstrumentedTestResult(ExtendedTestResult):
1009
def report_test_start(self, test): pass
1010
def addNotSupported(self, test, feature):
1011
self._call = test, feature
1012
result = InstrumentedTestResult(None, None, None, None)
1014
def test_function():
1015
raise UnavailableFeature(feature)
1016
test = unittest.FunctionTestCase(test_function)
1018
# it should invoke 'addNotSupported'.
1019
self.assertEqual(2, len(result._call))
1020
self.assertEqual(test, result._call[0])
1021
self.assertEqual(feature, result._call[1])
1022
# and not count as an error
1023
self.assertEqual(0, result.error_count)
1025
def test_strict_with_unsupported_feature(self):
1026
result = bzrlib.tests.TextTestResult(self._log_file, descriptions=0,
1028
test = self.get_passing_test()
1029
feature = "Unsupported Feature"
1030
result.addNotSupported(test, feature)
1031
self.assertFalse(result.wasStrictlySuccessful())
1032
self.assertEqual(None, result._extractBenchmarkTime(test))
1034
def test_strict_with_known_failure(self):
1035
result = bzrlib.tests.TextTestResult(self._log_file, descriptions=0,
1037
test = self.get_passing_test()
1038
err = (KnownFailure, KnownFailure('foo'), None)
1039
result._addKnownFailure(test, err)
1040
self.assertFalse(result.wasStrictlySuccessful())
1041
self.assertEqual(None, result._extractBenchmarkTime(test))
1043
def test_strict_with_success(self):
1044
result = bzrlib.tests.TextTestResult(self._log_file, descriptions=0,
1046
test = self.get_passing_test()
1047
result.addSuccess(test)
1048
self.assertTrue(result.wasStrictlySuccessful())
1049
self.assertEqual(None, result._extractBenchmarkTime(test))
1052
class TestRunner(TestCase):
1054
def dummy_test(self):
1057
def run_test_runner(self, testrunner, test):
1058
"""Run suite in testrunner, saving global state and restoring it.
1060
This current saves and restores:
1061
TestCaseInTempDir.TEST_ROOT
1063
There should be no tests in this file that use bzrlib.tests.TextTestRunner
1064
without using this convenience method, because of our use of global state.
1066
old_root = TestCaseInTempDir.TEST_ROOT
1068
TestCaseInTempDir.TEST_ROOT = None
1069
return testrunner.run(test)
1071
TestCaseInTempDir.TEST_ROOT = old_root
1073
def test_known_failure_failed_run(self):
1074
# run a test that generates a known failure which should be printed in
1075
# the final output when real failures occur.
1076
def known_failure_test():
1077
raise KnownFailure('failed')
1078
test = unittest.TestSuite()
1079
test.addTest(unittest.FunctionTestCase(known_failure_test))
1081
raise AssertionError('foo')
1082
test.addTest(unittest.FunctionTestCase(failing_test))
1084
runner = TextTestRunner(stream=stream)
1085
result = self.run_test_runner(runner, test)
1086
lines = stream.getvalue().splitlines()
1089
'======================================================================',
1090
'FAIL: unittest.FunctionTestCase (failing_test)',
1091
'----------------------------------------------------------------------',
1092
'Traceback (most recent call last):',
1093
' raise AssertionError(\'foo\')',
1094
'AssertionError: foo',
1096
'----------------------------------------------------------------------',
1098
'FAILED (failures=1, known_failure_count=1)'],
1099
lines[0:5] + lines[6:10] + lines[11:])
1101
def test_known_failure_ok_run(self):
1102
# run a test that generates a known failure which should be printed in the final output.
1103
def known_failure_test():
1104
raise KnownFailure('failed')
1105
test = unittest.FunctionTestCase(known_failure_test)
1107
runner = TextTestRunner(stream=stream)
1108
result = self.run_test_runner(runner, test)
1109
self.assertContainsRe(stream.getvalue(),
1112
'Ran 1 test in .*\n'
1114
'OK \\(known_failures=1\\)\n')
1116
def test_skipped_test(self):
1117
# run a test that is skipped, and check the suite as a whole still
1119
# skipping_test must be hidden in here so it's not run as a real test
1120
def skipping_test():
1121
raise TestSkipped('test intentionally skipped')
1123
runner = TextTestRunner(stream=self._log_file)
1124
test = unittest.FunctionTestCase(skipping_test)
1125
result = self.run_test_runner(runner, test)
1126
self.assertTrue(result.wasSuccessful())
1128
def test_skipped_from_setup(self):
1130
class SkippedSetupTest(TestCase):
1133
calls.append('setUp')
1134
self.addCleanup(self.cleanup)
1135
raise TestSkipped('skipped setup')
1137
def test_skip(self):
1138
self.fail('test reached')
1141
calls.append('cleanup')
1143
runner = TextTestRunner(stream=self._log_file)
1144
test = SkippedSetupTest('test_skip')
1145
result = self.run_test_runner(runner, test)
1146
self.assertTrue(result.wasSuccessful())
1147
# Check if cleanup was called the right number of times.
1148
self.assertEqual(['setUp', 'cleanup'], calls)
1150
def test_skipped_from_test(self):
1152
class SkippedTest(TestCase):
1155
calls.append('setUp')
1156
self.addCleanup(self.cleanup)
1158
def test_skip(self):
1159
raise TestSkipped('skipped test')
1162
calls.append('cleanup')
1164
runner = TextTestRunner(stream=self._log_file)
1165
test = SkippedTest('test_skip')
1166
result = self.run_test_runner(runner, test)
1167
self.assertTrue(result.wasSuccessful())
1168
# Check if cleanup was called the right number of times.
1169
self.assertEqual(['setUp', 'cleanup'], calls)
1171
def test_not_applicable(self):
1172
# run a test that is skipped because it's not applicable
1173
def not_applicable_test():
1174
from bzrlib.tests import TestNotApplicable
1175
raise TestNotApplicable('this test never runs')
1177
runner = TextTestRunner(stream=out, verbosity=2)
1178
test = unittest.FunctionTestCase(not_applicable_test)
1179
result = self.run_test_runner(runner, test)
1180
self._log_file.write(out.getvalue())
1181
self.assertTrue(result.wasSuccessful())
1182
self.assertTrue(result.wasStrictlySuccessful())
1183
self.assertContainsRe(out.getvalue(),
1184
r'(?m)not_applicable_test * N/A')
1185
self.assertContainsRe(out.getvalue(),
1186
r'(?m)^ this test never runs')
1188
def test_not_applicable_demo(self):
1189
# just so you can see it in the test output
1190
raise TestNotApplicable('this test is just a demonstation')
1192
def test_unsupported_features_listed(self):
1193
"""When unsupported features are encountered they are detailed."""
1194
class Feature1(Feature):
1195
def _probe(self): return False
1196
class Feature2(Feature):
1197
def _probe(self): return False
1198
# create sample tests
1199
test1 = SampleTestCase('_test_pass')
1200
test1._test_needs_features = [Feature1()]
1201
test2 = SampleTestCase('_test_pass')
1202
test2._test_needs_features = [Feature2()]
1203
test = unittest.TestSuite()
1207
runner = TextTestRunner(stream=stream)
1208
result = self.run_test_runner(runner, test)
1209
lines = stream.getvalue().splitlines()
1212
"Missing feature 'Feature1' skipped 1 tests.",
1213
"Missing feature 'Feature2' skipped 1 tests.",
1217
def test_bench_history(self):
1218
# tests that the running the benchmark produces a history file
1219
# containing a timestamp and the revision id of the bzrlib source which
1221
workingtree = _get_bzr_source_tree()
1222
test = TestRunner('dummy_test')
1224
runner = TextTestRunner(stream=self._log_file, bench_history=output)
1225
result = self.run_test_runner(runner, test)
1226
output_string = output.getvalue()
1227
self.assertContainsRe(output_string, "--date [0-9.]+")
1228
if workingtree is not None:
1229
revision_id = workingtree.get_parent_ids()[0]
1230
self.assertEndsWith(output_string.rstrip(), revision_id)
1232
def assertLogDeleted(self, test):
1233
log = test._get_log()
1234
self.assertEqual("DELETED log file to reduce memory footprint", log)
1235
self.assertEqual('', test._log_contents)
1236
self.assertIs(None, test._log_file_name)
1238
def test_success_log_deleted(self):
1239
"""Successful tests have their log deleted"""
1241
class LogTester(TestCase):
1243
def test_success(self):
1244
self.log('this will be removed\n')
1246
sio = cStringIO.StringIO()
1247
runner = TextTestRunner(stream=sio)
1248
test = LogTester('test_success')
1249
result = self.run_test_runner(runner, test)
1251
self.assertLogDeleted(test)
1253
def test_skipped_log_deleted(self):
1254
"""Skipped tests have their log deleted"""
1256
class LogTester(TestCase):
1258
def test_skipped(self):
1259
self.log('this will be removed\n')
1260
raise tests.TestSkipped()
1262
sio = cStringIO.StringIO()
1263
runner = TextTestRunner(stream=sio)
1264
test = LogTester('test_skipped')
1265
result = self.run_test_runner(runner, test)
1267
self.assertLogDeleted(test)
1269
def test_not_aplicable_log_deleted(self):
1270
"""Not applicable tests have their log deleted"""
1272
class LogTester(TestCase):
1274
def test_not_applicable(self):
1275
self.log('this will be removed\n')
1276
raise tests.TestNotApplicable()
1278
sio = cStringIO.StringIO()
1279
runner = TextTestRunner(stream=sio)
1280
test = LogTester('test_not_applicable')
1281
result = self.run_test_runner(runner, test)
1283
self.assertLogDeleted(test)
1285
def test_known_failure_log_deleted(self):
1286
"""Know failure tests have their log deleted"""
1288
class LogTester(TestCase):
1290
def test_known_failure(self):
1291
self.log('this will be removed\n')
1292
raise tests.KnownFailure()
1294
sio = cStringIO.StringIO()
1295
runner = TextTestRunner(stream=sio)
1296
test = LogTester('test_known_failure')
1297
result = self.run_test_runner(runner, test)
1299
self.assertLogDeleted(test)
1301
def test_fail_log_kept(self):
1302
"""Failed tests have their log kept"""
1304
class LogTester(TestCase):
1306
def test_fail(self):
1307
self.log('this will be kept\n')
1308
self.fail('this test fails')
1310
sio = cStringIO.StringIO()
1311
runner = TextTestRunner(stream=sio)
1312
test = LogTester('test_fail')
1313
result = self.run_test_runner(runner, test)
1315
text = sio.getvalue()
1316
self.assertContainsRe(text, 'this will be kept')
1317
self.assertContainsRe(text, 'this test fails')
1319
log = test._get_log()
1320
self.assertContainsRe(log, 'this will be kept')
1321
self.assertEqual(log, test._log_contents)
1323
def test_error_log_kept(self):
1324
"""Tests with errors have their log kept"""
1326
class LogTester(TestCase):
1328
def test_error(self):
1329
self.log('this will be kept\n')
1330
raise ValueError('random exception raised')
1332
sio = cStringIO.StringIO()
1333
runner = TextTestRunner(stream=sio)
1334
test = LogTester('test_error')
1335
result = self.run_test_runner(runner, test)
1337
text = sio.getvalue()
1338
self.assertContainsRe(text, 'this will be kept')
1339
self.assertContainsRe(text, 'random exception raised')
1341
log = test._get_log()
1342
self.assertContainsRe(log, 'this will be kept')
1343
self.assertEqual(log, test._log_contents)
1346
class SampleTestCase(TestCase):
1348
def _test_pass(self):
1352
class TestTestCase(TestCase):
1353
"""Tests that test the core bzrlib TestCase."""
1355
def test_debug_flags_sanitised(self):
1356
"""The bzrlib debug flags should be sanitised by setUp."""
1357
# we could set something and run a test that will check
1358
# it gets santised, but this is probably sufficient for now:
1359
# if someone runs the test with -Dsomething it will error.
1360
self.assertEqual(set(), bzrlib.debug.debug_flags)
1362
def inner_test(self):
1363
# the inner child test
1366
def outer_child(self):
1367
# the outer child test
1369
self.inner_test = TestTestCase("inner_child")
1370
result = bzrlib.tests.TextTestResult(self._log_file,
1373
self.inner_test.run(result)
1374
note("outer finish")
1376
def test_trace_nesting(self):
1377
# this tests that each test case nests its trace facility correctly.
1378
# we do this by running a test case manually. That test case (A)
1379
# should setup a new log, log content to it, setup a child case (B),
1380
# which should log independently, then case (A) should log a trailer
1382
# we do two nested children so that we can verify the state of the
1383
# logs after the outer child finishes is correct, which a bad clean
1384
# up routine in tearDown might trigger a fault in our test with only
1385
# one child, we should instead see the bad result inside our test with
1387
# the outer child test
1388
original_trace = bzrlib.trace._trace_file
1389
outer_test = TestTestCase("outer_child")
1390
result = bzrlib.tests.TextTestResult(self._log_file,
1393
outer_test.run(result)
1394
self.assertEqual(original_trace, bzrlib.trace._trace_file)
1396
def method_that_times_a_bit_twice(self):
1397
# call self.time twice to ensure it aggregates
1398
self.time(time.sleep, 0.007)
1399
self.time(time.sleep, 0.007)
1401
def test_time_creates_benchmark_in_result(self):
1402
"""Test that the TestCase.time() method accumulates a benchmark time."""
1403
sample_test = TestTestCase("method_that_times_a_bit_twice")
1404
output_stream = StringIO()
1405
result = bzrlib.tests.VerboseTestResult(
1406
unittest._WritelnDecorator(output_stream),
1409
num_tests=sample_test.countTestCases())
1410
sample_test.run(result)
1411
self.assertContainsRe(
1412
output_stream.getvalue(),
1413
r"\d+ms/ +\d+ms\n$")
1415
def test_hooks_sanitised(self):
1416
"""The bzrlib hooks should be sanitised by setUp."""
1417
self.assertEqual(bzrlib.branch.BranchHooks(),
1418
bzrlib.branch.Branch.hooks)
1419
self.assertEqual(bzrlib.smart.server.SmartServerHooks(),
1420
bzrlib.smart.server.SmartTCPServer.hooks)
1422
def test__gather_lsprof_in_benchmarks(self):
1423
"""When _gather_lsprof_in_benchmarks is on, accumulate profile data.
1425
Each self.time() call is individually and separately profiled.
1427
self.requireFeature(test_lsprof.LSProfFeature)
1428
# overrides the class member with an instance member so no cleanup
1430
self._gather_lsprof_in_benchmarks = True
1431
self.time(time.sleep, 0.000)
1432
self.time(time.sleep, 0.003)
1433
self.assertEqual(2, len(self._benchcalls))
1434
self.assertEqual((time.sleep, (0.000,), {}), self._benchcalls[0][0])
1435
self.assertEqual((time.sleep, (0.003,), {}), self._benchcalls[1][0])
1436
self.assertIsInstance(self._benchcalls[0][1], bzrlib.lsprof.Stats)
1437
self.assertIsInstance(self._benchcalls[1][1], bzrlib.lsprof.Stats)
1439
def test_knownFailure(self):
1440
"""Self.knownFailure() should raise a KnownFailure exception."""
1441
self.assertRaises(KnownFailure, self.knownFailure, "A Failure")
1443
def test_requireFeature_available(self):
1444
"""self.requireFeature(available) is a no-op."""
1445
class Available(Feature):
1446
def _probe(self):return True
1447
feature = Available()
1448
self.requireFeature(feature)
1450
def test_requireFeature_unavailable(self):
1451
"""self.requireFeature(unavailable) raises UnavailableFeature."""
1452
class Unavailable(Feature):
1453
def _probe(self):return False
1454
feature = Unavailable()
1455
self.assertRaises(UnavailableFeature, self.requireFeature, feature)
1457
def test_run_no_parameters(self):
1458
test = SampleTestCase('_test_pass')
1461
def test_run_enabled_unittest_result(self):
1462
"""Test we revert to regular behaviour when the test is enabled."""
1463
test = SampleTestCase('_test_pass')
1464
class EnabledFeature(object):
1465
def available(self):
1467
test._test_needs_features = [EnabledFeature()]
1468
result = unittest.TestResult()
1470
self.assertEqual(1, result.testsRun)
1471
self.assertEqual([], result.errors)
1472
self.assertEqual([], result.failures)
1474
def test_run_disabled_unittest_result(self):
1475
"""Test our compatability for disabled tests with unittest results."""
1476
test = SampleTestCase('_test_pass')
1477
class DisabledFeature(object):
1478
def available(self):
1480
test._test_needs_features = [DisabledFeature()]
1481
result = unittest.TestResult()
1483
self.assertEqual(1, result.testsRun)
1484
self.assertEqual([], result.errors)
1485
self.assertEqual([], result.failures)
1487
def test_run_disabled_supporting_result(self):
1488
"""Test disabled tests behaviour with support aware results."""
1489
test = SampleTestCase('_test_pass')
1490
class DisabledFeature(object):
1491
def available(self):
1493
the_feature = DisabledFeature()
1494
test._test_needs_features = [the_feature]
1495
class InstrumentedTestResult(unittest.TestResult):
1497
unittest.TestResult.__init__(self)
1499
def startTest(self, test):
1500
self.calls.append(('startTest', test))
1501
def stopTest(self, test):
1502
self.calls.append(('stopTest', test))
1503
def addNotSupported(self, test, feature):
1504
self.calls.append(('addNotSupported', test, feature))
1505
result = InstrumentedTestResult()
1508
('startTest', test),
1509
('addNotSupported', test, the_feature),
1515
@symbol_versioning.deprecated_function(zero_eleven)
1516
def sample_deprecated_function():
1517
"""A deprecated function to test applyDeprecated with."""
1521
def sample_undeprecated_function(a_param):
1522
"""A undeprecated function to test applyDeprecated with."""
1525
class ApplyDeprecatedHelper(object):
1526
"""A helper class for ApplyDeprecated tests."""
1528
@symbol_versioning.deprecated_method(zero_eleven)
1529
def sample_deprecated_method(self, param_one):
1530
"""A deprecated method for testing with."""
1533
def sample_normal_method(self):
1534
"""A undeprecated method."""
1536
@symbol_versioning.deprecated_method(zero_ten)
1537
def sample_nested_deprecation(self):
1538
return sample_deprecated_function()
1541
class TestExtraAssertions(TestCase):
1542
"""Tests for new test assertions in bzrlib test suite"""
1544
def test_assert_isinstance(self):
1545
self.assertIsInstance(2, int)
1546
self.assertIsInstance(u'', basestring)
1547
self.assertRaises(AssertionError, self.assertIsInstance, None, int)
1548
self.assertRaises(AssertionError, self.assertIsInstance, 23.3, int)
1550
def test_assertEndsWith(self):
1551
self.assertEndsWith('foo', 'oo')
1552
self.assertRaises(AssertionError, self.assertEndsWith, 'o', 'oo')
1554
def test_applyDeprecated_not_deprecated(self):
1555
sample_object = ApplyDeprecatedHelper()
1556
# calling an undeprecated callable raises an assertion
1557
self.assertRaises(AssertionError, self.applyDeprecated, zero_eleven,
1558
sample_object.sample_normal_method)
1559
self.assertRaises(AssertionError, self.applyDeprecated, zero_eleven,
1560
sample_undeprecated_function, "a param value")
1561
# calling a deprecated callable (function or method) with the wrong
1562
# expected deprecation fails.
1563
self.assertRaises(AssertionError, self.applyDeprecated, zero_ten,
1564
sample_object.sample_deprecated_method, "a param value")
1565
self.assertRaises(AssertionError, self.applyDeprecated, zero_ten,
1566
sample_deprecated_function)
1567
# calling a deprecated callable (function or method) with the right
1568
# expected deprecation returns the functions result.
1569
self.assertEqual("a param value", self.applyDeprecated(zero_eleven,
1570
sample_object.sample_deprecated_method, "a param value"))
1571
self.assertEqual(2, self.applyDeprecated(zero_eleven,
1572
sample_deprecated_function))
1573
# calling a nested deprecation with the wrong deprecation version
1574
# fails even if a deeper nested function was deprecated with the
1576
self.assertRaises(AssertionError, self.applyDeprecated,
1577
zero_eleven, sample_object.sample_nested_deprecation)
1578
# calling a nested deprecation with the right deprecation value
1579
# returns the calls result.
1580
self.assertEqual(2, self.applyDeprecated(zero_ten,
1581
sample_object.sample_nested_deprecation))
1583
def test_callDeprecated(self):
1584
def testfunc(be_deprecated, result=None):
1585
if be_deprecated is True:
1586
symbol_versioning.warn('i am deprecated', DeprecationWarning,
1589
result = self.callDeprecated(['i am deprecated'], testfunc, True)
1590
self.assertIs(None, result)
1591
result = self.callDeprecated([], testfunc, False, 'result')
1592
self.assertEqual('result', result)
1593
self.callDeprecated(['i am deprecated'], testfunc, be_deprecated=True)
1594
self.callDeprecated([], testfunc, be_deprecated=False)
1597
class TestWarningTests(TestCase):
1598
"""Tests for calling methods that raise warnings."""
1600
def test_callCatchWarnings(self):
1602
warnings.warn("this is your last warning")
1604
wlist, result = self.callCatchWarnings(meth, 1, 2)
1605
self.assertEquals(3, result)
1606
# would like just to compare them, but UserWarning doesn't implement
1609
self.assertIsInstance(w0, UserWarning)
1610
self.assertEquals("this is your last warning", str(w0))
1613
class TestConvenienceMakers(TestCaseWithTransport):
1614
"""Test for the make_* convenience functions."""
1616
def test_make_branch_and_tree_with_format(self):
1617
# we should be able to supply a format to make_branch_and_tree
1618
self.make_branch_and_tree('a', format=bzrlib.bzrdir.BzrDirMetaFormat1())
1619
self.make_branch_and_tree('b', format=bzrlib.bzrdir.BzrDirFormat6())
1620
self.assertIsInstance(bzrlib.bzrdir.BzrDir.open('a')._format,
1621
bzrlib.bzrdir.BzrDirMetaFormat1)
1622
self.assertIsInstance(bzrlib.bzrdir.BzrDir.open('b')._format,
1623
bzrlib.bzrdir.BzrDirFormat6)
1625
def test_make_branch_and_memory_tree(self):
1626
# we should be able to get a new branch and a mutable tree from
1627
# TestCaseWithTransport
1628
tree = self.make_branch_and_memory_tree('a')
1629
self.assertIsInstance(tree, bzrlib.memorytree.MemoryTree)
1632
class TestSFTPMakeBranchAndTree(TestCaseWithSFTPServer):
1634
def test_make_tree_for_sftp_branch(self):
1635
"""Transports backed by local directories create local trees."""
1637
tree = self.make_branch_and_tree('t1')
1638
base = tree.bzrdir.root_transport.base
1639
self.failIf(base.startswith('sftp'),
1640
'base %r is on sftp but should be local' % base)
1641
self.assertEquals(tree.bzrdir.root_transport,
1642
tree.branch.bzrdir.root_transport)
1643
self.assertEquals(tree.bzrdir.root_transport,
1644
tree.branch.repository.bzrdir.root_transport)
1647
class TestSelftest(TestCase):
1648
"""Tests of bzrlib.tests.selftest."""
1650
def test_selftest_benchmark_parameter_invokes_test_suite__benchmark__(self):
1653
factory_called.append(True)
1657
self.apply_redirected(out, err, None, bzrlib.tests.selftest,
1658
test_suite_factory=factory)
1659
self.assertEqual([True], factory_called)
1662
class TestKnownFailure(TestCase):
1664
def test_known_failure(self):
1665
"""Check that KnownFailure is defined appropriately."""
1666
# a KnownFailure is an assertion error for compatability with unaware
1668
self.assertIsInstance(KnownFailure(""), AssertionError)
1670
def test_expect_failure(self):
1672
self.expectFailure("Doomed to failure", self.assertTrue, False)
1673
except KnownFailure, e:
1674
self.assertEqual('Doomed to failure', e.args[0])
1676
self.expectFailure("Doomed to failure", self.assertTrue, True)
1677
except AssertionError, e:
1678
self.assertEqual('Unexpected success. Should have failed:'
1679
' Doomed to failure', e.args[0])
1681
self.fail('Assertion not raised')
1684
class TestFeature(TestCase):
1686
def test_caching(self):
1687
"""Feature._probe is called by the feature at most once."""
1688
class InstrumentedFeature(Feature):
1690
Feature.__init__(self)
1693
self.calls.append('_probe')
1695
feature = InstrumentedFeature()
1697
self.assertEqual(['_probe'], feature.calls)
1699
self.assertEqual(['_probe'], feature.calls)
1701
def test_named_str(self):
1702
"""Feature.__str__ should thunk to feature_name()."""
1703
class NamedFeature(Feature):
1704
def feature_name(self):
1706
feature = NamedFeature()
1707
self.assertEqual('symlinks', str(feature))
1709
def test_default_str(self):
1710
"""Feature.__str__ should default to __class__.__name__."""
1711
class NamedFeature(Feature):
1713
feature = NamedFeature()
1714
self.assertEqual('NamedFeature', str(feature))
1717
class TestUnavailableFeature(TestCase):
1719
def test_access_feature(self):
1721
exception = UnavailableFeature(feature)
1722
self.assertIs(feature, exception.args[0])
1725
class TestSelftestFiltering(TestCase):
1728
self.suite = TestUtil.TestSuite()
1729
self.loader = TestUtil.TestLoader()
1730
self.suite.addTest(self.loader.loadTestsFromModuleNames([
1731
'bzrlib.tests.test_selftest']))
1732
self.all_names = _test_ids(self.suite)
1734
def test_condition_id_re(self):
1735
test_name = ('bzrlib.tests.test_selftest.TestSelftestFiltering.'
1736
'test_condition_id_re')
1737
filtered_suite = filter_suite_by_condition(self.suite,
1738
condition_id_re('test_condition_id_re'))
1739
self.assertEqual([test_name], _test_ids(filtered_suite))
1741
def test_condition_id_in_list(self):
1742
test_names = ['bzrlib.tests.test_selftest.TestSelftestFiltering.'
1743
'test_condition_id_in_list']
1744
id_list = tests.TestIdList(test_names)
1745
filtered_suite = filter_suite_by_condition(
1746
self.suite, tests.condition_id_in_list(id_list))
1747
my_pattern = 'TestSelftestFiltering.*test_condition_id_in_list'
1748
re_filtered = filter_suite_by_re(self.suite, my_pattern)
1749
self.assertEqual(_test_ids(re_filtered), _test_ids(filtered_suite))
1751
def test_condition_id_startswith(self):
1752
klass = 'bzrlib.tests.test_selftest.TestSelftestFiltering.'
1753
start = klass + 'test_condition_id_starts'
1754
test_names = [klass + 'test_condition_id_startswith']
1755
filtered_suite = filter_suite_by_condition(
1756
self.suite, tests.condition_id_startswith(start))
1757
my_pattern = 'TestSelftestFiltering.*test_condition_id_startswith'
1758
re_filtered = filter_suite_by_re(self.suite, my_pattern)
1759
self.assertEqual(_test_ids(re_filtered), _test_ids(filtered_suite))
1761
def test_condition_isinstance(self):
1762
filtered_suite = filter_suite_by_condition(self.suite,
1763
condition_isinstance(self.__class__))
1764
class_pattern = 'bzrlib.tests.test_selftest.TestSelftestFiltering.'
1765
re_filtered = filter_suite_by_re(self.suite, class_pattern)
1766
self.assertEqual(_test_ids(re_filtered), _test_ids(filtered_suite))
1768
def test_exclude_tests_by_condition(self):
1769
excluded_name = ('bzrlib.tests.test_selftest.TestSelftestFiltering.'
1770
'test_exclude_tests_by_condition')
1771
filtered_suite = exclude_tests_by_condition(self.suite,
1772
lambda x:x.id() == excluded_name)
1773
self.assertEqual(len(self.all_names) - 1,
1774
filtered_suite.countTestCases())
1775
self.assertFalse(excluded_name in _test_ids(filtered_suite))
1776
remaining_names = list(self.all_names)
1777
remaining_names.remove(excluded_name)
1778
self.assertEqual(remaining_names, _test_ids(filtered_suite))
1780
def test_exclude_tests_by_re(self):
1781
self.all_names = _test_ids(self.suite)
1782
filtered_suite = exclude_tests_by_re(self.suite, 'exclude_tests_by_re')
1783
excluded_name = ('bzrlib.tests.test_selftest.TestSelftestFiltering.'
1784
'test_exclude_tests_by_re')
1785
self.assertEqual(len(self.all_names) - 1,
1786
filtered_suite.countTestCases())
1787
self.assertFalse(excluded_name in _test_ids(filtered_suite))
1788
remaining_names = list(self.all_names)
1789
remaining_names.remove(excluded_name)
1790
self.assertEqual(remaining_names, _test_ids(filtered_suite))
1792
def test_filter_suite_by_condition(self):
1793
test_name = ('bzrlib.tests.test_selftest.TestSelftestFiltering.'
1794
'test_filter_suite_by_condition')
1795
filtered_suite = filter_suite_by_condition(self.suite,
1796
lambda x:x.id() == test_name)
1797
self.assertEqual([test_name], _test_ids(filtered_suite))
1799
def test_filter_suite_by_re(self):
1800
filtered_suite = filter_suite_by_re(self.suite, 'test_filter_suite_by_r')
1801
filtered_names = _test_ids(filtered_suite)
1802
self.assertEqual(filtered_names, ['bzrlib.tests.test_selftest.'
1803
'TestSelftestFiltering.test_filter_suite_by_re'])
1805
def test_filter_suite_by_id_list(self):
1806
test_list = ['bzrlib.tests.test_selftest.'
1807
'TestSelftestFiltering.test_filter_suite_by_id_list']
1808
filtered_suite = tests.filter_suite_by_id_list(
1809
self.suite, tests.TestIdList(test_list))
1810
filtered_names = _test_ids(filtered_suite)
1813
['bzrlib.tests.test_selftest.'
1814
'TestSelftestFiltering.test_filter_suite_by_id_list'])
1816
def test_filter_suite_by_id_startswith(self):
1817
# By design this test may fail if another test is added whose name also
1818
# begins with the start value used.
1819
klass = 'bzrlib.tests.test_selftest.TestSelftestFiltering.'
1820
start = klass + 'test_filter_suite_by_id_starts'
1821
test_list = [klass + 'test_filter_suite_by_id_startswith']
1822
filtered_suite = tests.filter_suite_by_id_startswith(self.suite, start)
1823
filtered_names = _test_ids(filtered_suite)
1826
['bzrlib.tests.test_selftest.'
1827
'TestSelftestFiltering.test_filter_suite_by_id_startswith'])
1829
def test_preserve_input(self):
1830
# NB: Surely this is something in the stdlib to do this?
1831
self.assertTrue(self.suite is preserve_input(self.suite))
1832
self.assertTrue("@#$" is preserve_input("@#$"))
1834
def test_randomize_suite(self):
1835
randomized_suite = randomize_suite(self.suite)
1836
# randomizing should not add or remove test names.
1837
self.assertEqual(set(_test_ids(self.suite)),
1838
set(_test_ids(randomized_suite)))
1839
# Technically, this *can* fail, because random.shuffle(list) can be
1840
# equal to list. Trying multiple times just pushes the frequency back.
1841
# As its len(self.all_names)!:1, the failure frequency should be low
1842
# enough to ignore. RBC 20071021.
1843
# It should change the order.
1844
self.assertNotEqual(self.all_names, _test_ids(randomized_suite))
1845
# But not the length. (Possibly redundant with the set test, but not
1847
self.assertEqual(len(self.all_names), len(_test_ids(randomized_suite)))
1849
def test_split_suit_by_condition(self):
1850
self.all_names = _test_ids(self.suite)
1851
condition = condition_id_re('test_filter_suite_by_r')
1852
split_suite = split_suite_by_condition(self.suite, condition)
1853
filtered_name = ('bzrlib.tests.test_selftest.TestSelftestFiltering.'
1854
'test_filter_suite_by_re')
1855
self.assertEqual([filtered_name], _test_ids(split_suite[0]))
1856
self.assertFalse(filtered_name in _test_ids(split_suite[1]))
1857
remaining_names = list(self.all_names)
1858
remaining_names.remove(filtered_name)
1859
self.assertEqual(remaining_names, _test_ids(split_suite[1]))
1861
def test_split_suit_by_re(self):
1862
self.all_names = _test_ids(self.suite)
1863
split_suite = split_suite_by_re(self.suite, 'test_filter_suite_by_r')
1864
filtered_name = ('bzrlib.tests.test_selftest.TestSelftestFiltering.'
1865
'test_filter_suite_by_re')
1866
self.assertEqual([filtered_name], _test_ids(split_suite[0]))
1867
self.assertFalse(filtered_name in _test_ids(split_suite[1]))
1868
remaining_names = list(self.all_names)
1869
remaining_names.remove(filtered_name)
1870
self.assertEqual(remaining_names, _test_ids(split_suite[1]))
1873
class TestCheckInventoryShape(TestCaseWithTransport):
1875
def test_check_inventory_shape(self):
1876
files = ['a', 'b/', 'b/c']
1877
tree = self.make_branch_and_tree('.')
1878
self.build_tree(files)
1882
self.check_inventory_shape(tree.inventory, files)
1887
class TestBlackboxSupport(TestCase):
1888
"""Tests for testsuite blackbox features."""
1890
def test_run_bzr_failure_not_caught(self):
1891
# When we run bzr in blackbox mode, we want any unexpected errors to
1892
# propagate up to the test suite so that it can show the error in the
1893
# usual way, and we won't get a double traceback.
1894
e = self.assertRaises(
1896
self.run_bzr, ['assert-fail'])
1897
# make sure we got the real thing, not an error from somewhere else in
1898
# the test framework
1899
self.assertEquals('always fails', str(e))
1900
# check that there's no traceback in the test log
1901
self.assertNotContainsRe(self._get_log(keep_log_file=True),
1904
def test_run_bzr_user_error_caught(self):
1905
# Running bzr in blackbox mode, normal/expected/user errors should be
1906
# caught in the regular way and turned into an error message plus exit
1908
out, err = self.run_bzr(["log", "/nonexistantpath"], retcode=3)
1909
self.assertEqual(out, '')
1910
self.assertContainsRe(err,
1911
'bzr: ERROR: Not a branch: ".*nonexistantpath/".\n')
1914
class TestTestLoader(TestCase):
1915
"""Tests for the test loader."""
1917
def _get_loader_and_module(self):
1918
"""Gets a TestLoader and a module with one test in it."""
1919
loader = TestUtil.TestLoader()
1921
class Stub(TestCase):
1924
class MyModule(object):
1926
MyModule.a_class = Stub
1928
return loader, module
1930
def test_module_no_load_tests_attribute_loads_classes(self):
1931
loader, module = self._get_loader_and_module()
1932
self.assertEqual(1, loader.loadTestsFromModule(module).countTestCases())
1934
def test_module_load_tests_attribute_gets_called(self):
1935
loader, module = self._get_loader_and_module()
1936
# 'self' is here because we're faking the module with a class. Regular
1937
# load_tests do not need that :)
1938
def load_tests(self, standard_tests, module, loader):
1939
result = loader.suiteClass()
1940
for test in iter_suite_tests(standard_tests):
1941
result.addTests([test, test])
1943
# add a load_tests() method which multiplies the tests from the module.
1944
module.__class__.load_tests = load_tests
1945
self.assertEqual(2, loader.loadTestsFromModule(module).countTestCases())
1947
def test_load_tests_from_module_name_smoke_test(self):
1948
loader = TestUtil.TestLoader()
1949
suite = loader.loadTestsFromModuleName('bzrlib.tests.test_sampler')
1950
self.assertEquals(['bzrlib.tests.test_sampler.DemoTest.test_nothing'],
1953
def test_load_tests_from_module_name_with_bogus_module_name(self):
1954
loader = TestUtil.TestLoader()
1955
self.assertRaises(ImportError, loader.loadTestsFromModuleName, 'bogus')
1958
class TestTestIdList(tests.TestCase):
1960
def _create_id_list(self, test_list):
1961
return tests.TestIdList(test_list)
1963
def _create_suite(self, test_id_list):
1965
class Stub(TestCase):
1969
def _create_test_id(id):
1972
suite = TestUtil.TestSuite()
1973
for id in test_id_list:
1974
t = Stub('test_foo')
1975
t.id = _create_test_id(id)
1979
def _test_ids(self, test_suite):
1980
"""Get the ids for the tests in a test suite."""
1981
return [t.id() for t in iter_suite_tests(test_suite)]
1983
def test_empty_list(self):
1984
id_list = self._create_id_list([])
1985
self.assertEquals({}, id_list.tests)
1986
self.assertEquals({}, id_list.modules)
1988
def test_valid_list(self):
1989
id_list = self._create_id_list(
1990
['mod1.cl1.meth1', 'mod1.cl1.meth2',
1991
'mod1.func1', 'mod1.cl2.meth2',
1993
'mod1.submod2.cl1.meth1', 'mod1.submod2.cl2.meth2',
1995
self.assertTrue(id_list.refers_to('mod1'))
1996
self.assertTrue(id_list.refers_to('mod1.submod1'))
1997
self.assertTrue(id_list.refers_to('mod1.submod2'))
1998
self.assertTrue(id_list.includes('mod1.cl1.meth1'))
1999
self.assertTrue(id_list.includes('mod1.submod1'))
2000
self.assertTrue(id_list.includes('mod1.func1'))
2002
def test_bad_chars_in_params(self):
2003
id_list = self._create_id_list(['mod1.cl1.meth1(xx.yy)'])
2004
self.assertTrue(id_list.refers_to('mod1'))
2005
self.assertTrue(id_list.includes('mod1.cl1.meth1(xx.yy)'))
2007
def test_module_used(self):
2008
id_list = self._create_id_list(['mod.class.meth'])
2009
self.assertTrue(id_list.refers_to('mod'))
2010
self.assertTrue(id_list.refers_to('mod.class'))
2011
self.assertTrue(id_list.refers_to('mod.class.meth'))
2013
def test_test_suite(self):
2014
# This test is slow, so we do a single test with one test in each
2018
'bzrlib.tests.blackbox.test_branch.TestBranch.test_branch',
2019
'bzrlib.tests.test_selftest.TestTestIdList.test_test_suite',
2020
# transport implementations
2021
'bzrlib.tests.test_transport_implementations.TransportTests'
2022
'.test_abspath(LocalURLServer)',
2023
# modules_to_doctest
2024
'bzrlib.timestamp.format_highres_date',
2025
# plugins can't be tested that way since selftest may be run with
2028
suite = tests.test_suite(test_list)
2029
self.assertEquals(test_list, _test_ids(suite))
2031
def test_test_suite_matches_id_list_with_unknown(self):
2032
loader = TestUtil.TestLoader()
2033
suite = loader.loadTestsFromModuleName('bzrlib.tests.test_sampler')
2034
test_list = ['bzrlib.tests.test_sampler.DemoTest.test_nothing',
2036
not_found, duplicates = tests.suite_matches_id_list(suite, test_list)
2037
self.assertEquals(['bogus'], not_found)
2038
self.assertEquals([], duplicates)
2040
def test_suite_matches_id_list_with_duplicates(self):
2041
loader = TestUtil.TestLoader()
2042
suite = loader.loadTestsFromModuleName('bzrlib.tests.test_sampler')
2043
dupes = loader.suiteClass()
2044
for test in iter_suite_tests(suite):
2046
dupes.addTest(test) # Add it again
2048
test_list = ['bzrlib.tests.test_sampler.DemoTest.test_nothing',]
2049
not_found, duplicates = tests.suite_matches_id_list(
2051
self.assertEquals([], not_found)
2052
self.assertEquals(['bzrlib.tests.test_sampler.DemoTest.test_nothing'],
2056
class TestLoadTestIdList(tests.TestCaseInTempDir):
2058
def _create_test_list_file(self, file_name, content):
2059
fl = open(file_name, 'wt')
2063
def test_load_unknown(self):
2064
self.assertRaises(errors.NoSuchFile,
2065
tests.load_test_id_list, 'i_do_not_exist')
2067
def test_load_test_list(self):
2068
test_list_fname = 'test.list'
2069
self._create_test_list_file(test_list_fname,
2070
'mod1.cl1.meth1\nmod2.cl2.meth2\n')
2071
tlist = tests.load_test_id_list(test_list_fname)
2072
self.assertEquals(2, len(tlist))
2073
self.assertEquals('mod1.cl1.meth1', tlist[0])
2074
self.assertEquals('mod2.cl2.meth2', tlist[1])
2076
def test_load_dirty_file(self):
2077
test_list_fname = 'test.list'
2078
self._create_test_list_file(test_list_fname,
2079
' mod1.cl1.meth1\n\nmod2.cl2.meth2 \n'
2081
tlist = tests.load_test_id_list(test_list_fname)
2082
self.assertEquals(4, len(tlist))
2083
self.assertEquals('mod1.cl1.meth1', tlist[0])
2084
self.assertEquals('', tlist[1])
2085
self.assertEquals('mod2.cl2.meth2', tlist[2])
2086
self.assertEquals('bar baz', tlist[3])
2089
class TestFilteredByModuleTestLoader(tests.TestCase):
2091
def _create_loader(self, test_list):
2092
id_filter = tests.TestIdList(test_list)
2093
loader = TestUtil.FilteredByModuleTestLoader(id_filter.refers_to)
2096
def test_load_tests(self):
2097
test_list = ['bzrlib.tests.test_sampler.DemoTest.test_nothing']
2098
loader = self._create_loader(test_list)
2100
suite = loader.loadTestsFromModuleName('bzrlib.tests.test_sampler')
2101
self.assertEquals(test_list, _test_ids(suite))
2103
def test_exclude_tests(self):
2104
test_list = ['bogus']
2105
loader = self._create_loader(test_list)
2107
suite = loader.loadTestsFromModuleName('bzrlib.tests.test_sampler')
2108
self.assertEquals([], _test_ids(suite))
2111
class TestFilteredByNameStartTestLoader(tests.TestCase):
2113
def _create_loader(self, name_start):
2114
def needs_module(name):
2115
return name.startswith(name_start) or name_start.startswith(name)
2116
loader = TestUtil.FilteredByModuleTestLoader(needs_module)
2119
def test_load_tests(self):
2120
test_list = ['bzrlib.tests.test_sampler.DemoTest.test_nothing']
2121
loader = self._create_loader('bzrlib.tests.test_samp')
2123
suite = loader.loadTestsFromModuleName('bzrlib.tests.test_sampler')
2124
self.assertEquals(test_list, _test_ids(suite))
2126
def test_load_tests_inside_module(self):
2127
test_list = ['bzrlib.tests.test_sampler.DemoTest.test_nothing']
2128
loader = self._create_loader('bzrlib.tests.test_sampler.Demo')
2130
suite = loader.loadTestsFromModuleName('bzrlib.tests.test_sampler')
2131
self.assertEquals(test_list, _test_ids(suite))
2133
def test_exclude_tests(self):
2134
test_list = ['bogus']
2135
loader = self._create_loader('bogus')
2137
suite = loader.loadTestsFromModuleName('bzrlib.tests.test_sampler')
2138
self.assertEquals([], _test_ids(suite))