[Zope3-checkins] SVN: zope.testing/trunk/src/zope/testing/testrunner Highlight test timings, using a different color for very slow tests.

Marius Gedminas marius at pov.lt
Sat Jul 21 05:22:18 EDT 2007


Log message for revision 78256:
  Highlight test timings, using a different color for very slow tests.
  
  

Changed:
  U   zope.testing/trunk/src/zope/testing/testrunner-colors.txt
  U   zope.testing/trunk/src/zope/testing/testrunner.py

-=-
Modified: zope.testing/trunk/src/zope/testing/testrunner-colors.txt
===================================================================
--- zope.testing/trunk/src/zope/testing/testrunner-colors.txt	2007-07-21 09:10:48 UTC (rev 78255)
+++ zope.testing/trunk/src/zope/testing/testrunner-colors.txt	2007-07-21 09:22:18 UTC (rev 78256)
@@ -236,6 +236,44 @@
     {normal}  Ran {green}1{normal} tests with {boldred}1{normal} failures and {green}0{normal} errors in {green}0.003{normal} seconds.{normal}
 
 
+Timing individual tests
+-----------------------
+
+At very high verbosity levels you can see the time taken by each test
+
+    >>> sys.argv = 'test -u -t test_one.TestNotMuch -c -vvv'.split()
+    >>> testrunner.run(defaults)
+    {normal}Running tests at level 1{normal}
+    {normal}Running unit tests:{normal}
+    {normal}  Running:{normal}
+     test_1 (sample1.sampletests.test_one.TestNotMuch) ({green}N.NNN s{normal})
+     test_2 (sample1.sampletests.test_one.TestNotMuch) ({green}N.NNN s{normal})
+     test_3 (sample1.sampletests.test_one.TestNotMuch) ({green}N.NNN s{normal})
+     test_1 (sampletests.test_one.TestNotMuch) ({green}N.NNN s{normal})
+     test_2 (sampletests.test_one.TestNotMuch) ({green}N.NNN s{normal})
+     test_3 (sampletests.test_one.TestNotMuch) ({green}N.NNN s{normal})
+    {normal}  Ran {green}6{normal} tests with {green}0{normal} failures and {green}0{normal} errors in {green}N.NNN{normal} seconds.{normal}
+    False
+
+If we had very slow tests we would see their times highlighted in a different color.
+Instead of creating a test that waits 10 seconds, let's lower the slow test threshold
+in the test runner to 0 seconds to make all of the tests seem slow.
+
+    >>> sys.argv = 'test -u -t test_one.TestNotMuch -c -vvv --slow-test 0'.split()
+    >>> testrunner.run(defaults)
+    {normal}Running tests at level 1{normal}
+    {normal}Running unit tests:{normal}
+    {normal}  Running:{normal}
+     test_1 (sample1.sampletests.test_one.TestNotMuch) ({boldmagenta}N.NNN s{normal})
+     test_2 (sample1.sampletests.test_one.TestNotMuch) ({boldmagenta}N.NNN s{normal})
+     test_3 (sample1.sampletests.test_one.TestNotMuch) ({boldmagenta}N.NNN s{normal})
+     test_1 (sampletests.test_one.TestNotMuch) ({boldmagenta}N.NNN s{normal})
+     test_2 (sampletests.test_one.TestNotMuch) ({boldmagenta}N.NNN s{normal})
+     test_3 (sampletests.test_one.TestNotMuch) ({boldmagenta}N.NNN s{normal})
+    {normal}  Ran {green}6{normal} tests with {green}0{normal} failures and {green}0{normal} errors in {green}N.NNN{normal} seconds.{normal}
+    False
+
+
 Disabling colors
 ----------------
 

Modified: zope.testing/trunk/src/zope/testing/testrunner.py
===================================================================
--- zope.testing/trunk/src/zope/testing/testrunner.py	2007-07-21 09:10:48 UTC (rev 78255)
+++ zope.testing/trunk/src/zope/testing/testrunner.py	2007-07-21 09:22:18 UTC (rev 78256)
@@ -601,6 +601,7 @@
                    'suboptimal-behaviour': 'magenta',
                    'error': 'brightred',
                    'number': 'green',
+                   'slow-test': 'brightmagenta',
                    'ok-number': 'green',
                    'error-number': 'brightred',
                    'filename': 'lightblue',
@@ -642,6 +643,8 @@
                   'cyan': 36,
                   'grey': 37, 'gray': 37, 'white': 37}
 
+    slow_test_threshold = 10.0 # seconds
+
     def color_code(self, color):
         """Convert a color description (e.g. 'lightgray') to a terminal code."""
         prefix_code = ''
@@ -710,6 +713,14 @@
             return "%s seconds" % (
                         self.colorize('number', '%.3f' % n_seconds, normal))
 
+    def format_seconds_short(self, n_seconds):
+        """Format a time in seconds (short version)."""
+        if n_seconds >= self.slow_test_threshold:
+            color = 'slow-test'
+        else:
+            color = 'number'
+        return self.colorize(color, "%.3f s" % n_seconds)
+
     def summary(self, n_tests, n_failures, n_errors, n_seconds):
         """Summarize the results."""
         sys.stdout.writelines([
@@ -2046,6 +2057,14 @@
 """)
 
 reporting.add_option(
+    '--slow-test', type='float', dest='slow_test_threshold',
+    metavar='N', default=10,
+    help="""\
+With -c and -vvv, highlight tests that take longer than N seconds (default:
+%default).
+""")
+
+reporting.add_option(
     '-1', '--hide-secondary-failures',
     action="store_true", dest='report_only_first_failure',
     help="""\
@@ -2352,6 +2371,7 @@
 
     if options.color:
         options.output = ColorfulOutputFormatter(options)
+        options.output.slow_test_threshold = options.slow_test_threshold
     else:
         options.output = OutputFormatter(options)
 



More information about the Zope3-Checkins mailing list