Commits

Gary Oberbrunner committed 14331be Merge

Merged in techtonik/scons (pull request #124)

New configurable test runner for unittests

Comments (0)

Files changed (48)

      --passed                 Summarize which tests passed.
   -q --quiet                  Don't print the test being executed.
      --quit-on-failure        Quit on any test failure
+     --runner CLASS           Alternative test runner class for unit tests
   -s --short-progress         Short progress, prints only the command line
                               and a percentage value, based on the total and
                               current number of tests.
                       help="Run all tests.")
 parser.add_option('-o', '--output',
                       help="Save the output from a test run to the log file.")
+parser.add_option('--runner', metavar='class',
+                      help="Test runner class for unit tests.")
 parser.add_option('--xml',
                       help="Save results to file in SCons XML format.")
 (options, args) = parser.parse_args()
     elif o in ['-x', '--exec']:
         scons = a
 
-if not args and not options.all and not testlistfile:
-    sys.stderr.write(usagestr + """
-runtest.py:  No tests were specified.
-             Tests can be specified on the command line, read from file
-             with -f option, or discovered with -a to run all tests.
-""")
-    sys.exit(1)
 
 
 # --- setup stdout/stderr ---
 
 # FIXME: the following is necessary to pull in half of the testing
 #        harness from $srcdir/etc. Those modules should be transfered
-#        to QMTest/ once we completely cut over to using that as
-#        the harness, in which case this manipulation of PYTHONPATH
+#        to testing/, in which case this manipulation of PYTHONPATH
 #        should be able to go away.
 pythonpaths = [ pythonpath_dir ]
 
 # Add path of the QMTest folder to PYTHONPATH
+# [ ] move used parts from QMTest to testing/framework/
 scriptpath = os.path.dirname(os.path.realpath(__file__))
 pythonpaths.append(os.path.join(scriptpath, 'QMTest'))
+# Add path for testing framework to PYTHONPATH
+pythonpaths.append(os.path.join(scriptpath, 'testing', 'framework'))
+
 
 os.environ['PYTHONPATH'] = os.pathsep.join(pythonpaths)
 
 
 tests = []
 
+unittests = []
+endtests = []
+
 def find_Tests_py(directory):
     """ Look for unit tests """
     result = []
                 result.append(os.path.join(dirpath, fname))
     return sorted(result)
 
-if args:
-    for a in args:
-        for path in glob.glob(a):
-            if os.path.isdir(path):
-                if path[:3] == 'src':
-                    for p in find_Tests_py(path):
-                        tests.append(p)
-                elif path[:4] == 'test':
-                    for p in find_py(path):
-                        tests.append(p)
-            else:
-                tests.append(path)
 
-elif testlistfile:
+if testlistfile:
     tests = open(testlistfile, 'r').readlines()
     tests = [x for x in tests if x[0] != '#']
     tests = [x[:-1] for x in tests]
     tests = [x.strip() for x in tests]
 
-elif options.all:
-    # Find all of the SCons functional tests in the local directory
-    # tree.  This is anything under the 'src' subdirectory that ends
-    # with 'Tests.py', or any Python script (*.py) under the 'test'
-    # subdirectory.
+else:
+    testpaths = []
+
+    # Each test path specifies a test file, or a directory to search for
+    # SCons tests. SCons code layout assumes that any file under the 'src'
+    # subdirectory that ends with 'Tests.py' is a unit test, and Python
+    # script (*.py) under the 'test' subdirectory an end-to-end test.
     #
     # Note that there are some tests under 'src' that *begin* with
     # 'test_', but they're packaging and installation tests, not
     # still be executed by hand, though, and are routinely executed
     # by the Aegis packaging build to make sure that we're building
     # things correctly.)
-    tests.extend(find_Tests_py('src'))
-    tests.extend(find_py('test'))
+
+    if options.all:
+        testpaths = ['src', 'test']
+    elif args:
+        testpaths = args
+
+    for tp in testpaths:
+        for path in glob.glob(tp):
+            if os.path.isdir(path):
+                if path.startswith('src'):
+                    for p in find_Tests_py(path):
+                        unittests.append(p)
+                elif path.startswith('test'):
+                    for p in find_py(path):
+                        endtests.append(p)
+            else:
+                if path.endswith("Tests.py"):
+                    unittests.append(path)
+                else:
+                    endtests.append(path)
+
+    tests.extend(unittests)
+    tests.extend(endtests)
     tests.sort()
 
 if not tests:
