Commits

Anonymous committed 6c40ead

Added randomizer and tag exclude support to test framework. Improved detail output.

Comments (0)

Files changed (4)

test/sdl_video_test.py

         try:
             video.set_gamma (1, 1, 1)
         except pygame2.Error:
+            video.quit ()
             return
         self.assert_ (video.set_gamma (0, 0, 0) == None)
         self.assert_ (video.set_gamma (1, 1, 1) == None)

test/util/runtests.py

 import os, sys, traceback
 import unittest
 import optparse
+import random
 
 try:
     from pygame2.test.util import support, testrunner
 def printerror ():
     print (traceback.format_exc ())
 
+def include_tag (option, opt, value, parser, *args, **kwargs):
+    try:
+        if args:
+            EXCLUDETAGS.remove (args[0])
+        else:
+            EXCLUDETAGS.remove (value)
+    finally:
+        pass
+
+def exclude_tag (option, opt, value, parser, *args, **kwargs):
+    if value not in EXCLUDETAGS:
+        EXCLUDETAGS.append (value)
+
 def create_options ():
     """Create the accepatble options for the test runner."""
     optparser = optparse.OptionParser ()
-    optparser.add_option ("-n", "--nosubprocess", action="store_true",
+    optparser.add_option ("-s", "--subprocess", action="store_true",
                           default=False,
-                          help="run everything in a single process "
-                          "(default: use seperate subprocesses)")
+                          help="run everything in an own subprocess "
+                          "(default: use a single process)")
     optparser.add_option ("-v", "--verbose", action="store_true", default=False,
                           help="be verbose adnd print anything instantly")
     optparser.add_option ("-r", "--random", action="store_true", default=False,
                           help="randomize the order of tests")
     optparser.add_option ("-S", "--seed", type="int",
                           help="seed the randomizer (useful to "
-                          "recreate earlier test cases)")
+                          "recreate earlier randomized test cases)")
     optparser.add_option ("-i", "--interactive", action="callback",
-                          callback=EXCLUDETAGS.remove,
+                          callback=include_tag,
                           callback_args=("interactive",),
-                          help="also execute interactive tests ")
-    return optparser
+                          help="also execute interactive tests")
+    optparser.add_option ("-e", "--exclude", action="callback",
+                          callback=exclude_tag, type="string",
+                          help="exclude test containing the tag")
+    optkeys = [
+        "subprocess",
+        "random",
+        "seed",
+        "verbose"
+        ]
 
