1
# Copyright (C) 2005, 2006 by 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 version 2 as published by
5
# the Free Software Foundation.
7
# This program is distributed in the hope that it will be useful,
8
# but WITHOUT ANY WARRANTY; without even the implied warranty of
9
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
# GNU General Public License for more details.
12
# You should have received a copy of the GNU General Public License
13
# along with this program; if not, write to the Free Software
14
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16
"""Tests for the test framework."""
19
from StringIO import StringIO
26
from bzrlib.progress import _BaseProgressBar
27
from bzrlib.tests import (
31
TestCaseWithTransport,
36
from bzrlib.tests.TestUtil import _load_module_by_name
37
import bzrlib.errors as errors
38
from bzrlib.trace import note
41
class SelftestTests(TestCase):
43
def test_import_tests(self):
44
mod = _load_module_by_name('bzrlib.tests.test_selftest')
45
self.assertEqual(mod.SelftestTests, SelftestTests)
47
def test_import_test_failure(self):
48
self.assertRaises(ImportError,
53
class MetaTestLog(TestCase):
55
def test_logging(self):
56
"""Test logs are captured when a test fails."""
57
self.log('a test message')
58
self._log_file.flush()
59
self.assertContainsRe(self._get_log(), 'a test message\n')
62
class TestTreeShape(TestCaseInTempDir):
64
def test_unicode_paths(self):
65
filename = u'hell\u00d8'
67
self.build_tree_contents([(filename, 'contents of hello')])
68
except UnicodeEncodeError:
69
raise TestSkipped("can't build unicode working tree in "
70
"filesystem encoding %s" % sys.getfilesystemencoding())
71
self.failUnlessExists(filename)
74
class TestTransportProviderAdapter(TestCase):
75
"""A group of tests that test the transport implementation adaption core.
77
This is a meta test that the tests are applied to all available
80
This will be generalised in the future which is why it is in this
81
test file even though it is specific to transport tests at the moment.
84
def test_get_transport_permutations(self):
85
# this checks that we the module get_test_permutations call
86
# is made by the adapter get_transport_test_permitations method.
87
class MockModule(object):
88
def get_test_permutations(self):
89
return sample_permutation
90
sample_permutation = [(1,2), (3,4)]
91
from bzrlib.transport import TransportTestProviderAdapter
92
adapter = TransportTestProviderAdapter()
93
self.assertEqual(sample_permutation,
94
adapter.get_transport_test_permutations(MockModule()))
96
def test_adapter_checks_all_modules(self):
97
# this checks that the adapter returns as many permurtations as
98
# there are in all the registered# transport modules for there
99
# - we assume if this matches its probably doing the right thing
100
# especially in combination with the tests for setting the right
102
from bzrlib.transport import (TransportTestProviderAdapter,
103
_get_transport_modules
105
modules = _get_transport_modules()
106
permutation_count = 0
107
for module in modules:
109
permutation_count += len(reduce(getattr,
110
(module + ".get_test_permutations").split('.')[1:],
111
__import__(module))())
112
except errors.DependencyNotPresent:
114
input_test = TestTransportProviderAdapter(
115
"test_adapter_sets_transport_class")
116
adapter = TransportTestProviderAdapter()
117
self.assertEqual(permutation_count,
118
len(list(iter(adapter.adapt(input_test)))))
120
def test_adapter_sets_transport_class(self):
121
# Check that the test adapter inserts a transport and server into the
124
# This test used to know about all the possible transports and the
125
# order they were returned but that seems overly brittle (mbp
127
input_test = TestTransportProviderAdapter(
128
"test_adapter_sets_transport_class")
129
from bzrlib.transport import TransportTestProviderAdapter
130
suite = TransportTestProviderAdapter().adapt(input_test)
131
tests = list(iter(suite))
132
self.assertTrue(len(tests) > 6)
133
# there are at least that many builtin transports
135
self.assertTrue(issubclass(one_test.transport_class,
136
bzrlib.transport.Transport))
137
self.assertTrue(issubclass(one_test.transport_server,
138
bzrlib.transport.Server))
141
class TestBranchProviderAdapter(TestCase):
142
"""A group of tests that test the branch implementation test adapter."""
144
def test_adapted_tests(self):
145
# check that constructor parameters are passed through to the adapted
147
from bzrlib.branch import BranchTestProviderAdapter
148
input_test = TestBranchProviderAdapter(
149
"test_adapted_tests")
152
formats = [("c", "C"), ("d", "D")]
153
adapter = BranchTestProviderAdapter(server1, server2, formats)
154
suite = adapter.adapt(input_test)
155
tests = list(iter(suite))
156
self.assertEqual(2, len(tests))
157
self.assertEqual(tests[0].branch_format, formats[0][0])
158
self.assertEqual(tests[0].bzrdir_format, formats[0][1])
159
self.assertEqual(tests[0].transport_server, server1)
160
self.assertEqual(tests[0].transport_readonly_server, server2)
161
self.assertEqual(tests[1].branch_format, formats[1][0])
162
self.assertEqual(tests[1].bzrdir_format, formats[1][1])
163
self.assertEqual(tests[1].transport_server, server1)
164
self.assertEqual(tests[1].transport_readonly_server, server2)
167
class TestBzrDirProviderAdapter(TestCase):
168
"""A group of tests that test the bzr dir implementation test adapter."""
170
def test_adapted_tests(self):
171
# check that constructor parameters are passed through to the adapted
173
from bzrlib.bzrdir import BzrDirTestProviderAdapter
174
input_test = TestBzrDirProviderAdapter(
175
"test_adapted_tests")
179
adapter = BzrDirTestProviderAdapter(server1, server2, formats)
180
suite = adapter.adapt(input_test)
181
tests = list(iter(suite))
182
self.assertEqual(2, len(tests))
183
self.assertEqual(tests[0].bzrdir_format, formats[0])
184
self.assertEqual(tests[0].transport_server, server1)
185
self.assertEqual(tests[0].transport_readonly_server, server2)
186
self.assertEqual(tests[1].bzrdir_format, formats[1])
187
self.assertEqual(tests[1].transport_server, server1)
188
self.assertEqual(tests[1].transport_readonly_server, server2)
191
class TestRepositoryProviderAdapter(TestCase):
192
"""A group of tests that test the repository implementation test adapter."""
194
def test_adapted_tests(self):
195
# check that constructor parameters are passed through to the adapted
197
from bzrlib.repository import RepositoryTestProviderAdapter
198
input_test = TestRepositoryProviderAdapter(
199
"test_adapted_tests")
202
formats = [("c", "C"), ("d", "D")]
203
adapter = RepositoryTestProviderAdapter(server1, server2, formats)
204
suite = adapter.adapt(input_test)
205
tests = list(iter(suite))
206
self.assertEqual(2, len(tests))
207
self.assertEqual(tests[0].bzrdir_format, formats[0][1])
208
self.assertEqual(tests[0].repository_format, formats[0][0])
209
self.assertEqual(tests[0].transport_server, server1)
210
self.assertEqual(tests[0].transport_readonly_server, server2)
211
self.assertEqual(tests[1].bzrdir_format, formats[1][1])
212
self.assertEqual(tests[1].repository_format, formats[1][0])
213
self.assertEqual(tests[1].transport_server, server1)
214
self.assertEqual(tests[1].transport_readonly_server, server2)
217
class TestInterRepositoryProviderAdapter(TestCase):
218
"""A group of tests that test the InterRepository test adapter."""
220
def test_adapted_tests(self):
221
# check that constructor parameters are passed through to the adapted
223
from bzrlib.repository import InterRepositoryTestProviderAdapter
224
input_test = TestInterRepositoryProviderAdapter(
225
"test_adapted_tests")
228
formats = [(str, "C1", "C2"), (int, "D1", "D2")]
229
adapter = InterRepositoryTestProviderAdapter(server1, server2, formats)
230
suite = adapter.adapt(input_test)
231
tests = list(iter(suite))
232
self.assertEqual(2, len(tests))
233
self.assertEqual(tests[0].interrepo_class, formats[0][0])
234
self.assertEqual(tests[0].repository_format, formats[0][1])
235
self.assertEqual(tests[0].repository_format_to, formats[0][2])
236
self.assertEqual(tests[0].transport_server, server1)
237
self.assertEqual(tests[0].transport_readonly_server, server2)
238
self.assertEqual(tests[1].interrepo_class, formats[1][0])
239
self.assertEqual(tests[1].repository_format, formats[1][1])
240
self.assertEqual(tests[1].repository_format_to, formats[1][2])
241
self.assertEqual(tests[1].transport_server, server1)
242
self.assertEqual(tests[1].transport_readonly_server, server2)
245
class TestInterVersionedFileProviderAdapter(TestCase):
246
"""A group of tests that test the InterVersionedFile test adapter."""
248
def test_adapted_tests(self):
249
# check that constructor parameters are passed through to the adapted
251
from bzrlib.versionedfile import InterVersionedFileTestProviderAdapter
252
input_test = TestInterRepositoryProviderAdapter(
253
"test_adapted_tests")
256
formats = [(str, "C1", "C2"), (int, "D1", "D2")]
257
adapter = InterVersionedFileTestProviderAdapter(server1, server2, formats)
258
suite = adapter.adapt(input_test)
259
tests = list(iter(suite))
260
self.assertEqual(2, len(tests))
261
self.assertEqual(tests[0].interversionedfile_class, formats[0][0])
262
self.assertEqual(tests[0].versionedfile_factory, formats[0][1])
263
self.assertEqual(tests[0].versionedfile_factory_to, formats[0][2])
264
self.assertEqual(tests[0].transport_server, server1)
265
self.assertEqual(tests[0].transport_readonly_server, server2)
266
self.assertEqual(tests[1].interversionedfile_class, formats[1][0])
267
self.assertEqual(tests[1].versionedfile_factory, formats[1][1])
268
self.assertEqual(tests[1].versionedfile_factory_to, formats[1][2])
269
self.assertEqual(tests[1].transport_server, server1)
270
self.assertEqual(tests[1].transport_readonly_server, server2)
273
class TestRevisionStoreProviderAdapter(TestCase):
274
"""A group of tests that test the RevisionStore test adapter."""
276
def test_adapted_tests(self):
277
# check that constructor parameters are passed through to the adapted
279
from bzrlib.store.revision import RevisionStoreTestProviderAdapter
280
input_test = TestRevisionStoreProviderAdapter(
281
"test_adapted_tests")
282
# revision stores need a store factory - i.e. RevisionKnit
283
#, a readonly and rw transport
287
store_factories = ["c", "d"]
288
adapter = RevisionStoreTestProviderAdapter(server1, server2, store_factories)
289
suite = adapter.adapt(input_test)
290
tests = list(iter(suite))
291
self.assertEqual(2, len(tests))
292
self.assertEqual(tests[0].store_factory, store_factories[0][0])
293
self.assertEqual(tests[0].transport_server, server1)
294
self.assertEqual(tests[0].transport_readonly_server, server2)
295
self.assertEqual(tests[1].store_factory, store_factories[1][0])
296
self.assertEqual(tests[1].transport_server, server1)
297
self.assertEqual(tests[1].transport_readonly_server, server2)
300
class TestWorkingTreeProviderAdapter(TestCase):
301
"""A group of tests that test the workingtree implementation test adapter."""
303
def test_adapted_tests(self):
304
# check that constructor parameters are passed through to the adapted
306
from bzrlib.workingtree import WorkingTreeTestProviderAdapter
307
input_test = TestWorkingTreeProviderAdapter(
308
"test_adapted_tests")
311
formats = [("c", "C"), ("d", "D")]
312
adapter = WorkingTreeTestProviderAdapter(server1, server2, formats)
313
suite = adapter.adapt(input_test)
314
tests = list(iter(suite))
315
self.assertEqual(2, len(tests))
316
self.assertEqual(tests[0].workingtree_format, formats[0][0])
317
self.assertEqual(tests[0].bzrdir_format, formats[0][1])
318
self.assertEqual(tests[0].transport_server, server1)
319
self.assertEqual(tests[0].transport_readonly_server, server2)
320
self.assertEqual(tests[1].workingtree_format, formats[1][0])
321
self.assertEqual(tests[1].bzrdir_format, formats[1][1])
322
self.assertEqual(tests[1].transport_server, server1)
323
self.assertEqual(tests[1].transport_readonly_server, server2)
326
class TestTreeProviderAdapter(TestCase):
327
"""Test the setup of tree_implementation tests."""
329
def test_adapted_tests(self):
330
# the tree implementation adapter is meant to setup one instance for
331
# each working tree format, and one additional instance that will
332
# use the default wt format, but create a revision tree for the tests.
333
# this means that the wt ones should have the workingtree_to_test_tree
334
# attribute set to 'return_parameter' and the revision one set to
335
# revision_tree_from_workingtree.
337
from bzrlib.tests.tree_implementations import (
338
TreeTestProviderAdapter,
340
revision_tree_from_workingtree
342
from bzrlib.workingtree import WorkingTreeFormat
343
input_test = TestTreeProviderAdapter(
344
"test_adapted_tests")
347
formats = [("c", "C"), ("d", "D")]
348
adapter = TreeTestProviderAdapter(server1, server2, formats)
349
suite = adapter.adapt(input_test)
350
tests = list(iter(suite))
351
self.assertEqual(3, len(tests))
352
default_format = WorkingTreeFormat.get_default_format()
353
self.assertEqual(tests[0].workingtree_format, formats[0][0])
354
self.assertEqual(tests[0].bzrdir_format, formats[0][1])
355
self.assertEqual(tests[0].transport_server, server1)
356
self.assertEqual(tests[0].transport_readonly_server, server2)
357
self.assertEqual(tests[0].workingtree_to_test_tree, return_parameter)
358
self.assertEqual(tests[1].workingtree_format, formats[1][0])
359
self.assertEqual(tests[1].bzrdir_format, formats[1][1])
360
self.assertEqual(tests[1].transport_server, server1)
361
self.assertEqual(tests[1].transport_readonly_server, server2)
362
self.assertEqual(tests[1].workingtree_to_test_tree, return_parameter)
363
self.assertEqual(tests[2].workingtree_format, default_format)
364
self.assertEqual(tests[2].bzrdir_format, default_format._matchingbzrdir)
365
self.assertEqual(tests[2].transport_server, server1)
366
self.assertEqual(tests[2].transport_readonly_server, server2)
367
self.assertEqual(tests[2].workingtree_to_test_tree,
368
revision_tree_from_workingtree)
371
class TestTestCaseWithTransport(TestCaseWithTransport):
372
"""Tests for the convenience functions TestCaseWithTransport introduces."""
374
def test_get_readonly_url_none(self):
375
from bzrlib.transport import get_transport
376
from bzrlib.transport.memory import MemoryServer
377
from bzrlib.transport.readonly import ReadonlyTransportDecorator
378
self.transport_server = MemoryServer
379
self.transport_readonly_server = None
380
# calling get_readonly_transport() constructs a decorator on the url
382
url = self.get_readonly_url()
383
url2 = self.get_readonly_url('foo/bar')
384
t = get_transport(url)
385
t2 = get_transport(url2)
386
self.failUnless(isinstance(t, ReadonlyTransportDecorator))
387
self.failUnless(isinstance(t2, ReadonlyTransportDecorator))
388
self.assertEqual(t2.base[:-1], t.abspath('foo/bar'))
390
def test_get_readonly_url_http(self):
391
from bzrlib.transport import get_transport
392
from bzrlib.transport.local import LocalRelpathServer
393
from bzrlib.transport.http import HttpServer, HttpTransportBase
394
self.transport_server = LocalRelpathServer
395
self.transport_readonly_server = HttpServer
396
# calling get_readonly_transport() gives us a HTTP server instance.
397
url = self.get_readonly_url()
398
url2 = self.get_readonly_url('foo/bar')
399
# the transport returned may be any HttpTransportBase subclass
400
t = get_transport(url)
401
t2 = get_transport(url2)
402
self.failUnless(isinstance(t, HttpTransportBase))
403
self.failUnless(isinstance(t2, HttpTransportBase))
404
self.assertEqual(t2.base[:-1], t.abspath('foo/bar'))
406
def test_is_directory(self):
407
"""Test assertIsDirectory assertion"""
408
t = self.get_transport()
409
self.build_tree(['a_dir/', 'a_file'], transport=t)
410
self.assertIsDirectory('a_dir', t)
411
self.assertRaises(AssertionError, self.assertIsDirectory, 'a_file', t)
412
self.assertRaises(AssertionError, self.assertIsDirectory, 'not_here', t)
415
class TestChrootedTest(ChrootedTestCase):
417
def test_root_is_root(self):
418
from bzrlib.transport import get_transport
419
t = get_transport(self.get_readonly_url())
421
self.assertEqual(url, t.clone('..').base)
424
class MockProgress(_BaseProgressBar):
425
"""Progress-bar standin that records calls.
427
Useful for testing pb using code.
431
_BaseProgressBar.__init__(self)
435
self.calls.append(('tick',))
437
def update(self, msg=None, current=None, total=None):
438
self.calls.append(('update', msg, current, total))
441
self.calls.append(('clear',))
444
class TestTestResult(TestCase):
446
def test_progress_bar_style_quiet(self):
447
# test using a progress bar.
448
dummy_test = TestTestResult('test_progress_bar_style_quiet')
449
dummy_error = (Exception, None, [])
450
mypb = MockProgress()
451
mypb.update('Running tests', 0, 4)
452
last_calls = mypb.calls[:]
453
result = bzrlib.tests._MyResult(self._log_file,
457
self.assertEqual(last_calls, mypb.calls)
460
result.startTest(dummy_test)
461
# starting a test prints the test name
462
self.assertEqual(last_calls + [('update', '...tyle_quiet', 0, None)], mypb.calls)
463
last_calls = mypb.calls[:]
464
result.addError(dummy_test, dummy_error)
465
self.assertEqual(last_calls + [('update', 'ERROR ', 1, None)], mypb.calls)
466
last_calls = mypb.calls[:]
469
result.startTest(dummy_test)
470
self.assertEqual(last_calls + [('update', '...tyle_quiet', 1, None)], mypb.calls)
471
last_calls = mypb.calls[:]
472
result.addFailure(dummy_test, dummy_error)
473
self.assertEqual(last_calls + [('update', 'FAIL ', 2, None)], mypb.calls)
474
last_calls = mypb.calls[:]
477
result.startTest(dummy_test)
478
self.assertEqual(last_calls + [('update', '...tyle_quiet', 2, None)], mypb.calls)
479
last_calls = mypb.calls[:]
480
result.addSuccess(dummy_test)
481
self.assertEqual(last_calls + [('update', 'OK ', 3, None)], mypb.calls)
482
last_calls = mypb.calls[:]
485
result.startTest(dummy_test)
486
self.assertEqual(last_calls + [('update', '...tyle_quiet', 3, None)], mypb.calls)
487
last_calls = mypb.calls[:]
488
result.addSkipped(dummy_test, dummy_error)
489
self.assertEqual(last_calls + [('update', 'SKIP ', 4, None)], mypb.calls)
490
last_calls = mypb.calls[:]
492
def test_elapsed_time_with_benchmarking(self):
493
result = bzrlib.tests._MyResult(self._log_file,
497
result._recordTestStartTime()
499
result.extractBenchmarkTime(self)
500
timed_string = result._testTimeString()
501
# without explicit benchmarking, we should get a simple time.
502
self.assertContainsRe(timed_string, "^ [ 1-9][0-9]ms$")
503
# if a benchmark time is given, we want a x of y style result.
504
self.time(time.sleep, 0.001)
505
result.extractBenchmarkTime(self)
506
timed_string = result._testTimeString()
507
self.assertContainsRe(timed_string, "^ [ 1-9][0-9]ms/ [ 1-9][0-9]ms$")
508
# extracting the time from a non-bzrlib testcase sets to None
509
result._recordTestStartTime()
510
result.extractBenchmarkTime(
511
unittest.FunctionTestCase(self.test_elapsed_time_with_benchmarking))
512
timed_string = result._testTimeString()
513
self.assertContainsRe(timed_string, "^ [ 1-9][0-9]ms$")
514
# cheat. Yes, wash thy mouth out with soap.
515
self._benchtime = None
517
def _time_hello_world_encoding(self):
518
"""Profile two sleep calls
520
This is used to exercise the test framework.
522
self.time(unicode, 'hello', errors='replace')
523
self.time(unicode, 'world', errors='replace')
525
def test_lsprofiling(self):
526
"""Verbose test result prints lsprof statistics from test cases."""
530
raise TestSkipped("lsprof not installed.")
531
result_stream = StringIO()
532
result = bzrlib.tests._MyResult(
533
unittest._WritelnDecorator(result_stream),
537
# we want profile a call of some sort and check it is output by
538
# addSuccess. We dont care about addError or addFailure as they
539
# are not that interesting for performance tuning.
540
# make a new test instance that when run will generate a profile
541
example_test_case = TestTestResult("_time_hello_world_encoding")
542
example_test_case._gather_lsprof_in_benchmarks = True
543
# execute the test, which should succeed and record profiles
544
example_test_case.run(result)
545
# lsprofile_something()
546
# if this worked we want
547
# LSProf output for <built in function unicode> (['hello'], {'errors': 'replace'})
548
# CallCount Recursive Total(ms) Inline(ms) module:lineno(function)
549
# (the lsprof header)
550
# ... an arbitrary number of lines
551
# and the function call which is time.sleep.
552
# 1 0 ??? ??? ???(sleep)
553
# and then repeated but with 'world', rather than 'hello'.
554
# this should appear in the output stream of our test result.
555
output = result_stream.getvalue()
556
self.assertContainsRe(output,
557
r"LSProf output for <type 'unicode'>\(\('hello',\), {'errors': 'replace'}\)")
558
self.assertContainsRe(output,
559
r" *CallCount *Recursive *Total\(ms\) *Inline\(ms\) *module:lineno\(function\)\n")
560
self.assertContainsRe(output,
561
r"( +1 +0 +0\.\d+ +0\.\d+ +<method 'disable' of '_lsprof\.Profiler' objects>\n)?")
562
self.assertContainsRe(output,
563
r"LSProf output for <type 'unicode'>\(\('world',\), {'errors': 'replace'}\)\n")
566
class TestRunner(TestCase):
568
def dummy_test(self):
571
def run_test_runner(self, testrunner, test):
572
"""Run suite in testrunner, saving global state and restoring it.
574
This current saves and restores:
575
TestCaseInTempDir.TEST_ROOT
577
There should be no tests in this file that use bzrlib.tests.TextTestRunner
578
without using this convenience method, because of our use of global state.
580
old_root = TestCaseInTempDir.TEST_ROOT
582
TestCaseInTempDir.TEST_ROOT = None
583
return testrunner.run(test)
585
TestCaseInTempDir.TEST_ROOT = old_root
587
def test_accepts_and_uses_pb_parameter(self):
588
test = TestRunner('dummy_test')
589
mypb = MockProgress()
590
self.assertEqual([], mypb.calls)
591
runner = TextTestRunner(stream=self._log_file, pb=mypb)
592
result = self.run_test_runner(runner, test)
593
self.assertEqual(1, result.testsRun)
594
self.assertEqual(('update', 'Running tests', 0, 1), mypb.calls[0])
595
self.assertEqual(('update', '...dummy_test', 0, None), mypb.calls[1])
596
self.assertEqual(('update', 'OK ', 1, None), mypb.calls[2])
597
self.assertEqual(('update', 'Cleaning up', 0, 1), mypb.calls[3])
598
self.assertEqual(('clear',), mypb.calls[4])
599
self.assertEqual(5, len(mypb.calls))
601
def test_skipped_test(self):
602
# run a test that is skipped, and check the suite as a whole still
604
# skipping_test must be hidden in here so it's not run as a real test
606
raise TestSkipped('test intentionally skipped')
607
runner = TextTestRunner(stream=self._log_file, keep_output=True)
608
test = unittest.FunctionTestCase(skipping_test)
609
result = self.run_test_runner(runner, test)
610
self.assertTrue(result.wasSuccessful())
613
class TestTestCase(TestCase):
614
"""Tests that test the core bzrlib TestCase."""
616
def inner_test(self):
617
# the inner child test
620
def outer_child(self):
621
# the outer child test
623
self.inner_test = TestTestCase("inner_child")
624
result = bzrlib.tests._MyResult(self._log_file,
627
self.inner_test.run(result)
630
def test_trace_nesting(self):
631
# this tests that each test case nests its trace facility correctly.
632
# we do this by running a test case manually. That test case (A)
633
# should setup a new log, log content to it, setup a child case (B),
634
# which should log independently, then case (A) should log a trailer
636
# we do two nested children so that we can verify the state of the
637
# logs after the outer child finishes is correct, which a bad clean
638
# up routine in tearDown might trigger a fault in our test with only
639
# one child, we should instead see the bad result inside our test with
641
# the outer child test
642
original_trace = bzrlib.trace._trace_file
643
outer_test = TestTestCase("outer_child")
644
result = bzrlib.tests._MyResult(self._log_file,
647
outer_test.run(result)
648
self.assertEqual(original_trace, bzrlib.trace._trace_file)
650
def method_that_times_a_bit_twice(self):
651
# call self.time twice to ensure it aggregates
652
self.time(time.sleep, 0.007)
653
self.time(time.sleep, 0.007)
655
def test_time_creates_benchmark_in_result(self):
656
"""Test that the TestCase.time() method accumulates a benchmark time."""
657
sample_test = TestTestCase("method_that_times_a_bit_twice")
658
output_stream = StringIO()
659
result = bzrlib.tests._MyResult(
660
unittest._WritelnDecorator(output_stream),
663
sample_test.run(result)
664
self.assertContainsRe(
665
output_stream.getvalue(),
666
"[1-9][0-9]ms/ [1-9][0-9]ms\n$")
668
def test__gather_lsprof_in_benchmarks(self):
669
"""When _gather_lsprof_in_benchmarks is on, accumulate profile data.
671
Each self.time() call is individually and separately profiled.
676
raise TestSkipped("lsprof not installed.")
677
# overrides the class member with an instance member so no cleanup
679
self._gather_lsprof_in_benchmarks = True
680
self.time(time.sleep, 0.000)
681
self.time(time.sleep, 0.003)
682
self.assertEqual(2, len(self._benchcalls))
683
self.assertEqual((time.sleep, (0.000,), {}), self._benchcalls[0][0])
684
self.assertEqual((time.sleep, (0.003,), {}), self._benchcalls[1][0])
685
self.assertIsInstance(self._benchcalls[0][1], bzrlib.lsprof.Stats)
686
self.assertIsInstance(self._benchcalls[1][1], bzrlib.lsprof.Stats)
689
class TestExtraAssertions(TestCase):
690
"""Tests for new test assertions in bzrlib test suite"""
692
def test_assert_isinstance(self):
693
self.assertIsInstance(2, int)
694
self.assertIsInstance(u'', basestring)
695
self.assertRaises(AssertionError, self.assertIsInstance, None, int)
696
self.assertRaises(AssertionError, self.assertIsInstance, 23.3, int)
698
def test_assertEndsWith(self):
699
self.assertEndsWith('foo', 'oo')
700
self.assertRaises(AssertionError, self.assertEndsWith, 'o', 'oo')
703
class TestConvenienceMakers(TestCaseWithTransport):
704
"""Test for the make_* convenience functions."""
706
def test_make_branch_and_tree_with_format(self):
707
# we should be able to supply a format to make_branch_and_tree
708
self.make_branch_and_tree('a', format=bzrlib.bzrdir.BzrDirMetaFormat1())
709
self.make_branch_and_tree('b', format=bzrlib.bzrdir.BzrDirFormat6())
710
self.assertIsInstance(bzrlib.bzrdir.BzrDir.open('a')._format,
711
bzrlib.bzrdir.BzrDirMetaFormat1)
712
self.assertIsInstance(bzrlib.bzrdir.BzrDir.open('b')._format,
713
bzrlib.bzrdir.BzrDirFormat6)
716
class TestSelftest(TestCase):
717
"""Tests of bzrlib.tests.selftest."""
719
def test_selftest_benchmark_parameter_invokes_test_suite__benchmark__(self):
722
factory_called.append(True)
726
self.apply_redirected(out, err, None, bzrlib.tests.selftest,
727
test_suite_factory=factory)
728
self.assertEqual([True], factory_called)
730
def test_run_bzr_subprocess(self):
731
"""The run_bzr_helper_external comand behaves nicely."""
732
result = self.run_bzr_subprocess('--version')
733
result = self.run_bzr_subprocess('--version', retcode=None)
734
self.assertContainsRe(result[0], 'is free software')
735
self.assertRaises(AssertionError, self.run_bzr_subprocess,
737
result = self.run_bzr_subprocess('--versionn', retcode=3)
738
result = self.run_bzr_subprocess('--versionn', retcode=None)
739
self.assertContainsRe(result[1], 'unknown command')
740
err = self.run_bzr_subprocess('merge', '--merge-type', 'magic merge',
742
self.assertContainsRe(err, 'No known merge type magic merge')
744
def test_run_bzr_error(self):
745
out, err = self.run_bzr_error(['^$'], 'rocks', retcode=0)
746
self.assertEqual(out, 'it sure does!\n')
748
out, err = self.run_bzr_error(["'foobarbaz' is not a versioned file"],
749
'file-id', 'foobarbaz')