-    sys.stderr.write("""\
+    sys.stderr.write(usagestr + """
 runtest.py:  No tests were found.
+             Tests can be specified on the command line, read from file
+             with -f option, or discovered with -a to run all tests.
 """)
     sys.exit(1)
 
         sys.stdout.write(t.path + "\n")
     sys.exit(0)
 
-#
 if not python:
     if os.name == 'java':
         python = os.path.join(sys.prefix, 'jython')
     if debug:
         command_args.append(debug)
     command_args.append(t.path)
+    if options.runner and t.path in unittests:
+        # For example --runner TestUnit.TAPTestRunner
+        command_args.append('--runner ' + options.runner)
     t.command_args = [python] + command_args
     t.command_str = " ".join([escape(python)] + command_args)
     if printcommand:

src/engine/SCons/ActionTests.py

 import SCons.Errors
 
 import TestCmd
+import TestUnit
 
 # Initial setup of the common environment for all tests,
 # a temporary working directory containing a
     for tclass in tclasses:
         names = unittest.getTestCaseNames(tclass, 'test_')
         suite.addTests(list(map(tclass, names)))
-    if not unittest.TextTestRunner().run(suite).wasSuccessful():
-        sys.exit(1)
+
+    TestUnit.run(suite)
 
 # Local Variables:
 # tab-width:4

src/engine/SCons/BuilderTests.py

 import unittest
 
 import TestCmd
+import TestUnit
 
 import SCons.Action
 import SCons.Builder
     for tclass in tclasses:
         names = unittest.getTestCaseNames(tclass, 'test_')
         suite.addTests(list(map(tclass, names)))
-    if not unittest.TextTestRunner().run(suite).wasSuccessful():
-        sys.exit(1)
+
+    TestUnit.run(suite)
 
 # Local Variables:
 # tab-width:4

src/engine/SCons/CacheDirTests.py

 import unittest
 
 from TestCmd import TestCmd
+import TestUnit
 
 import SCons.CacheDir
 
     for tclass in tclasses:
         names = unittest.getTestCaseNames(tclass, 'test_')
         suite.addTests(list(map(tclass, names)))
-    if not unittest.TextTestRunner().run(suite).wasSuccessful():
-        sys.exit(1)
+    TestUnit.run(suite)
 
 # Local Variables:
 # tab-width:4

src/engine/SCons/DefaultsTests.py

 from collections import UserDict
 
 import TestCmd
+import TestUnit
 
 import SCons.Errors
 
     for tclass in tclasses:
         names = unittest.getTestCaseNames(tclass, 'test_')
         suite.addTests(list(map(tclass, names)))
-    if not unittest.TextTestRunner().run(suite).wasSuccessful():
-        sys.exit(1)
+    TestUnit.run(suite)
 
 # Local Variables:
 # tab-width:4

src/engine/SCons/EnvironmentTests.py

 import io
 import os
 import sys
-import TestCmd
 import unittest
 from collections import UserDict as UD, UserList as UL
 
+import TestCmd
+import TestUnit
+
 from SCons.Environment import *
 import SCons.Warnings
 
     for tclass in tclasses:
         names = unittest.getTestCaseNames(tclass, 'test_')
         suite.addTests(list(map(tclass, names)))
-    if not unittest.TextTestRunner().run(suite).wasSuccessful():
-        sys.exit(1)
+    TestUnit.run(suite)
 
 # Local Variables:
 # tab-width:4

src/engine/SCons/ErrorsTests.py

 
 import sys
 import unittest
+
+import TestUnit
+
 import SCons.Errors
 
 
 
 if __name__ == "__main__":
     suite = unittest.makeSuite(ErrorsTestCase, 'test_')
-    if not unittest.TextTestRunner().run(suite).wasSuccessful():
-        sys.exit(1)
+    TestUnit.run(suite)
 
 # Local Variables:
 # tab-width:4

src/engine/SCons/ExecutorTests.py

 import sys
 import unittest
 
+import TestUnit
+
 import SCons.Executor
 
 
     for tclass in tclasses:
         names = unittest.getTestCaseNames(tclass, 'test_')
         suite.addTests(list(map(tclass, names)))
-    if not unittest.TextTestRunner().run(suite).wasSuccessful():
-        sys.exit(1)
+    TestUnit.run(suite)
 
 # Local Variables:
 # tab-width:4

src/engine/SCons/JobTests.py

 import unittest
 import random
 import math
-import SCons.Job
 import sys
 import time
 
+import TestUnit
+
+import SCons.Job
+
+
 # a large number
 num_sines = 10000
 
     return suite
 
 if __name__ == "__main__":
-    runner = unittest.TextTestRunner()
-    result = runner.run(suite())
+    runner = TestUnit.cli.get_runner()
+    result = runner().run(suite())
     if (len(result.failures) == 0
         and len(result.errors) == 1
         and isinstance(result.errors[0][0], SerialTestCase)

src/engine/SCons/MemoizeTests.py

 import sys
 import unittest
 
+import TestUnit
+
 import SCons.Memoize
 
 
     for tclass in tclasses:
         names = unittest.getTestCaseNames(tclass, 'test_')
         suite.addTests(list(map(tclass, names)))
-    if not unittest.TextTestRunner().run(suite).wasSuccessful():
-        sys.exit(1)
+    TestUnit.run(suite)
 
 # Local Variables:
 # tab-width:4

src/engine/SCons/Node/AliasTests.py

 import sys
 import unittest
 
+import TestUnit
+
 import SCons.Errors
 import SCons.Node.Alias
 
     for tclass in tclasses:
         names = unittest.getTestCaseNames(tclass, 'test_')
         suite.addTests(list(map(tclass, names)))
-    if not unittest.TextTestRunner().run(suite).wasSuccessful():
-        sys.exit(1)
+    TestUnit.run(suite)
 
 # Local Variables:
 # tab-width:4

src/engine/SCons/Node/FSTests.py

 import sys
 import time
 import unittest
-from TestCmd import TestCmd
 import shutil
 import stat
 
+from TestCmd import TestCmd
+import TestUnit
+
 import SCons.Errors
 import SCons.Node.FS
 import SCons.Util
     for tclass in tclasses:
         names = unittest.getTestCaseNames(tclass, 'test_')
         suite.addTests(list(map(tclass, names)))
-    if not unittest.TextTestRunner().run(suite).wasSuccessful():
-        sys.exit(1)
+    TestUnit.run(suite)
 
 # Local Variables:
 # tab-width:4

src/engine/SCons/Node/NodeTests.py

 import sys
 import unittest
 
+import TestUnit
+
 import SCons.Errors
 import SCons.Node
 import SCons.Util
     for tclass in tclasses:
         names = unittest.getTestCaseNames(tclass, 'test_')
         suite.addTests(list(map(tclass, names)))
-    if not unittest.TextTestRunner().run(suite).wasSuccessful():
-        sys.exit(1)
+    TestUnit.run(suite)
 
 # Local Variables:
 # tab-width:4

src/engine/SCons/Node/PythonTests.py

 import sys
 import unittest
 
+import TestUnit
+
 import SCons.Errors
 import SCons.Node.Python
 
     for tclass in tclasses:
         names = unittest.getTestCaseNames(tclass, 'test_')
         suite.addTests(list(map(tclass, names)))
-    if not unittest.TextTestRunner().run(suite).wasSuccessful():
-        sys.exit(1)
+    TestUnit.run(suite)
 
 # Local Variables:
 # tab-width:4

src/engine/SCons/PathListTests.py

 import sys
 import unittest
 
+import TestUnit
+
 import SCons.PathList
 
 
     for tclass in tclasses:
         names = unittest.getTestCaseNames(tclass, 'test_')
         suite.addTests(list(map(tclass, names)))
-    if not unittest.TextTestRunner().run(suite).wasSuccessful():
-        sys.exit(1)
+    TestUnit.run(suite)
 
 # Local Variables:
 # tab-width:4

src/engine/SCons/Platform/PlatformTests.py

 import sys
 import unittest
 
+import TestUnit
+
 import SCons.Errors
 import SCons.Platform
 
 
 if __name__ == "__main__":
     suite = unittest.makeSuite(PlatformTestCase, 'test_')
-    if not unittest.TextTestRunner().run(suite).wasSuccessful():
-        sys.exit(1)
+    TestUnit.run(suite)
 
 # Local Variables:
 # tab-width:4

src/engine/SCons/SConfTests.py

 import unittest
 
 import TestCmd
+import TestUnit
+
 
 sys.stdout = io.StringIO()
 
 
 if __name__ == "__main__":
     suite = unittest.makeSuite(SConfTestCase, 'test_')
-    res = unittest.TextTestRunner().run(suite)
-    if not res.wasSuccessful():
-        sys.exit(1)
+    TestUnit.run(suite)
 
 # Local Variables:
 # tab-width:4

src/engine/SCons/SConsignTests.py

 
 import os
 import sys
+import unittest
+
 import TestCmd
-import unittest
+import TestUnit
 
 import SCons.dblite
 
     for tclass in tclasses:
         names = unittest.getTestCaseNames(tclass, 'test_')
         suite.addTests(list(map(tclass, names)))
-    if not unittest.TextTestRunner().run(suite).wasSuccessful():
-        sys.exit(1)
+    TestUnit.run(suite)
 
 # Local Variables:
 # tab-width:4

src/engine/SCons/Scanner/CTests.py

 import collections
 import os
 import sys
+import unittest
+
 import TestCmd
-import unittest
+import TestUnit
 
 import SCons.Node.FS
 import SCons.Warnings
     return suite
 
 if __name__ == "__main__":
-    runner = unittest.TextTestRunner()
-    result = runner.run(suite())
-    if not result.wasSuccessful():
-        sys.exit(1)
+    TestUnit.run(suite())
 
 # Local Variables:
 # tab-width:4

src/engine/SCons/Scanner/DirTests.py

 import unittest
 
 import TestCmd
+import TestUnit
+
 import SCons.Node.FS
 import SCons.Scanner.Dir
 
     return suite
 
 if __name__ == "__main__":
-    runner = unittest.TextTestRunner()
-    result = runner.run(suite())
-    if not result.wasSuccessful():
-        sys.exit(1)
+    TestUnit.run(suite())
 
 # Local Variables:
 # tab-width:4

src/engine/SCons/Scanner/FortranTests.py

 import SCons.Warnings
 
 import TestCmd
+import TestUnit
 
 original = os.getcwd()
 
     return suite
 
 if __name__ == "__main__":
-    runner = unittest.TextTestRunner()
-    result = runner.run(suite())
-    if not result.wasSuccessful():
-        sys.exit(1)
+    TestUnit.run(suite())
 
 # Local Variables:
 # tab-width:4

src/engine/SCons/Scanner/IDLTests.py

 
 __revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
 
-import TestCmd
-import SCons.Scanner.IDL
 import unittest
 import sys
 import os
 import os.path
+
+import TestCmd
+import TestUnit
+
+import SCons.Scanner.IDL
 import SCons.Node.FS
 import SCons.Warnings
 
     return suite
 
 if __name__ == "__main__":
-    runner = unittest.TextTestRunner()
-    result = runner.run(suite())
-    if not result.wasSuccessful():
-        sys.exit(1)
+    TestUnit.run(suite())
 
 # Local Variables:
 # tab-width:4

src/engine/SCons/Scanner/LaTeXTests.py

 import unittest
 
 import TestCmd
+import TestUnit
+
 import SCons.Node.FS
 import SCons.Scanner.LaTeX
 
     return suite
 
 if __name__ == "__main__":
-    runner = unittest.TextTestRunner()
-    result = runner.run(suite())
-    if not result.wasSuccessful():
-        sys.exit(1)
+    TestUnit.run(suite())
 
 # Local Variables:
 # tab-width:4

src/engine/SCons/Scanner/ProgTests.py

 import unittest
 
 import TestCmd
+import TestUnit
+
 import SCons.Node.FS
 import SCons.Scanner.Prog
 
     return suite
 
 if __name__ == "__main__":
-    runner = unittest.TextTestRunner()
-    result = runner.run(suite())
-    if not result.wasSuccessful():
-        sys.exit(1)
+    TestUnit.run(suite())
 
 # Local Variables:
 # tab-width:4

src/engine/SCons/Scanner/RCTests.py

 
 __revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
 
-import TestCmd
-import SCons.Scanner.RC
 import unittest
 import sys
 import collections
 import os
+
+import TestCmd
+import TestUnit
+
+import SCons.Scanner.RC
 import SCons.Node.FS
 import SCons.Warnings
 
     return suite
 
 if __name__ == "__main__":
-    runner = unittest.TextTestRunner()
-    result = runner.run(suite())
-    if not result.wasSuccessful():
-        sys.exit(1)
+    TestUnit.run(suite())
 
 # Local Variables:
 # tab-width:4

src/engine/SCons/Scanner/ScannerTests.py

 import sys
 import unittest
 
+import TestUnit
+
 import SCons.Scanner
 
 class DummyFS(object):
     return suite
 
 if __name__ == "__main__":
-    runner = unittest.TextTestRunner()
-    result = runner.run(suite())
-    if not result.wasSuccessful():
-        sys.exit(1)
+    TestUnit.run(suite())
 
 # Local Variables:
 # tab-width:4

src/engine/SCons/Script/MainTests.py

 __revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
 
 import unittest
+
+import TestUnit
+
 import SCons.Errors
 import SCons.Script.Main
 
     for tclass in tclasses:
         names = unittest.getTestCaseNames(tclass, 'test_')
         suite.addTests(list(map(tclass, names)))
-    if not unittest.TextTestRunner().run(suite).wasSuccessful():
-        sys.exit(1)
+    TestUnit.run(suite)
 
 # Local Variables:
 # tab-width:4

src/engine/SCons/SubstTests.py

 
 from collections import UserDict
 
+import TestUnit
+
 import SCons.Errors
 
 from SCons.Subst import *
     for tclass in tclasses:
         names = unittest.getTestCaseNames(tclass, 'test_')
         suite.addTests(list(map(tclass, names)))
-    if not unittest.TextTestRunner().run(suite).wasSuccessful():
-        sys.exit(1)
+    TestUnit.run(suite)
 
 # Local Variables:
 # tab-width:4

src/engine/SCons/TaskmasterTests.py

 import sys
 import unittest
 
+import TestUnit
+
 import SCons.Taskmaster
 import SCons.Errors
 
 
 if __name__ == "__main__":
     suite = unittest.makeSuite(TaskmasterTestCase, 'test_')
-    if not unittest.TextTestRunner().run(suite).wasSuccessful():
-        sys.exit(1)
+    TestUnit.run(suite)
 
 # Local Variables:
 # tab-width:4

src/engine/SCons/Tool/JavaCommonTests.py

 import sys
 import unittest
 
+import TestUnit
+
+import SCons.Scanner.IDL
 import SCons.Tool.JavaCommon
 
 
     for tclass in tclasses:
         names = unittest.getTestCaseNames(tclass, 'test_')
         suite.addTests(list(map(tclass, names)))
-    if not unittest.TextTestRunner().run(suite).wasSuccessful():
-        sys.exit(1)
+    TestUnit.run(suite)
 
 # Local Variables:
 # tab-width:4

src/engine/SCons/Tool/PharLapCommonTests.py

 import os
 import sys
 
+import TestUnit
+
 import SCons.Errors
 from SCons.Tool.PharLapCommon import *
 
 
 if __name__ == "__main__":
     suite = unittest.makeSuite(PharLapCommonTestCase, 'test_')
-    if not unittest.TextTestRunner().run(suite).wasSuccessful():
-        sys.exit(1)
+    TestUnit.run(suite)
 
 # Local Variables:
 # tab-width:4

src/engine/SCons/Tool/ToolTests.py

 import sys
 import unittest
 
+import TestUnit
+
 import SCons.Errors
 import SCons.Tool
 
 
 if __name__ == "__main__":
     suite = unittest.makeSuite(ToolTestCase, 'test_')
-    if not unittest.TextTestRunner().run(suite).wasSuccessful():
-        sys.exit(1)
+    TestUnit.run(suite)
 
 # Local Variables:
 # tab-width:4

src/engine/SCons/Tool/javacTests.py

 import os
 import unittest
 
+import TestUnit
+
 import SCons.Tool.javac
 
 class DummyNode(object):
             '')
 
 if __name__ == "__main__":
-    unittest.main()
+    suite = unittest.makeSuite(pathoptTestCase, 'test_')
+    TestUnit.run(suite)

src/engine/SCons/Tool/msvsTests.py

 
 import os
 import sys
-import TestCmd
 import unittest
 import copy
 
+import TestCmd
+import TestUnit
+
 from SCons.Tool.msvs import *
 from SCons.Tool.MSCommon.vs import SupportedVSList
 import SCons.Util
                     del os.environ[k]
 
             suite = unittest.makeSuite(test_class, 'test_')
-            if not unittest.TextTestRunner().run(suite).wasSuccessful():
+            if not TestUnit.cli.get_runner()().run(suite).wasSuccessful():
                 exit_val = 1
         finally:
             os.env = back_osenv

src/engine/SCons/Tool/wixTests.py

 from SCons.Environment import Environment
 
 import TestCmd
+import TestUnit
+
 
 # create fake candle and light, so the tool's exists() method will succeed
 test = TestCmd.TestCmd(workdir = '')
 
 if __name__ == "__main__":
     suite = unittest.makeSuite(WixTestCase, 'test_')
-    if not unittest.TextTestRunner().run(suite).wasSuccessful():
-        sys.exit(1)
+    TestUnit.run(suite)
 
 # Local Variables:
 # tab-width:4

src/engine/SCons/UtilTests.py

 from collections import UserDict, UserList, UserString
 
 import TestCmd
+import TestUnit
 
 import SCons.Errors
 
     for tclass in tclasses:
         names = unittest.getTestCaseNames(tclass, 'test_')
         suite.addTests(list(map(tclass, names)))
-    if not unittest.TextTestRunner().run(suite).wasSuccessful():
-        sys.exit(1)
+    TestUnit.run(suite)
 
 # Local Variables:
 # tab-width:4

src/engine/SCons/Variables/BoolVariableTests.py

 import sys
 import unittest
 
+import TestUnit
+
 import SCons.Errors
 import SCons.Variables
 
 
 if __name__ == "__main__":
     suite = unittest.makeSuite(BoolVariableTestCase, 'test_')
-    if not unittest.TextTestRunner().run(suite).wasSuccessful():
-        sys.exit(1)
+    TestUnit.run(suite)
 
 # Local Variables:
 # tab-width:4

src/engine/SCons/Variables/EnumVariableTests.py

 import sys
 import unittest
 
+import TestUnit
+
 import SCons.Errors
 import SCons.Variables
 
 
 if __name__ == "__main__":
     suite = unittest.makeSuite(EnumVariableTestCase, 'test_')
-    if not unittest.TextTestRunner().run(suite).wasSuccessful():
-        sys.exit(1)
+    TestUnit.run(suite)
 
 # Local Variables:
 # tab-width:4

src/engine/SCons/Variables/ListVariableTests.py

 import sys
 import unittest
 
+import TestUnit
+
 import SCons.Errors
 import SCons.Variables
 
 
 if __name__ == "__main__":
     suite = unittest.makeSuite(ListVariableTestCase, 'test_')
-    if not unittest.TextTestRunner().run(suite).wasSuccessful():
-        sys.exit(1)
+    TestUnit.run(suite)
 
 # Local Variables:
 # tab-width:4

src/engine/SCons/Variables/PackageVariableTests.py

 import SCons.Variables
 
 import TestCmd
+import TestUnit
+
 
 class PackageVariableTestCase(unittest.TestCase):
     def test_PackageVariable(self):
 
 if __name__ == "__main__":
     suite = unittest.makeSuite(PackageVariableTestCase, 'test_')
-    if not unittest.TextTestRunner().run(suite).wasSuccessful():
-        sys.exit(1)
+    TestUnit.run(suite)
 
 # Local Variables:
 # tab-width:4

src/engine/SCons/Variables/PathVariableTests.py

 import SCons.Variables
 
 import TestCmd
+import TestUnit
+
 
 class PathVariableTestCase(unittest.TestCase):
     def test_PathVariable(self):
 
 if __name__ == "__main__":
     suite = unittest.makeSuite(PathVariableTestCase, 'test_')
-    if not unittest.TextTestRunner().run(suite).wasSuccessful():
-        sys.exit(1)
+    TestUnit.run(suite)
 
 # Local Variables:
 # tab-width:4

src/engine/SCons/Variables/VariablesTests.py

 
 import sys
 import unittest
+
 import TestSCons
+import TestUnit
 
 import SCons.Variables
 import SCons.Subst
     for tclass in tclasses:
         names = unittest.getTestCaseNames(tclass, 'test_')
         suite.addTests(list(map(tclass, names)))
-    if not unittest.TextTestRunner().run(suite).wasSuccessful():
-        sys.exit(1)
+    TestUnit.run(suite)
 
 # Local Variables:
 # tab-width:4

src/engine/SCons/WarningsTests.py

 
 import sys
 import unittest
+
+import TestUnit
+
 import SCons.Warnings
 
 class TestOutput(object):
 
 if __name__ == "__main__":
     suite = unittest.makeSuite(WarningsTestCase, 'test_')
-    if not unittest.TextTestRunner().run(suite).wasSuccessful():
-        sys.exit(1)
+    TestUnit.run(suite)
 
 # Local Variables:
 # tab-width:4

src/engine/SCons/cppTests.py

 import sys
 import unittest
 
+import TestUnit
+
 import cpp
 
 
             pass
         names.sort()
         suite.addTests(list(map(tclass, names)))
-    if not unittest.TextTestRunner().run(suite).wasSuccessful():
-        sys.exit(1)
+    TestUnit.run(suite)
 
 # Local Variables:
 # tab-width:4

test/runtest/testargv.py

+#!/usr/bin/env python
+#
+# __COPYRIGHT__
+#
+# Permission is hereby granted, free of charge, to any person obtaining
+# a copy of this software and associated documentation files (the
+# "Software"), to deal in the Software without restriction, including
+# without limitation the rights to use, copy, modify, merge, publish,
+# distribute, sublicense, and/or sell copies of the Software, and to
+# permit persons to whom the Software is furnished to do so, subject to
+# the following conditions:
+#
+# The above copyright notice and this permission notice shall be included
+# in all copies or substantial portions of the Software.
+#
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
+# KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
+# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+#
+
+__revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
+
+"""
+Test subdir args for runtest.py, for example:
+
+    python runtest.py test/subdir
+
+"""
+
+import os
+
+import TestRuntest
+
+test = TestRuntest.TestRuntest()
+test.subdir('test', ['test', 'subdir'])
+
+files = {}
+files['pythonstring'] = TestRuntest.pythonstring
+
+files['one'] = os.path.join('test/subdir', 'test_one.py')
+files['two'] = os.path.join('test/subdir', 'two.py')
+files['three'] = os.path.join('test', 'test_three.py')
+
+test.write_passing_test(files['one'])
+test.write_passing_test(files['two'])
+test.write_passing_test(files['three'])
+
+expect_stdout = """\
+%(pythonstring)s -tt %(one)s
+PASSING TEST STDOUT
+%(pythonstring)s -tt %(two)s
+PASSING TEST STDOUT
+""" % files
+
+expect_stderr = """\
+PASSING TEST STDERR
+PASSING TEST STDERR
+"""
+
+test.run(arguments = '--no-progress test/subdir',
+         status = 0,
+         stdout = expect_stdout,
+         stderr = expect_stderr)
+
+test.pass_test()
+
+# Local Variables:
+# tab-width:4
+# indent-tabs-mode:nil
+# End:
+# vim: set expandtab tabstop=4 shiftwidth=4:

testing/framework/TestUnit/__init__.py

+
+__all__ = ['TAPTestRunner', 'TAPTestResult', 'run']
+
+from .taprunner import TAPTestRunner, TAPTestResult
+from .cli import run

testing/framework/TestUnit/cli.py

+"""
+Choose test runner class from --runner command line option
+and execute test cases.
+"""
+
+import unittest
+import optparse
+import sys
+
+
+def get_runner():
+    parser = optparse.OptionParser()
+    parser.add_option('--runner', default='unittest.TextTestRunner',
+                                  help='name of test runner class to use')
+    opts, args = parser.parse_args()
+
+    fromsplit = opts.runner.rsplit('.', 1)
+    if len(fromsplit) < 2:
+        raise ValueError('Can\'t use module as a runner')
+    else:
+        runnermod = __import__(fromsplit[0])
+    return getattr(runnermod, fromsplit[1])
+
+
+def run(suite=None):
+    runner = get_runner()
+    if suite:
+        if not runner().run(suite).wasSuccessful():
+            sys.exit(1)
+    else:
+        unittest.main(argv=sys.argv[:1], testRunner=runner)
+
+
+if __name__ == '__main__':
+    run()

testing/framework/TestUnit/taprunner.py

+"""
+Format unittest results in Test Anything Protocol (TAP).
+http://testanything.org/tap-version-13-specification.html
+
+Public domain work by:
+  anatoly techtonik <techtonik@gmail.com>
+
+"""
+
+from unittest import suite
+from unittest.runner import TextTestRunner, TextTestResult
+
+__version__ = "0.1"
+
+class TAPTestResult(TextTestResult):
+
+    def _process(self, test, msg, failtype = None, directive = None):
+        """ increase the counter, format and output TAP info """
+        # counterhack: increase test counter
+        test.suite.tap_counter += 1
+        msg = "%s %d" % (msg, test.suite.tap_counter)
+        if "not" not in msg:
+            msg += "    "  # justify
+        self.stream.write("%s - " % msg)
+        if failtype:
+            self.stream.write("%s - " % failtype)
+        self.stream.write("%s" % test.__class__.__name__)
+        self.stream.write(".%s" % test._testMethodName)
+        if directive:
+            self.stream.write(directive)
+        self.stream.write("\n")
+        # [ ] write test __doc__ (if exists) in comment
+        self.stream.flush()
+
+    def addSuccess(self, test):
+        super(TextTestResult, self).addSuccess(test)
+        self._process(test, "ok")
+
+    def addFailure(self, test, err):
+        super(TextTestResult, self).addFailure(test, err)
+        self._process(test, "not ok", "FAIL")
+        # [ ] add structured data about assertion
+
+    def addError(self, test, err):
+        super(TextTestResult, self).addError(test, err)
+        self._process(test, "not ok", "ERROR")
+        # [ ] add structured data about exception
+
+    def addSkip(self, test, reason):
+        super(TextTestResult, self).addSkip(test, reason)
+        self._process(test, "ok", directive=("  # SKIP  %s" % reason))
+
+    def addExpectedFailure(self, test, err):
+        super(TextTestResult, self).addExpectedFailure(test, err)
+        self._process(test, "not ok", directive=("  # TODO"))
+
+    def addUnexpectedSuccess(self, test):
+        super(TextTestResult, self).addUnexpectedSuccess(test)
+        self._process(test, "not ok", "FAIL (unexpected success)")
+
+    """
+    def printErrors(self):
+    def printErrorList(self, flavour, errors):
+    """
+
+
+class TAPTestRunner(TextTestRunner):
+    resultclass = TAPTestResult
+
+    def run(self, test):
+        self.stream.write("TAP version 13\n")
+        # [ ] add commented block with test suite __doc__
+        # [ ] check call with a single test
+        # if isinstance(test, suite.TestSuite):
+        self.stream.write("1..%s\n" % len(list(test)))
+
+        # counterhack: inject test counter into test suite
+        test.tap_counter = 0
+        # counterhack: inject reference to suite into each test case
+        for case in test:
+            case.suite = test
+
+        return super(TAPTestRunner, self).run(test)
+
+
+if __name__ == "__main__":
+    import sys
+    import unittest
+
+    class Test(unittest.TestCase):
+       def test_ok(self):
+           pass
+       def test_fail(self):
+           self.assertTrue(False)
+       def test_error(self):
+           bad_symbol
+       @unittest.skip("skipin'")
+       def test_skip(self):
+           pass
+       @unittest.expectedFailure
+       def test_not_ready(self):
+           self.fail()
+       @unittest.expectedFailure
+       def test_invalid_fail_mark(self):
+           pass
+       def test_another_ok(self):
+           pass
+
+
+    suite = unittest.TestSuite([
+       Test('test_ok'),
+       Test('test_fail'),
+       Test('test_error'),
+       Test('test_skip'),
+       Test('test_not_ready'),
+       Test('test_invalid_fail_mark'),
+       Test('test_another_ok')
+    ])
+    if not TAPTestRunner().run(suite).wasSuccessful():
+       sys.exit(1)