16
16
"""Tests for the test framework."""
19
from StringIO import StringIO
24
26
from bzrlib.progress import _BaseProgressBar
25
27
from bzrlib.tests import (
30
31
TestCaseWithTransport,
36
from bzrlib.tests.TestUtil import _load_module_by_name
34
37
import bzrlib.errors as errors
440
443
self.assertEqual(last_calls + [('update', 'SKIP ', 4, None)], mypb.calls)
441
444
last_calls = mypb.calls[:]
446
def test_elapsed_time_with_benchmarking(self):
447
result = bzrlib.tests._MyResult(self._log_file,
451
result._recordTestStartTime()
453
result.extractBenchmarkTime(self)
454
timed_string = result._testTimeString()
455
# without explicit benchmarking, we should get a simple time.
456
self.assertContainsRe(timed_string, "^ [ 1-9][0-9]ms$")
457
# if a benchmark time is given, we want a x of y style result.
458
self.time(time.sleep, 0.001)
459
result.extractBenchmarkTime(self)
460
timed_string = result._testTimeString()
461
self.assertContainsRe(timed_string, "^ [0-9]ms/ [ 1-9][0-9]ms$")
462
# extracting the time from a non-bzrlib testcase sets to None
463
result._recordTestStartTime()
464
result.extractBenchmarkTime(
465
unittest.FunctionTestCase(self.test_elapsed_time_with_benchmarking))
466
timed_string = result._testTimeString()
467
self.assertContainsRe(timed_string, "^ [0-9]ms$")
468
# cheat. Yes, wash thy mouth out with soap.
469
self._benchtime = None
444
472
class TestRunner(TestCase):
525
553
outer_test.run(result)
526
554
self.assertEqual(original_trace, bzrlib.trace._trace_file)
556
def method_that_times_a_bit_twice(self):
557
# call self.time twice to ensure it aggregates
558
self.time(time.sleep, 0.007)
559
self.time(time.sleep, 0.007)
561
def test_time_creates_benchmark_in_result(self):
562
"""Test that the TestCase.time() method accumulates a benchmark time."""
563
sample_test = TestTestCase("method_that_times_a_bit_twice")
564
output_stream = StringIO()
565
result = bzrlib.tests._MyResult(
566
unittest._WritelnDecorator(output_stream),
569
sample_test.run(result)
570
self.assertContainsRe(
571
output_stream.getvalue(),
572
"[1-9][0-9]ms/ [1-9][0-9]ms\n$")
529
575
class TestExtraAssertions(TestCase):
551
597
bzrlib.bzrdir.BzrDirMetaFormat1)
552
598
self.assertIsInstance(bzrlib.bzrdir.BzrDir.open('b')._format,
553
599
bzrlib.bzrdir.BzrDirFormat6)
602
class TestSelftest(TestCase):
603
"""Tests of bzrlib.tests.selftest."""
605
def test_selftest_benchmark_parameter_invokes_test_suite__benchmark__(self):
608
factory_called.append(True)
612
self.apply_redirected(out, err, None, bzrlib.tests.selftest,
613
test_suite_factory=factory)
614
self.assertEqual([True], factory_called)