Package pysys :: Package unit :: Module pyunit
[hide private]
[frames] | no frames]

Source Code for Module pysys.unit.pyunit

  1  # PySys System Test Framework, Copyright (C) 2006-2013  M.B.Grieve 
  2   
  3  # This library is free software; you can redistribute it and/or 
  4  # modify it under the terms of the GNU Lesser General Public 
  5  # License as published by the Free Software Foundation; either 
  6  # version 2.1 of the License, or (at your option) any later version. 
  7   
  8  # This library is distributed in the hope that it will be useful, 
  9  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 10  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
 11  # Lesser General Public License for more details. 
 12   
 13  # You should have received a copy of the GNU Lesser General Public 
 14  # License along with this library; if not, write to the Free Software 
 15  # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 
 16   
 17  # Contact: moraygrieve@users.sourceforge.net 
 18  """ 
 19  Contains the test class used to run pyunit tests 
 20  """ 
 21   
 22  from pysys import ThreadFilter 
 23  from pysys.constants import * 
 24  from pysys.basetest import BaseTest 
 25  import glob, os, unittest 
 26   
27 -class PyUnitTest(BaseTest):
28 """ 29 Class for running PyUnit tests (standard Python unittest module). The 30 execute method will execute all the .py files, find all the 31 unittest.TestCase classes within those files and run the test methods 32 within them. A separate Python process will be spawned for each input 33 test file. By default child Python processes will have the same 34 PYTHONPATH as the python process which is running pysys. However, 35 this can be changed by overriding the getPythonPath() method. 36 """ 37
38 - def execute(self):
39 """ 40 Implementation of the execute() abstract method which simply 41 calls executePyUnitTests() 42 """ 43 self.executePyUnitTests()
44
45 - def executePyUnitTests(self):
46 """ 47 Run all the PyUnit tests in the Input directory. 48 """ 49 pyfiles = self._findPythonFiles() 50 for pyfile in pyfiles: 51 self._runTestFile(pyfile)
52
53 - def _findPythonFiles(self):
54 return glob.glob(os.path.join(self.input , '*.py'))
55
56 - def _runTestFile(self, testFile):
57 globals = {} 58 locals = {} 59 command = sys.executable 60 displayName = 'PyUnit' 61 instance = self.getInstanceCount(displayName) 62 dstdout = os.path.join(self.output, 'pyunit.out') 63 dstderr = os.path.join(self.output, 'pyunit.err') 64 if instance: dstdout = "%s.%d" % (dstdout, instance) 65 if instance: dstderr = "%s.%d" % (dstderr, instance) 66 arguments = [__file__, testFile] 67 filter = ThreadFilter() 68 self.log.addFilter(filter) 69 environ = os.environ.copy() 70 environ['PYTHONPATH'] = os.pathsep.join(self.getPythonPath() + sys.path) 71 process = self.startProcess(command, arguments, environ, self.output, FOREGROUND, DEFAULT_TIMEOUT, dstdout, dstderr, displayName) 72 self.log.removeFilter(filter) 73 if process.exitStatus: 74 self.outcome.append(FAILED) 75 else: 76 self.outcome.append(PASSED) 77 for l in open(dstdout): 78 self.log.info(l.rstrip())
79
80 - def getPythonPath(self):
81 """Override this method to return a sequence of paths to put 82 at the beginning of the PYTHONPATH when running the PyUnit 83 tests. See PyUnit_test_002 for an example of this. 84 """ 85 return []
86 87 if __name__ == '__main__': 88
89 - class PysysTestResult(unittest.TestResult):
90
91 - def __init__(self):
92 unittest.TestResult.__init__(self) 93 self.successes = []
94
95 - def addSuccess(self, test):
96 self.successes.append(test)
97
98 - def getTestClasses(testFile):
99 globals = {} 100 # Use globals dictionary for locals as well because we 101 # want to treat this like it is being run in a global 102 # (file scope) context) 103 execfile(testFile, globals, globals) 104 testClasses = [] 105 for k, v in globals.items(): 106 if isinstance(v, type(unittest.TestCase)): 107 testClasses.append(v) 108 return (testClasses, globals)
109
110 - def createTestSuite(testFile):
111 suite = unittest.TestSuite() 112 loader = unittest.TestLoader() 113 testClasses, globals = getTestClasses(testFile) 114 for testClass in testClasses: 115 methods = loader.getTestCaseNames(testClass) 116 for method in methods: 117 suite.addTest(testClass(method)) 118 return (suite, globals)
119
120 - def getTestName(testcase):
121 name = testcase.id() 122 return name.replace('__builtin__.','')
123 124 testFile = sys.argv[1] 125 126 suite, globals = createTestSuite(testFile) 127 results = PysysTestResult() 128 129 globals['_suite_'] = suite 130 globals['_results_'] = results 131 132 eval('_suite_.run(_results_)', globals) 133 134 for r in results.successes: 135 print getTestName(r), '... passed' 136 for r in results.errors: 137 print getTestName(r[0]), '... failed' 138 print r[1].rstrip() 139 for r in results.failures: 140 print getTestName(r[0]), '... failed' 141 print r[1].rstrip() 142 143 if results.wasSuccessful(): 144 sys.exit(0) 145 else: 146 sys.exit(1) 147