78
88
TestUtil._load_module_by_name,
79
89
'bzrlib.no-name-yet')
81
92
class MetaTestLog(tests.TestCase):
83
94
def test_logging(self):
84
95
"""Test logs are captured when a test fails."""
85
96
self.log('a test message')
86
self._log_file.flush()
87
self.assertContainsRe(self._get_log(keep_log_file=True),
97
details = self.getDetails()
99
self.assertThat(log.content_type, Equals(ContentType(
100
"text", "plain", {"charset": "utf8"})))
101
self.assertThat(u"".join(log.iter_text()), Equals(self.get_log()))
102
self.assertThat(self.get_log(),
103
DocTestMatches(u"...a test message\n", ELLIPSIS))
91
106
class TestUnicodeFilename(tests.TestCase):
687
710
def sample_function(self):
689
712
test = Sample("a")
690
test.attrs_to_keep = test.attrs_to_keep + ('_benchcalls',)
692
self.assertLength(1, test._benchcalls)
714
case = terminal._events[0][1]
715
self.assertLength(1, case._benchcalls)
693
716
# We must be able to unpack it as the test reporting code wants
694
(_, _, _), stats = test._benchcalls[0]
717
(_, _, _), stats = case._benchcalls[0]
695
718
self.assertTrue(callable(stats.pprint))
824
849
def stopTestRun(self): pass
825
850
def startTests(self): pass
826
851
def report_test_start(self, test): pass
827
def report_known_failure(self, test, err):
828
self._call = test, err
852
def report_known_failure(self, test, err=None, details=None):
853
self._call = test, 'known failure'
829
854
result = InstrumentedTestResult(None, None, None, None)
831
raise tests.KnownFailure('failed!')
832
test = unittest.FunctionTestCase(test_function)
855
class Test(tests.TestCase):
856
def test_function(self):
857
raise tests.KnownFailure('failed!')
858
test = Test("test_function")
834
860
# it should invoke 'report_known_failure'.
835
861
self.assertEqual(2, len(result._call))
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)
862
self.assertEqual(test.id(), result._call[0].id())
863
self.assertEqual('known failure', result._call[1])
839
864
# we dont introspec the traceback, if the rest is ok, it would be
840
865
# exceptional for it not to be.
841
866
# it should update the known_failure_count on the object.
913
938
result.report_unsupported(test, feature)
914
939
output = result_stream.getvalue()[prefix:]
915
940
lines = output.splitlines()
916
self.assertEqual(lines, ['NODEP 0ms',
917
" The feature 'Feature' is not available."])
941
# We don't check for the final '0ms' since it may fail on slow hosts
942
self.assertStartsWith(lines[0], 'NODEP')
943
self.assertEqual(lines[1],
944
" The feature 'Feature' is not available.")
919
946
def test_unavailable_exception(self):
920
947
"""An UnavailableFeature being raised should invoke addNotSupported."""
926
953
self._call = test, feature
927
954
result = InstrumentedTestResult(None, None, None, None)
928
955
feature = tests.Feature()
930
raise tests.UnavailableFeature(feature)
931
test = unittest.FunctionTestCase(test_function)
956
class Test(tests.TestCase):
957
def test_function(self):
958
raise tests.UnavailableFeature(feature)
959
test = Test("test_function")
933
961
# it should invoke 'addNotSupported'.
934
962
self.assertEqual(2, len(result._call))
935
self.assertEqual(test, result._call[0])
963
self.assertEqual(test.id(), result._call[0].id())
936
964
self.assertEqual(feature, result._call[1])
937
965
# and not count as an error
938
966
self.assertEqual(0, result.error_count)
1014
1042
def test_known_failure_failed_run(self):
1015
1043
# run a test that generates a known failure which should be printed in
1016
1044
# the final output when real failures occur.
1017
def known_failure_test():
1018
raise tests.KnownFailure('failed')
1045
class Test(tests.TestCase):
1046
def known_failure_test(self):
1047
self.expectFailure('failed', self.assertTrue, False)
1019
1048
test = unittest.TestSuite()
1020
test.addTest(unittest.FunctionTestCase(known_failure_test))
1049
test.addTest(Test("known_failure_test"))
1021
1050
def failing_test():
1022
raise AssertionError('foo')
1023
1052
test.addTest(unittest.FunctionTestCase(failing_test))
1024
1053
stream = StringIO()
1025
1054
runner = tests.TextTestRunner(stream=stream)
1026
1055
result = self.run_test_runner(runner, test)
1027
1056
lines = stream.getvalue().splitlines()
1028
1057
self.assertContainsRe(stream.getvalue(),
1058
'(?sm)^bzr selftest.*$'
1031
1060
'^======================================================================\n'
1032
'^FAIL: unittest.FunctionTestCase \\(failing_test\\)\n'
1061
'^FAIL: failing_test\n'
1033
1062
'^----------------------------------------------------------------------\n'
1034
1063
'Traceback \\(most recent call last\\):\n'
1035
1064
' .*' # File .*, line .*, in failing_test' - but maybe not from .pyc
1036
' raise AssertionError\\(\'foo\'\\)\n'
1065
' self.fail\\(\'foo\'\\)\n'
1038
1067
'^----------------------------------------------------------------------\n'
1043
1072
def test_known_failure_ok_run(self):
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)
1073
# run a test that generates a known failure which should be printed in
1075
class Test(tests.TestCase):
1076
def known_failure_test(self):
1077
self.expectFailure('failed', self.assertTrue, False)
1078
test = Test("known_failure_test")
1048
1079
stream = StringIO()
1049
1080
runner = tests.TextTestRunner(stream=stream)
1050
1081
result = self.run_test_runner(runner, test)
1128
1159
def test_not_applicable(self):
1129
1160
# run a test that is skipped because it's not applicable
1130
def not_applicable_test():
1131
raise tests.TestNotApplicable('this test never runs')
1161
class Test(tests.TestCase):
1162
def not_applicable_test(self):
1163
raise tests.TestNotApplicable('this test never runs')
1132
1164
out = StringIO()
1133
1165
runner = tests.TextTestRunner(stream=out, verbosity=2)
1134
test = unittest.FunctionTestCase(not_applicable_test)
1166
test = Test("not_applicable_test")
1135
1167
result = self.run_test_runner(runner, test)
1136
1168
self._log_file.write(out.getvalue())
1137
1169
self.assertTrue(result.wasSuccessful())
1193
1225
self.assertContainsRe(output_string, "--date [0-9.]+")
1194
1226
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)
1309
1228
def test_startTestRun(self):
1310
1229
"""run should call result.startTestRun()"""
2418
2338
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')
2443
2341
class TestFeature(tests.TestCase):
2445
2343
def test_caching(self):
2481
2379
self.assertIs(feature, exception.args[0])
2382
simple_thunk_feature = tests._CompatabilityThunkFeature(
2383
'bzrlib.tests', 'UnicodeFilename',
2384
'bzrlib.tests.test_selftest.simple_thunk_feature',
2385
deprecated_in((2,1,0)))
2387
class Test_CompatibilityFeature(tests.TestCase):
2389
def test_does_thunk(self):
2390
res = self.callDeprecated(
2391
['bzrlib.tests.test_selftest.simple_thunk_feature was deprecated'
2392
' in version 2.1.0. Use bzrlib.tests.UnicodeFilename instead.'],
2393
simple_thunk_feature.available)
2394
self.assertEqual(tests.UnicodeFilename.available(), res)
2397
class TestModuleAvailableFeature(tests.TestCase):
2399
def test_available_module(self):
2400
feature = tests.ModuleAvailableFeature('bzrlib.tests')
2401
self.assertEqual('bzrlib.tests', feature.module_name)
2402
self.assertEqual('bzrlib.tests', str(feature))
2403
self.assertTrue(feature.available())
2404
self.assertIs(tests, feature.module)
2406
def test_unavailable_module(self):
2407
feature = tests.ModuleAvailableFeature('bzrlib.no_such_module_exists')
2408
self.assertEqual('bzrlib.no_such_module_exists', str(feature))
2409
self.assertFalse(feature.available())
2410
self.assertIs(None, feature.module)
2484
2413
class TestSelftestFiltering(tests.TestCase):
2486
2415
def setUp(self):
2665
2594
# the test framework
2666
2595
self.assertEquals('always fails', str(e))
2667
2596
# check that there's no traceback in the test log
2668
self.assertNotContainsRe(self._get_log(keep_log_file=True),
2597
self.assertNotContainsRe(self.get_log(), r'Traceback')
2671
2599
def test_run_bzr_user_error_caught(self):
2672
2600
# Running bzr in blackbox mode, normal/expected/user errors should be
2673
2601
# caught in the regular way and turned into an error message plus exit
2675
2603
transport_server = MemoryServer()
2676
transport_server.setUp()
2677
self.addCleanup(transport_server.tearDown)
2604
transport_server.start_server()
2605
self.addCleanup(transport_server.stop_server)
2678
2606
url = transport_server.get_url()
2679
2607
self.permit_url(url)
2680
2608
out, err = self.run_bzr(["log", "%s/nonexistantpath" % url], retcode=3)