78
87
TestUtil._load_module_by_name,
79
88
'bzrlib.no-name-yet')
81
91
class MetaTestLog(tests.TestCase):
83
93
def test_logging(self):
84
94
"""Test logs are captured when a test fails."""
85
95
self.log('a test message')
86
self._log_file.flush()
87
self.assertContainsRe(self._get_log(keep_log_file=True),
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))
91
105
class TestUnicodeFilename(tests.TestCase):
695
709
def sample_function(self):
697
711
test = Sample("a")
698
test.attrs_to_keep = test.attrs_to_keep + ('_benchcalls',)
700
self.assertLength(1, test._benchcalls)
713
case = terminal._events[0][1]
714
self.assertLength(1, case._benchcalls)
701
715
# We must be able to unpack it as the test reporting code wants
702
(_, _, _), stats = test._benchcalls[0]
716
(_, _, _), stats = case._benchcalls[0]
703
717
self.assertTrue(callable(stats.pprint))
832
848
def stopTestRun(self): pass
833
849
def startTests(self): pass
834
850
def report_test_start(self, test): pass
835
def report_known_failure(self, test, err):
836
self._call = test, err
851
def report_known_failure(self, test, err=None, details=None):
852
self._call = test, 'known failure'
837
853
result = InstrumentedTestResult(None, None, None, None)
838
854
class Test(tests.TestCase):
839
855
def test_function(self):
843
859
# it should invoke 'report_known_failure'.
844
860
self.assertEqual(2, len(result._call))
845
self.assertEqual(test, result._call[0])
846
self.assertEqual(tests.KnownFailure, result._call[1][0])
847
self.assertIsInstance(result._call[1][1], tests.KnownFailure)
861
self.assertEqual(test.id(), result._call[0].id())
862
self.assertEqual('known failure', result._call[1])
848
863
# we dont introspec the traceback, if the rest is ok, it would be
849
864
# exceptional for it not to be.
850
865
# it should update the known_failure_count on the object.
1028
1043
# the final output when real failures occur.
1029
1044
class Test(tests.TestCase):
1030
1045
def known_failure_test(self):
1031
raise tests.KnownFailure('failed')
1046
self.expectFailure('failed', self.assertTrue, False)
1032
1047
test = unittest.TestSuite()
1033
1048
test.addTest(Test("known_failure_test"))
1034
1049
def failing_test():
1035
raise AssertionError('foo')
1036
1051
test.addTest(unittest.FunctionTestCase(failing_test))
1037
1052
stream = StringIO()
1038
1053
runner = tests.TextTestRunner(stream=stream)
1039
1054
result = self.run_test_runner(runner, test)
1040
1055
lines = stream.getvalue().splitlines()
1041
1056
self.assertContainsRe(stream.getvalue(),
1057
'(?sm)^bzr selftest.*$'
1044
1059
'^======================================================================\n'
1045
1060
'^FAIL: unittest.FunctionTestCase \\(failing_test\\)\n'
1046
1061
'^----------------------------------------------------------------------\n'
1047
1062
'Traceback \\(most recent call last\\):\n'
1048
1063
' .*' # File .*, line .*, in failing_test' - but maybe not from .pyc
1049
' raise AssertionError\\(\'foo\'\\)\n'
1064
' self.fail\\(\'foo\'\\)\n'
1051
1066
'^----------------------------------------------------------------------\n'
1209
1224
self.assertContainsRe(output_string, "--date [0-9.]+")
1210
1225
self.assertLength(1, self._get_source_tree_calls)
1212
def assertLogDeleted(self, test):
1213
log = test._get_log()
1214
self.assertEqual("DELETED log file to reduce memory footprint", log)
1215
self.assertEqual('', test._log_contents)
1216
self.assertIs(None, test._log_file_name)
1218
def test_success_log_deleted(self):
1219
"""Successful tests have their log deleted"""
1221
class LogTester(tests.TestCase):
1223
def test_success(self):
1224
self.log('this will be removed\n')
1227
runner = tests.TextTestRunner(stream=sio)
1228
test = LogTester('test_success')
1229
result = self.run_test_runner(runner, test)
1231
self.assertLogDeleted(test)
1233
def test_skipped_log_deleted(self):
1234
"""Skipped tests have their log deleted"""
1236
class LogTester(tests.TestCase):
1238
def test_skipped(self):
1239
self.log('this will be removed\n')
1240
raise tests.TestSkipped()
1243
runner = tests.TextTestRunner(stream=sio)
1244
test = LogTester('test_skipped')
1245
result = self.run_test_runner(runner, test)
1247
self.assertLogDeleted(test)
1249
def test_not_aplicable_log_deleted(self):
1250
"""Not applicable tests have their log deleted"""
1252
class LogTester(tests.TestCase):
1254
def test_not_applicable(self):
1255
self.log('this will be removed\n')
1256
raise tests.TestNotApplicable()
1259
runner = tests.TextTestRunner(stream=sio)
1260
test = LogTester('test_not_applicable')
1261
result = self.run_test_runner(runner, test)
1263
self.assertLogDeleted(test)
1265
def test_known_failure_log_deleted(self):
1266
"""Know failure tests have their log deleted"""
1268
class LogTester(tests.TestCase):
1270
def test_known_failure(self):
1271
self.log('this will be removed\n')
1272
raise tests.KnownFailure()
1275
runner = tests.TextTestRunner(stream=sio)
1276
test = LogTester('test_known_failure')
1277
result = self.run_test_runner(runner, test)
1279
self.assertLogDeleted(test)
1281
def test_fail_log_kept(self):
1282
"""Failed tests have their log kept"""
1284
class LogTester(tests.TestCase):
1286
def test_fail(self):
1287
self.log('this will be kept\n')
1288
self.fail('this test fails')
1291
runner = tests.TextTestRunner(stream=sio)
1292
test = LogTester('test_fail')
1293
result = self.run_test_runner(runner, test)
1295
text = sio.getvalue()
1296
self.assertContainsRe(text, 'this will be kept')
1297
self.assertContainsRe(text, 'this test fails')
1299
log = test._get_log()
1300
self.assertContainsRe(log, 'this will be kept')
1301
self.assertEqual(log, test._log_contents)
1303
def test_error_log_kept(self):
1304
"""Tests with errors have their log kept"""
1306
class LogTester(tests.TestCase):
1308
def test_error(self):
1309
self.log('this will be kept\n')
1310
raise ValueError('random exception raised')
1313
runner = tests.TextTestRunner(stream=sio)
1314
test = LogTester('test_error')
1315
result = self.run_test_runner(runner, test)
1317
text = sio.getvalue()
1318
self.assertContainsRe(text, 'this will be kept')
1319
self.assertContainsRe(text, 'random exception raised')
1321
log = test._get_log()
1322
self.assertContainsRe(log, 'this will be kept')
1323
self.assertEqual(log, test._log_contents)
1325
1227
def test_startTestRun(self):
1326
1228
"""run should call result.startTestRun()"""
2431
2337
self.assertEqual('bzr: interrupted\n', result[1])
2434
class TestKnownFailure(tests.TestCase):
2436
def test_known_failure(self):
2437
"""Check that KnownFailure is defined appropriately."""
2438
# a KnownFailure is an assertion error for compatability with unaware
2440
self.assertIsInstance(tests.KnownFailure(""), AssertionError)
2442
def test_expect_failure(self):
2444
self.expectFailure("Doomed to failure", self.assertTrue, False)
2445
except tests.KnownFailure, e:
2446
self.assertEqual('Doomed to failure', e.args[0])
2448
self.expectFailure("Doomed to failure", self.assertTrue, True)
2449
except AssertionError, e:
2450
self.assertEqual('Unexpected success. Should have failed:'
2451
' Doomed to failure', e.args[0])
2453
self.fail('Assertion not raised')
2456
2340
class TestFeature(tests.TestCase):
2458
2342
def test_caching(self):