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
830
class Test(tests.TestCase):
855
831
def test_function(self):
859
835
# it should invoke 'report_known_failure'.
860
836
self.assertEqual(2, len(result._call))
861
self.assertEqual(test.id(), result._call[0].id())
862
self.assertEqual('known failure', result._call[1])
837
self.assertEqual(test, result._call[0])
838
self.assertEqual(tests.KnownFailure, result._call[1][0])
839
self.assertIsInstance(result._call[1][1], tests.KnownFailure)
863
840
# we dont introspec the traceback, if the rest is ok, it would be
864
841
# exceptional for it not to be.
865
842
# it should update the known_failure_count on the object.
937
914
result.report_unsupported(test, feature)
938
915
output = result_stream.getvalue()[prefix:]
939
916
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.")
917
self.assertEqual(lines, ['NODEP 0ms',
918
" The feature 'Feature' is not available."])
945
920
def test_unavailable_exception(self):
946
921
"""An UnavailableFeature being raised should invoke addNotSupported."""
1043
1018
# the final output when real failures occur.
1044
1019
class Test(tests.TestCase):
1045
1020
def known_failure_test(self):
1046
self.expectFailure('failed', self.assertTrue, False)
1021
raise tests.KnownFailure('failed')
1047
1022
test = unittest.TestSuite()
1048
1023
test.addTest(Test("known_failure_test"))
1049
1024
def failing_test():
1025
raise AssertionError('foo')
1051
1026
test.addTest(unittest.FunctionTestCase(failing_test))
1052
1027
stream = StringIO()
1053
1028
runner = tests.TextTestRunner(stream=stream)
1054
1029
result = self.run_test_runner(runner, test)
1055
1030
lines = stream.getvalue().splitlines()
1056
1031
self.assertContainsRe(stream.getvalue(),
1057
'(?sm)^bzr selftest.*$'
1059
1034
'^======================================================================\n'
1060
1035
'^FAIL: unittest.FunctionTestCase \\(failing_test\\)\n'
1061
1036
'^----------------------------------------------------------------------\n'
1062
1037
'Traceback \\(most recent call last\\):\n'
1063
1038
' .*' # File .*, line .*, in failing_test' - but maybe not from .pyc
1064
' self.fail\\(\'foo\'\\)\n'
1039
' raise AssertionError\\(\'foo\'\\)\n'
1066
1041
'^----------------------------------------------------------------------\n'
1224
1199
self.assertContainsRe(output_string, "--date [0-9.]+")
1225
1200
self.assertLength(1, self._get_source_tree_calls)
1202
def assertLogDeleted(self, test):
1203
log = test._get_log()
1204
self.assertEqual("DELETED log file to reduce memory footprint", log)
1205
self.assertEqual('', test._log_contents)
1206
self.assertIs(None, test._log_file_name)
1208
def test_success_log_deleted(self):
1209
"""Successful tests have their log deleted"""
1211
class LogTester(tests.TestCase):
1213
def test_success(self):
1214
self.log('this will be removed\n')
1217
runner = tests.TextTestRunner(stream=sio)
1218
test = LogTester('test_success')
1219
result = self.run_test_runner(runner, test)
1221
self.assertLogDeleted(test)
1223
def test_skipped_log_deleted(self):
1224
"""Skipped tests have their log deleted"""
1226
class LogTester(tests.TestCase):
1228
def test_skipped(self):
1229
self.log('this will be removed\n')
1230
raise tests.TestSkipped()
1233
runner = tests.TextTestRunner(stream=sio)
1234
test = LogTester('test_skipped')
1235
result = self.run_test_runner(runner, test)
1237
self.assertLogDeleted(test)
1239
def test_not_aplicable_log_deleted(self):
1240
"""Not applicable tests have their log deleted"""
1242
class LogTester(tests.TestCase):
1244
def test_not_applicable(self):
1245
self.log('this will be removed\n')
1246
raise tests.TestNotApplicable()
1249
runner = tests.TextTestRunner(stream=sio)
1250
test = LogTester('test_not_applicable')
1251
result = self.run_test_runner(runner, test)
1253
self.assertLogDeleted(test)
1255
def test_known_failure_log_deleted(self):
1256
"""Know failure tests have their log deleted"""
1258
class LogTester(tests.TestCase):
1260
def test_known_failure(self):
1261
self.log('this will be removed\n')
1262
raise tests.KnownFailure()
1265
runner = tests.TextTestRunner(stream=sio)
1266
test = LogTester('test_known_failure')
1267
result = self.run_test_runner(runner, test)
1269
self.assertLogDeleted(test)
1271
def test_fail_log_kept(self):
1272
"""Failed tests have their log kept"""
1274
class LogTester(tests.TestCase):
1276
def test_fail(self):
1277
self.log('this will be kept\n')
1278
self.fail('this test fails')
1281
runner = tests.TextTestRunner(stream=sio)
1282
test = LogTester('test_fail')
1283
result = self.run_test_runner(runner, test)
1285
text = sio.getvalue()
1286
self.assertContainsRe(text, 'this will be kept')
1287
self.assertContainsRe(text, 'this test fails')
1289
log = test._get_log()
1290
self.assertContainsRe(log, 'this will be kept')
1291
self.assertEqual(log, test._log_contents)
1293
def test_error_log_kept(self):
1294
"""Tests with errors have their log kept"""
1296
class LogTester(tests.TestCase):
1298
def test_error(self):
1299
self.log('this will be kept\n')
1300
raise ValueError('random exception raised')
1303
runner = tests.TextTestRunner(stream=sio)
1304
test = LogTester('test_error')
1305
result = self.run_test_runner(runner, test)
1307
text = sio.getvalue()
1308
self.assertContainsRe(text, 'this will be kept')
1309
self.assertContainsRe(text, 'random exception raised')
1311
log = test._get_log()
1312
self.assertContainsRe(log, 'this will be kept')
1313
self.assertEqual(log, test._log_contents)
1227
1315
def test_startTestRun(self):
1228
1316
"""run should call result.startTestRun()"""
2337
2424
self.assertEqual('bzr: interrupted\n', result[1])
2427
class TestKnownFailure(tests.TestCase):
2429
def test_known_failure(self):
2430
"""Check that KnownFailure is defined appropriately."""
2431
# a KnownFailure is an assertion error for compatability with unaware
2433
self.assertIsInstance(tests.KnownFailure(""), AssertionError)
2435
def test_expect_failure(self):
2437
self.expectFailure("Doomed to failure", self.assertTrue, False)
2438
except tests.KnownFailure, e:
2439
self.assertEqual('Doomed to failure', e.args[0])
2441
self.expectFailure("Doomed to failure", self.assertTrue, True)
2442
except AssertionError, e:
2443
self.assertEqual('Unexpected success. Should have failed:'
2444
' Doomed to failure', e.args[0])
2446
self.fail('Assertion not raised')
2340
2449
class TestFeature(tests.TestCase):
2342
2451
def test_caching(self):
2378
2487
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
2490
class TestSelftestFiltering(tests.TestCase):
2414
2492
def setUp(self):
2593
2671
# the test framework
2594
2672
self.assertEquals('always fails', str(e))
2595
2673
# check that there's no traceback in the test log
2596
self.assertNotContainsRe(self.get_log(), r'Traceback')
2674
self.assertNotContainsRe(self._get_log(keep_log_file=True),
2598
2677
def test_run_bzr_user_error_caught(self):
2599
2678
# Running bzr in blackbox mode, normal/expected/user errors should be
2600
2679
# caught in the regular way and turned into an error message plus exit
2602
2681
transport_server = MemoryServer()
2603
transport_server.start_server()
2604
self.addCleanup(transport_server.stop_server)
2682
transport_server.setUp()
2683
self.addCleanup(transport_server.tearDown)
2605
2684
url = transport_server.get_url()
2606
2685
self.permit_url(url)
2607
2686
out, err = self.run_bzr(["log", "%s/nonexistantpath" % url], retcode=3)