87
78
TestUtil._load_module_by_name,
88
79
'bzrlib.no-name-yet')
91
81
class MetaTestLog(tests.TestCase):
93
83
def test_logging(self):
94
84
"""Test logs are captured when a test fails."""
95
85
self.log('a test message')
96
details = self.getDetails()
98
self.assertThat(log.content_type, Equals(ContentType(
99
"text", "plain", {"charset": "utf8"})))
100
self.assertThat(u"".join(log.iter_text()), Equals(self.get_log()))
101
self.assertThat(self.get_log(),
102
DocTestMatches(u"...a test message\n", ELLIPSIS))
86
self._log_file.flush()
87
self.assertContainsRe(self._get_log(keep_log_file=True),
105
91
class TestUnicodeFilename(tests.TestCase):
709
687
def sample_function(self):
711
689
test = Sample("a")
690
test.attrs_to_keep = test.attrs_to_keep + ('_benchcalls',)
713
case = terminal._events[0][1]
714
self.assertLength(1, case._benchcalls)
692
self.assertLength(1, test._benchcalls)
715
693
# We must be able to unpack it as the test reporting code wants
716
(_, _, _), stats = case._benchcalls[0]
694
(_, _, _), stats = test._benchcalls[0]
717
695
self.assertTrue(callable(stats.pprint))
848
824
def stopTestRun(self): pass
849
825
def startTests(self): pass
850
826
def report_test_start(self, test): pass
851
def report_known_failure(self, test, err=None, details=None):
852
self._call = test, 'known failure'
827
def report_known_failure(self, test, err):
828
self._call = test, err
853
829
result = InstrumentedTestResult(None, None, None, None)
854
class Test(tests.TestCase):
855
def test_function(self):
856
raise tests.KnownFailure('failed!')
857
test = Test("test_function")
831
raise tests.KnownFailure('failed!')
832
test = unittest.FunctionTestCase(test_function)
859
834
# it should invoke 'report_known_failure'.
860
835
self.assertEqual(2, len(result._call))
861
self.assertEqual(test.id(), result._call[0].id())
862
self.assertEqual('known failure', result._call[1])
836
self.assertEqual(test, result._call[0])
837
self.assertEqual(tests.KnownFailure, result._call[1][0])
838
self.assertIsInstance(result._call[1][1], tests.KnownFailure)
863
839
# we dont introspec the traceback, if the rest is ok, it would be
864
840
# exceptional for it not to be.
865
841
# it should update the known_failure_count on the object.
937
913
result.report_unsupported(test, feature)
938
914
output = result_stream.getvalue()[prefix:]
939
915
lines = output.splitlines()
940
# We don't check for the final '0ms' since it may fail on slow hosts
941
self.assertStartsWith(lines[0], 'NODEP')
942
self.assertEqual(lines[1],
943
" The feature 'Feature' is not available.")
916
self.assertEqual(lines, ['NODEP 0ms',
917
" The feature 'Feature' is not available."])
945
919
def test_unavailable_exception(self):
946
920
"""An UnavailableFeature being raised should invoke addNotSupported."""
952
926
self._call = test, feature
953
927
result = InstrumentedTestResult(None, None, None, None)
954
928
feature = tests.Feature()
955
class Test(tests.TestCase):
956
def test_function(self):
957
raise tests.UnavailableFeature(feature)
958
test = Test("test_function")
930
raise tests.UnavailableFeature(feature)
931
test = unittest.FunctionTestCase(test_function)
960
933
# it should invoke 'addNotSupported'.
961
934
self.assertEqual(2, len(result._call))
962
self.assertEqual(test.id(), result._call[0].id())
935
self.assertEqual(test, result._call[0])
963
936
self.assertEqual(feature, result._call[1])
964
937
# and not count as an error
965
938
self.assertEqual(0, result.error_count)
1041
1014
def test_known_failure_failed_run(self):
1042
1015
# run a test that generates a known failure which should be printed in
1043
1016
# the final output when real failures occur.
1044
class Test(tests.TestCase):
1045
def known_failure_test(self):
1046
self.expectFailure('failed', self.assertTrue, False)
1017
def known_failure_test():
1018
raise tests.KnownFailure('failed')
1047
1019
test = unittest.TestSuite()
1048
test.addTest(Test("known_failure_test"))
1020
test.addTest(unittest.FunctionTestCase(known_failure_test))
1049
1021
def failing_test():
1022
raise AssertionError('foo')
1051
1023
test.addTest(unittest.FunctionTestCase(failing_test))
1052
1024
stream = StringIO()
1053
1025
runner = tests.TextTestRunner(stream=stream)
1054
1026
result = self.run_test_runner(runner, test)
1055
1027
lines = stream.getvalue().splitlines()
1056
1028
self.assertContainsRe(stream.getvalue(),
1057
'(?sm)^bzr selftest.*$'
1059
1031
'^======================================================================\n'
1060
1032
'^FAIL: unittest.FunctionTestCase \\(failing_test\\)\n'
1061
1033
'^----------------------------------------------------------------------\n'
1062
1034
'Traceback \\(most recent call last\\):\n'
1063
1035
' .*' # File .*, line .*, in failing_test' - but maybe not from .pyc
1064
' self.fail\\(\'foo\'\\)\n'
1036
' raise AssertionError\\(\'foo\'\\)\n'
1066
1038
'^----------------------------------------------------------------------\n'
1071
1043
def test_known_failure_ok_run(self):
1072
# run a test that generates a known failure which should be printed in
1074
class Test(tests.TestCase):
1075
def known_failure_test(self):
1076
self.expectFailure('failed', self.assertTrue, False)
1077
test = Test("known_failure_test")
1044
# run a test that generates a known failure which should be printed in the final output.
1045
def known_failure_test():
1046
raise tests.KnownFailure('failed')
1047
test = unittest.FunctionTestCase(known_failure_test)
1078
1048
stream = StringIO()
1079
1049
runner = tests.TextTestRunner(stream=stream)
1080
1050
result = self.run_test_runner(runner, test)
1158
1128
def test_not_applicable(self):
1159
1129
# run a test that is skipped because it's not applicable
1160
class Test(tests.TestCase):
1161
def not_applicable_test(self):
1162
raise tests.TestNotApplicable('this test never runs')
1130
def not_applicable_test():
1131
raise tests.TestNotApplicable('this test never runs')
1163
1132
out = StringIO()
1164
1133
runner = tests.TextTestRunner(stream=out, verbosity=2)
1165
test = Test("not_applicable_test")
1134
test = unittest.FunctionTestCase(not_applicable_test)
1166
1135
result = self.run_test_runner(runner, test)
1167
1136
self._log_file.write(out.getvalue())
1168
1137
self.assertTrue(result.wasSuccessful())
1224
1193
self.assertContainsRe(output_string, "--date [0-9.]+")
1225
1194
self.assertLength(1, self._get_source_tree_calls)
1196
def assertLogDeleted(self, test):
1197
log = test._get_log()
1198
self.assertEqual("DELETED log file to reduce memory footprint", log)
1199
self.assertEqual('', test._log_contents)
1200
self.assertIs(None, test._log_file_name)
1202
def test_success_log_deleted(self):
1203
"""Successful tests have their log deleted"""
1205
class LogTester(tests.TestCase):
1207
def test_success(self):
1208
self.log('this will be removed\n')
1211
runner = tests.TextTestRunner(stream=sio)
1212
test = LogTester('test_success')
1213
result = self.run_test_runner(runner, test)
1215
self.assertLogDeleted(test)
1217
def test_skipped_log_deleted(self):
1218
"""Skipped tests have their log deleted"""
1220
class LogTester(tests.TestCase):
1222
def test_skipped(self):
1223
self.log('this will be removed\n')
1224
raise tests.TestSkipped()
1227
runner = tests.TextTestRunner(stream=sio)
1228
test = LogTester('test_skipped')
1229
result = self.run_test_runner(runner, test)
1231
self.assertLogDeleted(test)
1233
def test_not_aplicable_log_deleted(self):
1234
"""Not applicable tests have their log deleted"""
1236
class LogTester(tests.TestCase):
1238
def test_not_applicable(self):
1239
self.log('this will be removed\n')
1240
raise tests.TestNotApplicable()
1243
runner = tests.TextTestRunner(stream=sio)
1244
test = LogTester('test_not_applicable')
1245
result = self.run_test_runner(runner, test)
1247
self.assertLogDeleted(test)
1249
def test_known_failure_log_deleted(self):
1250
"""Know failure tests have their log deleted"""
1252
class LogTester(tests.TestCase):
1254
def test_known_failure(self):
1255
self.log('this will be removed\n')
1256
raise tests.KnownFailure()
1259
runner = tests.TextTestRunner(stream=sio)
1260
test = LogTester('test_known_failure')
1261
result = self.run_test_runner(runner, test)
1263
self.assertLogDeleted(test)
1265
def test_fail_log_kept(self):
1266
"""Failed tests have their log kept"""
1268
class LogTester(tests.TestCase):
1270
def test_fail(self):
1271
self.log('this will be kept\n')
1272
self.fail('this test fails')
1275
runner = tests.TextTestRunner(stream=sio)
1276
test = LogTester('test_fail')
1277
result = self.run_test_runner(runner, test)
1279
text = sio.getvalue()
1280
self.assertContainsRe(text, 'this will be kept')
1281
self.assertContainsRe(text, 'this test fails')
1283
log = test._get_log()
1284
self.assertContainsRe(log, 'this will be kept')
1285
self.assertEqual(log, test._log_contents)
1287
def test_error_log_kept(self):
1288
"""Tests with errors have their log kept"""
1290
class LogTester(tests.TestCase):
1292
def test_error(self):
1293
self.log('this will be kept\n')
1294
raise ValueError('random exception raised')
1297
runner = tests.TextTestRunner(stream=sio)
1298
test = LogTester('test_error')
1299
result = self.run_test_runner(runner, test)
1301
text = sio.getvalue()
1302
self.assertContainsRe(text, 'this will be kept')
1303
self.assertContainsRe(text, 'random exception raised')
1305
log = test._get_log()
1306
self.assertContainsRe(log, 'this will be kept')
1307
self.assertEqual(log, test._log_contents)
1227
1309
def test_startTestRun(self):
1228
1310
"""run should call result.startTestRun()"""
2337
2418
self.assertEqual('bzr: interrupted\n', result[1])
2421
class TestKnownFailure(tests.TestCase):
2423
def test_known_failure(self):
2424
"""Check that KnownFailure is defined appropriately."""
2425
# a KnownFailure is an assertion error for compatability with unaware
2427
self.assertIsInstance(tests.KnownFailure(""), AssertionError)
2429
def test_expect_failure(self):
2431
self.expectFailure("Doomed to failure", self.assertTrue, False)
2432
except tests.KnownFailure, e:
2433
self.assertEqual('Doomed to failure', e.args[0])
2435
self.expectFailure("Doomed to failure", self.assertTrue, True)
2436
except AssertionError, e:
2437
self.assertEqual('Unexpected success. Should have failed:'
2438
' Doomed to failure', e.args[0])
2440
self.fail('Assertion not raised')
2340
2443
class TestFeature(tests.TestCase):
2342
2445
def test_caching(self):
2378
2481
self.assertIs(feature, exception.args[0])
2381
simple_thunk_feature = tests._CompatabilityThunkFeature(
2382
'bzrlib.tests', 'UnicodeFilename',
2383
'bzrlib.tests.test_selftest.simple_thunk_feature',
2384
deprecated_in((2,1,0)))
2386
class Test_CompatibilityFeature(tests.TestCase):
2388
def test_does_thunk(self):
2389
res = self.callDeprecated(
2390
['bzrlib.tests.test_selftest.simple_thunk_feature was deprecated'
2391
' in version 2.1.0. Use bzrlib.tests.UnicodeFilename instead.'],
2392
simple_thunk_feature.available)
2393
self.assertEqual(tests.UnicodeFilename.available(), res)
2396
class TestModuleAvailableFeature(tests.TestCase):
2398
def test_available_module(self):
2399
feature = tests.ModuleAvailableFeature('bzrlib.tests')
2400
self.assertEqual('bzrlib.tests', feature.module_name)
2401
self.assertEqual('bzrlib.tests', str(feature))
2402
self.assertTrue(feature.available())
2403
self.assertIs(tests, feature.module)
2405
def test_unavailable_module(self):
2406
feature = tests.ModuleAvailableFeature('bzrlib.no_such_module_exists')
2407
self.assertEqual('bzrlib.no_such_module_exists', str(feature))
2408
self.assertFalse(feature.available())
2409
self.assertIs(None, feature.module)
2412
2484
class TestSelftestFiltering(tests.TestCase):
2414
2486
def setUp(self):
2593
2665
# the test framework
2594
2666
self.assertEquals('always fails', str(e))
2595
2667
# check that there's no traceback in the test log
2596
self.assertNotContainsRe(self.get_log(), r'Traceback')
2668
self.assertNotContainsRe(self._get_log(keep_log_file=True),
2598
2671
def test_run_bzr_user_error_caught(self):
2599
2672
# Running bzr in blackbox mode, normal/expected/user errors should be
2600
2673
# caught in the regular way and turned into an error message plus exit
2602
2675
transport_server = MemoryServer()
2603
transport_server.start_server()
2604
self.addCleanup(transport_server.stop_server)
2676
transport_server.setUp()
2677
self.addCleanup(transport_server.tearDown)
2605
2678
url = transport_server.get_url()
2606
2679
self.permit_url(url)
2607
2680
out, err = self.run_bzr(["log", "%s/nonexistantpath" % url], retcode=3)