-def gettestfiles (testdir=None):
+    return optparser, optkeys
+
+def gettestfiles (testdir=None, randomizer=None):
     """
     Get all test files from the passed test directory. If none is
     passed, use the default pygame2 test directory.
     for name in names:
         if name.endswith ("_test" + os.extsep + "py"):
             testfiles.append (name)
-    testfiles.sort ()
+    if randomizer:
+        randomizer.shuffle (testfiles)
+    else:
+        testfiles.sort ()
     return testdir, testfiles
 
-def loadtests (test, testdir, writer, options):
+def loadtests (test, testdir, writer, loader, options):
     """Loads a test."""
     suites = []
-    testloader = testrunner.TagTestLoader (EXCLUDETAGS)
 
     try:
         testmod = os.path.splitext (test)[0]
             if hasattr (val, "setUp") and hasattr (val, "tearDown"):
                 # might be a test.
                 try:
-                    tests = testloader.loadTestsFromTestCase (val)
+                    tests = loader.loadTestsFromTestCase (val)
                     suites.append (tests)
                     # TODO: provide a meaningful error information about
                     # the failure.
     return testcount, errors, failures, ok
 
 def run ():
-    optparser = create_options ()
+    optparser, optkeys = create_options ()
     options, args = optparser.parse_args ()
     #err, out = support.redirect_output ()
     writer = support.StreamOutput (sys.stdout)
         writer.writeline ("-- Starting tests --")
         writer.writeline (HEAVYDELIM)
 
-    testdir, testfiles = gettestfiles ()
+    loader = None
+    randomizer = None
+    if options.random:
+        if options.seed is None:
+            options.seed = random.randint (0, sys.maxint)
+        randomizer = random.Random (options.seed)
+    loader = testrunner.TagTestLoader (EXCLUDETAGS, randomizer)
+
+    testdir, testfiles = gettestfiles (randomizer=randomizer)
     testsuites = []
     for test in testfiles:
-        testsuites.extend (loadtests (test, testdir, writer, options))
+        testsuites.extend (loadtests (test, testdir, writer, loader, options))
     if not options.verbose:
         writer.writesame ("Tests loaded")
     runner = testrunner.SimpleTestRunner (sys.stderr, options.verbose)
     writer.writeline (HEAVYDELIM)
     writer.writeline ("-- Statistics --")
     writer.writeline (HEAVYDELIM)
+    writer.writeline ("Options:")
+    for key in optkeys:
+        writer.writeline ("                '%s' = '%s'" %
+                          (key, getattr (options, key)))
     writer.writeline ("Time taken:     %.3f seconds" % timetaken)
     writer.writeline ("Tests executed: %d " % testcount)
     writer.writeline ("Tests OK:       %d " % ok)
         writer.writeline ("Errors:" + os.linesep)
         for err in errors:
             writer.writeline (LINEDELIM)
-            writer.writeline ("ERROR: " + err[0])
+            writer.writeline ("ERROR: %s" % err[0])
             writer.writeline (HEAVYDELIM)
             writer.writeline (err[1])
     if len (failures) > 0:
         writer.writeline ("Failures:" + os.linesep)
         for fail in failures:
             writer.writeline (LINEDELIM)
-            writer.writeline ("FAILURE: " + fail[0])
+            writer.writeline ("FAILURE: %s" % fail[0])
             writer.writeline (HEAVYDELIM)
             writer.writeline (fail[1])
         

test/util/support.py

     def writesame (self, data):
         overhang = self.curoffset - len (data)
         if overhang > 0:
-            self.stream.write (data + " " * overhang + "\r")
+            self.stream.write ("%s %s\r" % (data, " " * overhang))
         else:
-            self.stream.write (data + "\r")
+            self.stream.write ("%s\r" % data)
         self.curoffset = len (data)
         self.stream.flush ()

test/util/testrunner.py

     """A TestLoader which handles additional __tags__ attributes for
     test functions.
     """
-    def __init__ (self, excludetags):
+    def __init__ (self, excludetags, randomizer=None):
         TestLoader.__init__ (self)
         self.excludetags = excludetags
+        self.randomizer = randomizer
 
     def getTestCaseNames(self, testCaseClass):
         """
         elif hasattr (unittest, "CmpToKey"):
             cmpkey = unittest.CmpToKey
 
-        if self.sortTestMethodsUsing:
+        if self.randomizer:
+            self.randomizer.shuffle (testFnNames)
+        elif self.sortTestMethodsUsing:
             if cmpkey:
                 testFnNames.sort (key=cmpkey(self.sortTestMethodsUsing))
             else:
     def addSuccess (self, test):
         TestResult.addSuccess (self, test)
         if self.verbose:
-            self.stream.write (".")
+            self.stream.write ("OK:     %s%s" % (test, os.linesep))
             self.stream.flush ()
         self.countcall ()
 
     def addError (self, test, err):
         TestResult.addError (self, test, err)
         if self.verbose:
-            self.stream.write ("E")
+            self.stream.write ("ERROR:  %s%s" % (test, os.linesep))
             self.stream.flush ()
         self.countcall ()
 
     def addFailure (self, test, err):
         TestResult.addFailure (self, test, err)
         if self.verbose:
-            self.stream.write ("F")
+            self.stream.write ("FAILED: %s%s" % (test, os.linesep))
             self.stream.flush ()
         self.countcall ()
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.