849
849
self.assertContainsRe(output,
850
850
r"LSProf output for <type 'unicode'>\(\('world',\), {'errors': 'replace'}\)\n")
852
def test_uses_time_from_testtools(self):
853
"""Test case timings in verbose results should use testtools times"""
855
class TimeAddedVerboseTestResult(tests.VerboseTestResult):
856
def startTest(self, test):
857
self.time(datetime.datetime.utcfromtimestamp(1.145))
858
super(TimeAddedVerboseTestResult, self).startTest(test)
859
def addSuccess(self, test):
860
self.time(datetime.datetime.utcfromtimestamp(51.147))
861
super(TimeAddedVerboseTestResult, self).addSuccess(test)
862
def report_tests_starting(self): pass
864
self.get_passing_test().run(TimeAddedVerboseTestResult(sio, 0, 2))
865
self.assertEndsWith(sio.getvalue(), "OK 50002ms\n")
852
867
def test_known_failure(self):
853
868
"""A KnownFailure being raised should trigger several result actions."""
854
869
class InstrumentedTestResult(tests.ExtendedTestResult):
3301
3316
self.assertContainsString(result.stream.getvalue(), "leaking threads")
3319
class TestPostMortemDebugging(tests.TestCase):
3320
"""Check post mortem debugging works when tests fail or error"""
3322
class TracebackRecordingResult(tests.ExtendedTestResult):
3324
tests.ExtendedTestResult.__init__(self, StringIO(), 0, 1)
3325
self.postcode = None
3326
def _post_mortem(self, tb=None):
3327
"""Record the code object at the end of the current traceback"""
3328
tb = tb or sys.exc_info()[2]
3331
while next is not None:
3334
self.postcode = tb.tb_frame.f_code
3335
def report_error(self, test, err):
3337
def report_failure(self, test, err):
3340
def test_location_unittest_error(self):
3341
"""Needs right post mortem traceback with erroring unittest case"""
3342
class Test(unittest.TestCase):
3345
result = self.TracebackRecordingResult()
3347
self.assertEqual(result.postcode, Test.runTest.func_code)
3349
def test_location_unittest_failure(self):
3350
"""Needs right post mortem traceback with failing unittest case"""
3351
class Test(unittest.TestCase):
3353
raise self.failureException
3354
result = self.TracebackRecordingResult()
3356
self.assertEqual(result.postcode, Test.runTest.func_code)
3358
def test_location_bt_error(self):
3359
"""Needs right post mortem traceback with erroring bzrlib.tests case"""
3360
class Test(tests.TestCase):
3361
def test_error(self):
3363
result = self.TracebackRecordingResult()
3364
Test("test_error").run(result)
3365
self.assertEqual(result.postcode, Test.test_error.func_code)
3367
def test_location_bt_failure(self):
3368
"""Needs right post mortem traceback with failing bzrlib.tests case"""
3369
class Test(tests.TestCase):
3370
def test_failure(self):
3371
raise self.failureException
3372
result = self.TracebackRecordingResult()
3373
Test("test_failure").run(result)
3374
self.assertEqual(result.postcode, Test.test_failure.func_code)
3376
def test_env_var_triggers_post_mortem(self):
3377
"""Check pdb.post_mortem is called iff BZR_TEST_PDB is set"""
3379
result = tests.ExtendedTestResult(StringIO(), 0, 1)
3380
post_mortem_calls = []
3381
self.overrideAttr(pdb, "post_mortem", post_mortem_calls.append)
3382
self.addCleanup(osutils.set_or_unset_env, "BZR_TEST_PDB",
3383
osutils.set_or_unset_env("BZR_TEST_PDB", None))
3384
result._post_mortem(1)
3385
os.environ["BZR_TEST_PDB"] = "on"
3386
result._post_mortem(2)
3387
self.assertEqual([2], post_mortem_calls)
3304
3390
class TestRunSuite(tests.TestCase):
3306
3392
def test_runner_class(self):