1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
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
39 """
40 Implementation of the execute() abstract method which simply
41 calls executePyUnitTests()
42 """
43 self.executePyUnitTests()
44
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
54 return glob.glob(os.path.join(self.input , '*.py'))
55
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
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
90
92 unittest.TestResult.__init__(self)
93 self.successes = []
94
96 self.successes.append(test)
97
99 globals = {}
100
101
102
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
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
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