~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_selftest.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2006-05-18 08:53:27 UTC
  • mfrom: (1713.1.4 integration)
  • Revision ID: pqm@pqm.ubuntu.com-20060518085327-89822346d9321aba
Merge benchmark selftests(Robert Collins, Martin Pool), bzr add chattiness(Robert Collins), and bzr push revision count reporting improvements(Robert Collins).

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
"""Tests for the test framework."""
17
17
 
18
18
import os
 
19
from StringIO import StringIO
19
20
import sys
 
21
import time
20
22
import unittest
21
23
import warnings
22
24
 
23
25
import bzrlib
24
26
from bzrlib.progress import _BaseProgressBar
25
27
from bzrlib.tests import (
26
 
                          _load_module_by_name,
27
28
                          ChrootedTestCase,
28
29
                          TestCase,
29
30
                          TestCaseInTempDir,
30
31
                          TestCaseWithTransport,
31
32
                          TestSkipped,
 
33
                          TestSuite,
32
34
                          TextTestRunner,
33
35
                          )
 
36
from bzrlib.tests.TestUtil import _load_module_by_name
34
37
import bzrlib.errors as errors
35
38
 
36
39
 
440
443
        self.assertEqual(last_calls + [('update', 'SKIP         ', 4, None)], mypb.calls)
441
444
        last_calls = mypb.calls[:]
442
445
 
 
446
    def test_elapsed_time_with_benchmarking(self):
 
447
        result = bzrlib.tests._MyResult(self._log_file,
 
448
                                        descriptions=0,
 
449
                                        verbosity=1,
 
450
                                        )
 
451
        result._recordTestStartTime()
 
452
        time.sleep(0.003)
 
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
 
470
 
443
471
 
444
472
class TestRunner(TestCase):
445
473
 
524
552
                                        verbosity=1)
525
553
        outer_test.run(result)
526
554
        self.assertEqual(original_trace, bzrlib.trace._trace_file)
 
555
 
 
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)
 
560
 
 
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),
 
567
            descriptions=0,
 
568
            verbosity=2)
 
569
        sample_test.run(result)
 
570
        self.assertContainsRe(
 
571
            output_stream.getvalue(),
 
572
            "[1-9][0-9]ms/   [1-9][0-9]ms\n$")
527
573
        
528
574
 
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)
 
600
 
 
601
 
 
602
class TestSelftest(TestCase):
 
603
    """Tests of bzrlib.tests.selftest."""
 
604
 
 
605
    def test_selftest_benchmark_parameter_invokes_test_suite__benchmark__(self):
 
606
        factory_called = []
 
607
        def factory():
 
608
            factory_called.append(True)
 
609
            return TestSuite()
 
610
        out = StringIO()
 
611
        err = StringIO()
 
612
        self.apply_redirected(out, err, None, bzrlib.tests.selftest, 
 
613
            test_suite_factory=factory)
 
614
        self.assertEqual([True], factory_called)