Salome HOME
PN bug
[tools/eficas.git] / Tests / run.py
1 import sys,types,os
2 import sre
3 import unittest
4 from optparse import OptionParser
5
6 sys.path[:0]=[".."]
7
8 testMatch = sre.compile(r'^[Tt]est')
9
10 class TestSuite(unittest.TestSuite):
11     loader = unittest.defaultTestLoader
12
13     def __init__(self, names=[]):
14         self.names=names
15         super(TestSuite,self).__init__()
16         tests=self.collectTests()
17         self.addTests(tests)
18
19     def _import(self,name):
20         mod = __import__(name,{},{})
21         components = name.split('.')
22         for comp in components[1:]:
23             mod = getattr(mod,comp)
24         return mod
25
26     def collectTests(self):
27         if self.names:
28            entries=self.names
29         else:
30            entries = [ item for item in os.listdir(os.getcwd())
31                         if item.lower().find('test') >= 0 ]
32
33         tests=[]
34         for item in entries:
35             path=os.path.abspath(os.path.join(os.getcwd(),item))
36             if os.path.isfile(item):
37                 root, ext = os.path.splitext(item)
38                 if ext == '.py':
39                     if root.find('/') >= 0:
40                        dirname, file = os.path.split(path)
41                        root, ext = os.path.splitext(file)
42                        sys.path.insert(0,dirname)
43                        mod=self._import(root)
44                        sys.path.remove(dirname)
45                     else:
46                        mod=self._import(root)
47                     tests.append(ModuleTestSuite(mod))
48             elif os.path.isdir(item):
49                 init = os.path.abspath(os.path.join(item,'__init__.py'))
50                 if os.path.isfile(init):
51                    package=self._import(item)
52                    if package:
53                       tests.append(TestPackageSuite(package))
54                 else:
55                    tests.append(TestDirectorySuite(path))
56         return tests
57
58 class TestDirectorySuite(TestSuite):
59     ignore=[]
60     def __init__(self,path):
61         self.path=path
62         super(TestDirectorySuite,self).__init__()
63
64     def collectTests(self):
65         tests=[]
66         if self.path:
67             sys.path.insert(0,self.path)
68             entries = os.listdir(self.path)
69             entries.sort()
70             for item in entries:
71                 if (item[0] == '.'
72                     or item in self.ignore
73                     or not testMatch.search(item)):
74                     continue
75                 item_path = os.path.abspath(os.path.join(self.path,item))
76                 if os.path.isfile(item_path):
77                     root, ext = os.path.splitext(item)
78                     if ext != '.py':
79                         continue
80                     if root.find('/') >= 0:
81                        dirname, file = os.path.split(item_path)
82                        root, ext = os.path.splitext(file)
83                        sys.path.insert(0,dirname)
84                        mod=self._import(root)
85                        sys.path.remove(dirname)
86                     else:
87                        mod=self._import(root)
88                     tests.append(ModuleTestSuite(mod))
89                 elif os.path.isdir(item_path):
90                     init = os.path.abspath(os.path.join(item_path,'__init__.py'))
91                     if os.path.isfile(init):
92                         package=self._import(item)
93                         if package:
94                            tests.append(TestPackageSuite(package))
95                     else:
96                         tests.append(TestDirectorySuite(item_path))
97             sys.path.remove(self.path)
98         return tests
99
100 class TestPackageSuite(TestDirectorySuite):
101     def __init__(self,package):
102         self.package=package
103         path=os.path.abspath(os.path.dirname(self.package.__file__))
104         super(TestPackageSuite,self).__init__(path)
105
106     def collectTests(self):
107         tests=[]
108         if self.path:
109             sys.path.insert(0,self.path)
110             entries = os.listdir(self.path)
111             entries.sort()
112             for item in entries:
113                 if (item[0] == '.'
114                     or item in self.ignore
115                     or not testMatch.search(item)):
116                     continue
117                 item_path = os.path.abspath(os.path.join(self.path,item))
118                 if os.path.isfile(item_path):
119                     root, ext = os.path.splitext(item)
120                     if ext != '.py':
121                         continue
122                     name="%s.%s" % (self.package.__name__,root)
123                     mod=self._import(name)
124                     tests.append(ModuleTestSuite(mod))
125                 elif os.path.isdir(item_path):
126                     init = os.path.abspath(os.path.join(item_path,'__init__.py'))
127                     if os.path.isfile(init):
128                         name="%s.%s" % (self.package.__name__,item)
129                         package=self._import(name)
130                         if package:
131                            tests.append(TestPackageSuite(package))
132                     else:
133                         tests.append(TestDirectorySuite(item_path))
134             sys.path.remove(self.path)
135         return tests
136
137
138 class ModuleTestSuite(TestSuite):
139
140     def __init__(self, module):
141         self.module = module
142         super(ModuleTestSuite,self).__init__()
143
144     def collectTests(self):
145         def cmpLineNo(a,b):
146             a_ln = a.func_code.co_firstlineno
147             b_ln = b.func_code.co_firstlineno
148             return cmp(a_ln,b_ln)
149
150         entries = dir(self.module)
151         tests = []
152         func_tests = []
153         for item in entries:
154             test = getattr(self.module,item)
155             if (isinstance(test, (type, types.ClassType))
156                 and issubclass(test,unittest.TestCase)):
157                 if testMatch.search(item):
158                     [ tests.append(case) for case in
159                       self.loader.loadTestsFromTestCase(test)._tests ]
160             elif callable(test) and testMatch.search(item):
161                 # simple functional test
162                 func_tests.append(test)
163
164         # run functional tests in the order in which they are defined
165         func_tests.sort(cmpLineNo)
166         [ tests.append(unittest.FunctionTestCase(test))
167           for test in func_tests ]
168         return tests
169
170 class TestProgram(unittest.TestProgram):
171     USAGE="""
172 """
173     def __init__(self,testRunner=None):
174         self.testRunner = testRunner
175         self.verbosity = 1
176         self.parseArgs(sys.argv)
177         self.createTests()
178         self.runTests()
179
180     def parseArgs(self,argv):
181         parser = OptionParser(usage=self.USAGE)
182         parser.add_option("-v","--verbose",action="count",
183                           dest="verbosity",default=1,
184                           help="Be more verbose. ")
185
186         options, args = parser.parse_args(argv)
187         self.verbosity = options.verbosity
188
189         if args:
190             self.names = list(args)
191             if self.names[0] == 'run.py':
192                 self.names = self.names[1:]
193
194     def createTests(self):
195         self.test = TestSuite(self.names)
196
197
198 main = TestProgram
199
200 if __name__ == "__main__":
201     main()
202