571
564
tree = self.make_branch_and_memory_tree('dir')
572
565
# Guard against regression into MemoryTransport leaking
573
566
# files to disk instead of keeping them in memory.
574
self.assertFalse(osutils.lexists('dir'))
567
self.failIf(osutils.lexists('dir'))
575
568
self.assertIsInstance(tree, memorytree.MemoryTree)
577
570
def test_make_branch_and_memory_tree_with_format(self):
578
571
"""make_branch_and_memory_tree should accept a format option."""
579
572
format = bzrdir.BzrDirMetaFormat1()
580
format.repository_format = repository.format_registry.get_default()
573
format.repository_format = weaverepo.RepositoryFormat7()
581
574
tree = self.make_branch_and_memory_tree('dir', format=format)
582
575
# Guard against regression into MemoryTransport leaking
583
576
# files to disk instead of keeping them in memory.
584
self.assertFalse(osutils.lexists('dir'))
577
self.failIf(osutils.lexists('dir'))
585
578
self.assertIsInstance(tree, memorytree.MemoryTree)
586
579
self.assertEqual(format.repository_format.__class__,
587
580
tree.branch.repository._format.__class__)
591
584
self.assertIsInstance(builder, branchbuilder.BranchBuilder)
592
585
# Guard against regression into MemoryTransport leaking
593
586
# files to disk instead of keeping them in memory.
594
self.assertFalse(osutils.lexists('dir'))
587
self.failIf(osutils.lexists('dir'))
596
589
def test_make_branch_builder_with_format(self):
597
590
# Use a repo layout that doesn't conform to a 'named' layout, to ensure
598
591
# that the format objects are used.
599
592
format = bzrdir.BzrDirMetaFormat1()
600
repo_format = repository.format_registry.get_default()
593
repo_format = weaverepo.RepositoryFormat7()
601
594
format.repository_format = repo_format
602
595
builder = self.make_branch_builder('dir', format=format)
603
596
the_branch = builder.get_branch()
604
597
# Guard against regression into MemoryTransport leaking
605
598
# files to disk instead of keeping them in memory.
606
self.assertFalse(osutils.lexists('dir'))
599
self.failIf(osutils.lexists('dir'))
607
600
self.assertEqual(format.repository_format.__class__,
608
601
the_branch.repository._format.__class__)
609
602
self.assertEqual(repo_format.get_format_string(),
771
764
self.check_timing(ShortDelayTestCase('test_short_delay'),
767
def _patch_get_bzr_source_tree(self):
768
# Reading from the actual source tree breaks isolation, but we don't
769
# want to assume that thats *all* that would happen.
770
self.overrideAttr(bzrlib.version, '_get_bzr_source_tree', lambda: None)
772
def test_assigned_benchmark_file_stores_date(self):
773
self._patch_get_bzr_source_tree()
775
result = bzrlib.tests.TextTestResult(self._log_file,
780
output_string = output.getvalue()
781
# if you are wondering about the regexp please read the comment in
782
# test_bench_history (bzrlib.tests.test_selftest.TestRunner)
783
# XXX: what comment? -- Andrew Bennetts
784
self.assertContainsRe(output_string, "--date [0-9.]+")
786
def test_benchhistory_records_test_times(self):
787
self._patch_get_bzr_source_tree()
788
result_stream = StringIO()
789
result = bzrlib.tests.TextTestResult(
793
bench_history=result_stream
796
# we want profile a call and check that its test duration is recorded
797
# make a new test instance that when run will generate a benchmark
798
example_test_case = TestTestResult("_time_hello_world_encoding")
799
# execute the test, which should succeed and record times
800
example_test_case.run(result)
801
lines = result_stream.getvalue().splitlines()
802
self.assertEqual(2, len(lines))
803
self.assertContainsRe(lines[1],
804
" *[0-9]+ms bzrlib.tests.test_selftest.TestTestResult"
805
"._time_hello_world_encoding")
774
807
def _time_hello_world_encoding(self):
775
808
"""Profile two sleep calls
816
849
self.assertContainsRe(output,
817
850
r"LSProf output for <type 'unicode'>\(\('world',\), {'errors': 'replace'}\)\n")
819
def test_uses_time_from_testtools(self):
820
"""Test case timings in verbose results should use testtools times"""
822
class TimeAddedVerboseTestResult(tests.VerboseTestResult):
823
def startTest(self, test):
824
self.time(datetime.datetime.utcfromtimestamp(1.145))
825
super(TimeAddedVerboseTestResult, self).startTest(test)
826
def addSuccess(self, test):
827
self.time(datetime.datetime.utcfromtimestamp(51.147))
828
super(TimeAddedVerboseTestResult, self).addSuccess(test)
829
def report_tests_starting(self): pass
831
self.get_passing_test().run(TimeAddedVerboseTestResult(sio, 0, 2))
832
self.assertEndsWith(sio.getvalue(), "OK 50002ms\n")
834
852
def test_known_failure(self):
835
853
"""A KnownFailure being raised should trigger several result actions."""
836
854
class InstrumentedTestResult(tests.ExtendedTestResult):
1216
def _patch_get_bzr_source_tree(self):
1217
# Reading from the actual source tree breaks isolation, but we don't
1218
# want to assume that thats *all* that would happen.
1219
self._get_source_tree_calls = []
1221
self._get_source_tree_calls.append("called")
1223
self.overrideAttr(bzrlib.version, '_get_bzr_source_tree', new_get)
1225
def test_bench_history(self):
1226
# tests that the running the benchmark passes bench_history into
1227
# the test result object. We can tell that happens if
1228
# _get_bzr_source_tree is called.
1229
self._patch_get_bzr_source_tree()
1230
test = TestRunner('dummy_test')
1232
runner = tests.TextTestRunner(stream=self._log_file,
1233
bench_history=output)
1234
result = self.run_test_runner(runner, test)
1235
output_string = output.getvalue()
1236
self.assertContainsRe(output_string, "--date [0-9.]+")
1237
self.assertLength(1, self._get_source_tree_calls)
1198
1239
def test_verbose_test_count(self):
1199
1240
"""A verbose test run reports the right test count at the start"""
1200
1241
suite = TestUtil.TestSuite([
3273
3301
self.assertContainsString(result.stream.getvalue(), "leaking threads")
3276
class TestPostMortemDebugging(tests.TestCase):
3277
"""Check post mortem debugging works when tests fail or error"""
3279
class TracebackRecordingResult(tests.ExtendedTestResult):
3281
tests.ExtendedTestResult.__init__(self, StringIO(), 0, 1)
3282
self.postcode = None
3283
def _post_mortem(self, tb=None):
3284
"""Record the code object at the end of the current traceback"""
3285
tb = tb or sys.exc_info()[2]
3288
while next is not None:
3291
self.postcode = tb.tb_frame.f_code
3292
def report_error(self, test, err):
3294
def report_failure(self, test, err):
3297
def test_location_unittest_error(self):
3298
"""Needs right post mortem traceback with erroring unittest case"""
3299
class Test(unittest.TestCase):
3302
result = self.TracebackRecordingResult()
3304
self.assertEqual(result.postcode, Test.runTest.func_code)
3306
def test_location_unittest_failure(self):
3307
"""Needs right post mortem traceback with failing unittest case"""
3308
class Test(unittest.TestCase):
3310
raise self.failureException
3311
result = self.TracebackRecordingResult()
3313
self.assertEqual(result.postcode, Test.runTest.func_code)
3315
def test_location_bt_error(self):
3316
"""Needs right post mortem traceback with erroring bzrlib.tests case"""
3317
class Test(tests.TestCase):
3318
def test_error(self):
3320
result = self.TracebackRecordingResult()
3321
Test("test_error").run(result)
3322
self.assertEqual(result.postcode, Test.test_error.func_code)
3324
def test_location_bt_failure(self):
3325
"""Needs right post mortem traceback with failing bzrlib.tests case"""
3326
class Test(tests.TestCase):
3327
def test_failure(self):
3328
raise self.failureException
3329
result = self.TracebackRecordingResult()
3330
Test("test_failure").run(result)
3331
self.assertEqual(result.postcode, Test.test_failure.func_code)
3333
def test_env_var_triggers_post_mortem(self):
3334
"""Check pdb.post_mortem is called iff BZR_TEST_PDB is set"""
3336
result = tests.ExtendedTestResult(StringIO(), 0, 1)
3337
post_mortem_calls = []
3338
self.overrideAttr(pdb, "post_mortem", post_mortem_calls.append)
3339
self.overrideEnv('BZR_TEST_PDB', None)
3340
result._post_mortem(1)
3341
self.overrideEnv('BZR_TEST_PDB', 'on')
3342
result._post_mortem(2)
3343
self.assertEqual([2], post_mortem_calls)
3346
3304
class TestRunSuite(tests.TestCase):
3348
3306
def test_runner_class(self):
3359
3317
self.verbosity)
3360
3318
tests.run_suite(suite, runner_class=MyRunner, stream=StringIO())
3361
3319
self.assertLength(1, calls)
3364
class TestEnvironHandling(tests.TestCase):
3366
def test_overrideEnv_None_called_twice_doesnt_leak(self):
3367
self.assertFalse('MYVAR' in os.environ)
3368
self.overrideEnv('MYVAR', '42')
3369
# We use an embedded test to make sure we fix the _captureVar bug
3370
class Test(tests.TestCase):
3372
# The first call save the 42 value
3373
self.overrideEnv('MYVAR', None)
3374
self.assertEquals(None, os.environ.get('MYVAR'))
3375
# Make sure we can call it twice
3376
self.overrideEnv('MYVAR', None)
3377
self.assertEquals(None, os.environ.get('MYVAR'))
3379
result = tests.TextTestResult(output, 0, 1)
3380
Test('test_me').run(result)
3381
if not result.wasStrictlySuccessful():
3382
self.fail(output.getvalue())
3383
# We get our value back
3384
self.assertEquals('42', os.environ.get('MYVAR'))
3387
class TestIsolatedEnv(tests.TestCase):
3388
"""Test isolating tests from os.environ.
3390
Since we use tests that are already isolated from os.environ a bit of care
3391
should be taken when designing the tests to avoid bootstrap side-effects.
3392
The tests start an already clean os.environ which allow doing valid
3393
assertions about which variables are present or not and design tests around
3397
class ScratchMonkey(tests.TestCase):
3402
def test_basics(self):
3403
# Make sure we know the definition of BZR_HOME: not part of os.environ
3404
# for tests.TestCase.
3405
self.assertTrue('BZR_HOME' in tests.isolated_environ)
3406
self.assertEquals(None, tests.isolated_environ['BZR_HOME'])
3407
# Being part of isolated_environ, BZR_HOME should not appear here
3408
self.assertFalse('BZR_HOME' in os.environ)
3409
# Make sure we know the definition of LINES: part of os.environ for
3411
self.assertTrue('LINES' in tests.isolated_environ)
3412
self.assertEquals('25', tests.isolated_environ['LINES'])
3413
self.assertEquals('25', os.environ['LINES'])
3415
def test_injecting_unknown_variable(self):
3416
# BZR_HOME is known to be absent from os.environ
3417
test = self.ScratchMonkey('test_me')
3418
tests.override_os_environ(test, {'BZR_HOME': 'foo'})
3419
self.assertEquals('foo', os.environ['BZR_HOME'])
3420
tests.restore_os_environ(test)
3421
self.assertFalse('BZR_HOME' in os.environ)
3423
def test_injecting_known_variable(self):
3424
test = self.ScratchMonkey('test_me')
3425
# LINES is known to be present in os.environ
3426
tests.override_os_environ(test, {'LINES': '42'})
3427
self.assertEquals('42', os.environ['LINES'])
3428
tests.restore_os_environ(test)
3429
self.assertEquals('25', os.environ['LINES'])
3431
def test_deleting_variable(self):
3432
test = self.ScratchMonkey('test_me')
3433
# LINES is known to be present in os.environ
3434
tests.override_os_environ(test, {'LINES': None})
3435
self.assertTrue('LINES' not in os.environ)
3436
tests.restore_os_environ(test)
3437
self.assertEquals('25', os.environ['LINES'])
3440
class TestDocTestSuiteIsolation(tests.TestCase):
3441
"""Test that `tests.DocTestSuite` isolates doc tests from os.environ.
3443
Since tests.TestCase alreay provides an isolation from os.environ, we use
3444
the clean environment as a base for testing. To precisely capture the
3445
isolation provided by tests.DocTestSuite, we use doctest.DocTestSuite to
3448
We want to make sure `tests.DocTestSuite` respect `tests.isolated_environ`,
3449
not `os.environ` so each test overrides it to suit its needs.
3453
def get_doctest_suite_for_string(self, klass, string):
3454
class Finder(doctest.DocTestFinder):
3456
def find(*args, **kwargs):
3457
test = doctest.DocTestParser().get_doctest(
3458
string, {}, 'foo', 'foo.py', 0)
3461
suite = klass(test_finder=Finder())
3464
def run_doctest_suite_for_string(self, klass, string):
3465
suite = self.get_doctest_suite_for_string(klass, string)
3467
result = tests.TextTestResult(output, 0, 1)
3469
return result, output
3471
def assertDocTestStringSucceds(self, klass, string):
3472
result, output = self.run_doctest_suite_for_string(klass, string)
3473
if not result.wasStrictlySuccessful():
3474
self.fail(output.getvalue())
3476
def assertDocTestStringFails(self, klass, string):
3477
result, output = self.run_doctest_suite_for_string(klass, string)
3478
if result.wasStrictlySuccessful():
3479
self.fail(output.getvalue())
3481
def test_injected_variable(self):
3482
self.overrideAttr(tests, 'isolated_environ', {'LINES': '42'})
3485
>>> os.environ['LINES']
3488
# doctest.DocTestSuite fails as it sees '25'
3489
self.assertDocTestStringFails(doctest.DocTestSuite, test)
3490
# tests.DocTestSuite sees '42'
3491
self.assertDocTestStringSucceds(tests.IsolatedDocTestSuite, test)
3493
def test_deleted_variable(self):
3494
self.overrideAttr(tests, 'isolated_environ', {'LINES': None})
3497
>>> os.environ.get('LINES')
3499
# doctest.DocTestSuite fails as it sees '25'
3500
self.assertDocTestStringFails(doctest.DocTestSuite, test)
3501
# tests.DocTestSuite sees None
3502
self.assertDocTestStringSucceds(tests.IsolatedDocTestSuite, test)