755
749
self.check_timing(ShortDelayTestCase('test_short_delay'),
758
def _patch_get_bzr_source_tree(self):
759
# Reading from the actual source tree breaks isolation, but we don't
760
# want to assume that thats *all* that would happen.
761
self.overrideAttr(bzrlib.version, '_get_bzr_source_tree', lambda: None)
763
def test_assigned_benchmark_file_stores_date(self):
764
self._patch_get_bzr_source_tree()
766
result = bzrlib.tests.TextTestResult(self._log_file,
771
output_string = output.getvalue()
772
# if you are wondering about the regexp please read the comment in
773
# test_bench_history (bzrlib.tests.test_selftest.TestRunner)
774
# XXX: what comment? -- Andrew Bennetts
775
self.assertContainsRe(output_string, "--date [0-9.]+")
777
def test_benchhistory_records_test_times(self):
778
self._patch_get_bzr_source_tree()
779
result_stream = StringIO()
780
result = bzrlib.tests.TextTestResult(
784
bench_history=result_stream
787
# we want profile a call and check that its test duration is recorded
788
# make a new test instance that when run will generate a benchmark
789
example_test_case = TestTestResult("_time_hello_world_encoding")
790
# execute the test, which should succeed and record times
791
example_test_case.run(result)
792
lines = result_stream.getvalue().splitlines()
793
self.assertEqual(2, len(lines))
794
self.assertContainsRe(lines[1],
795
" *[0-9]+ms bzrlib.tests.test_selftest.TestTestResult"
796
"._time_hello_world_encoding")
798
752
def _time_hello_world_encoding(self):
799
753
"""Profile two sleep calls
1222
def _patch_get_bzr_source_tree(self):
1223
# Reading from the actual source tree breaks isolation, but we don't
1224
# want to assume that thats *all* that would happen.
1225
self._get_source_tree_calls = []
1227
self._get_source_tree_calls.append("called")
1229
self.overrideAttr(bzrlib.version, '_get_bzr_source_tree', new_get)
1231
def test_bench_history(self):
1232
# tests that the running the benchmark passes bench_history into
1233
# the test result object. We can tell that happens if
1234
# _get_bzr_source_tree is called.
1235
self._patch_get_bzr_source_tree()
1236
test = TestRunner('dummy_test')
1238
runner = tests.TextTestRunner(stream=self._log_file,
1239
bench_history=output)
1240
result = self.run_test_runner(runner, test)
1241
output_string = output.getvalue()
1242
self.assertContainsRe(output_string, "--date [0-9.]+")
1243
self.assertLength(1, self._get_source_tree_calls)
1245
1176
def test_verbose_test_count(self):
1246
1177
"""A verbose test run reports the right test count at the start"""
1247
1178
suite = TestUtil.TestSuite([
3394
3337
self.verbosity)
3395
3338
tests.run_suite(suite, runner_class=MyRunner, stream=StringIO())
3396
3339
self.assertLength(1, calls)
3342
class TestEnvironHandling(tests.TestCase):
3344
def test_overrideEnv_None_called_twice_doesnt_leak(self):
3345
self.failIf('MYVAR' in os.environ)
3346
self.overrideEnv('MYVAR', '42')
3347
# We use an embedded test to make sure we fix the _captureVar bug
3348
class Test(tests.TestCase):
3350
# The first call save the 42 value
3351
self.overrideEnv('MYVAR', None)
3352
self.assertEquals(None, os.environ.get('MYVAR'))
3353
# Make sure we can call it twice
3354
self.overrideEnv('MYVAR', None)
3355
self.assertEquals(None, os.environ.get('MYVAR'))
3357
result = tests.TextTestResult(output, 0, 1)
3358
Test('test_me').run(result)
3359
if not result.wasStrictlySuccessful():
3360
self.fail(output.getvalue())
3361
# We get our value back
3362
self.assertEquals('42', os.environ.get('MYVAR'))
3365
class TestIsolatedEnv(tests.TestCase):
3366
"""Test isolating tests from os.environ.
3368
Since we use tests that are already isolated from os.environ a bit of care
3369
should be taken when designing the tests to avoid bootstrap side-effects.
3370
The tests start an already clean os.environ which allow doing valid
3371
assertions about which variables are present or not and design tests around
3375
class ScratchMonkey(tests.TestCase):
3380
def test_basics(self):
3381
# Make sure we know the definition of BZR_HOME: not part of os.environ
3382
# for tests.TestCase.
3383
self.assertTrue('BZR_HOME' in tests.isolated_environ)
3384
self.assertEquals(None, tests.isolated_environ['BZR_HOME'])
3385
# Being part of isolated_environ, BZR_HOME should not appear here
3386
self.assertFalse('BZR_HOME' in os.environ)
3387
# Make sure we know the definition of LINES: part of os.environ for
3389
self.assertTrue('LINES' in tests.isolated_environ)
3390
self.assertEquals('25', tests.isolated_environ['LINES'])
3391
self.assertEquals('25', os.environ['LINES'])
3393
def test_injecting_unknown_variable(self):
3394
# BZR_HOME is known to be absent from os.environ
3395
test = self.ScratchMonkey('test_me')
3396
tests.override_os_environ(test, {'BZR_HOME': 'foo'})
3397
self.assertEquals('foo', os.environ['BZR_HOME'])
3398
tests.restore_os_environ(test)
3399
self.assertFalse('BZR_HOME' in os.environ)
3401
def test_injecting_known_variable(self):
3402
test = self.ScratchMonkey('test_me')
3403
# LINES is known to be present in os.environ
3404
tests.override_os_environ(test, {'LINES': '42'})
3405
self.assertEquals('42', os.environ['LINES'])
3406
tests.restore_os_environ(test)
3407
self.assertEquals('25', os.environ['LINES'])
3409
def test_deleting_variable(self):
3410
test = self.ScratchMonkey('test_me')
3411
# LINES is known to be present in os.environ
3412
tests.override_os_environ(test, {'LINES': None})
3413
self.assertTrue('LINES' not in os.environ)
3414
tests.restore_os_environ(test)
3415
self.assertEquals('25', os.environ['LINES'])
3418
class TestDocTestSuiteIsolation(tests.TestCase):
3419
"""Test that `tests.DocTestSuite` isolates doc tests from os.environ.
3421
Since tests.TestCase alreay provides an isolation from os.environ, we use
3422
the clean environment as a base for testing. To precisely capture the
3423
isolation provided by tests.DocTestSuite, we use doctest.DocTestSuite to
3426
We want to make sure `tests.DocTestSuite` respect `tests.isolated_environ`,
3427
not `os.environ` so each test overrides it to suit its needs.
3431
def get_doctest_suite_for_string(self, klass, string):
3432
class Finder(doctest.DocTestFinder):
3434
def find(*args, **kwargs):
3435
test = doctest.DocTestParser().get_doctest(
3436
string, {}, 'foo', 'foo.py', 0)
3439
suite = klass(test_finder=Finder())
3442
def run_doctest_suite_for_string(self, klass, string):
3443
suite = self.get_doctest_suite_for_string(klass, string)
3445
result = tests.TextTestResult(output, 0, 1)
3447
return result, output
3449
def assertDocTestStringSucceds(self, klass, string):
3450
result, output = self.run_doctest_suite_for_string(klass, string)
3451
if not result.wasStrictlySuccessful():
3452
self.fail(output.getvalue())
3454
def assertDocTestStringFails(self, klass, string):
3455
result, output = self.run_doctest_suite_for_string(klass, string)
3456
if result.wasStrictlySuccessful():
3457
self.fail(output.getvalue())
3459
def test_injected_variable(self):
3460
self.overrideAttr(tests, 'isolated_environ', {'LINES': '42'})
3463
>>> os.environ['LINES']
3466
# doctest.DocTestSuite fails as it sees '25'
3467
self.assertDocTestStringFails(doctest.DocTestSuite, test)
3468
# tests.DocTestSuite sees '42'
3469
self.assertDocTestStringSucceds(tests.IsolatedDocTestSuite, test)
3471
def test_deleted_variable(self):
3472
self.overrideAttr(tests, 'isolated_environ', {'LINES': None})
3475
>>> os.environ.get('LINES')
3477
# doctest.DocTestSuite fails as it sees '25'
3478
self.assertDocTestStringFails(doctest.DocTestSuite, test)
3479
# tests.DocTestSuite sees None
3480
self.assertDocTestStringSucceds(tests.IsolatedDocTestSuite, test)