Merge lp:~madteam/mg5amcnlo/test-framework-dev into lp:~madteam/mg5amcnlo/framework-dev
- test-framework-dev
- Merge into framework-dev
Proposed by
Olivier Mattelaer
Status: | Merged |
---|---|
Approved by: | Michel Herquet |
Approved revision: | 30 |
Merged at revision: | not available |
Proposed branch: | lp:~madteam/mg5amcnlo/test-framework-dev |
Merge into: | lp:~madteam/mg5amcnlo/framework-dev |
Diff against target: |
682 lines 2 files modified
bin/test_manager.py (+314/-0) tests/unit_tests/bin/test_test_manager.py (+357/-0) |
To merge this branch: | bzr merge lp:~madteam/mg5amcnlo/test-framework-dev |
Related bugs: |
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
Michel Herquet (community) | full | Approve | |
Review via email: mp+12875@code.launchpad.net |
Commit message
Description of the change
To post a comment you must log in.
Revision history for this message
Olivier Mattelaer (olivier-mattelaer) wrote : | # |
Revision history for this message
Michel Herquet (herquet) wrote : | # |
Perfect! Ok for merging
review:
Approve
(full)
Preview Diff
[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1 | === added file 'bin/test_manager.py' | |||
2 | --- bin/test_manager.py 1970-01-01 00:00:00 +0000 | |||
3 | +++ bin/test_manager.py 2009-10-05 15:15:19 +0000 | |||
4 | @@ -0,0 +1,314 @@ | |||
5 | 1 | #!/usr/bin/env python | ||
6 | 2 | ############################################################################## | ||
7 | 3 | # | ||
8 | 4 | # Copyright (c) 2009 The MadGraph Development team and Contributors | ||
9 | 5 | # | ||
10 | 6 | # This file is a part of the MadGraph 5 project, an application which | ||
11 | 7 | # automatically generates Feynman diagrams and matrix elements for arbitrary | ||
12 | 8 | # high-energy processes in the Standard Model and beyond. | ||
13 | 9 | # | ||
14 | 10 | # It is subject to the MadGraph license which should accompany this | ||
15 | 11 | # distribution. | ||
16 | 12 | # | ||
17 | 13 | # For more information, please visit: http://madgraph.phys.ucl.ac.be | ||
18 | 14 | # | ||
19 | 15 | ############################################################################## | ||
20 | 16 | """ Manager for running the test library | ||
21 | 17 | |||
22 | 18 | This library offer a simple way to launch test. | ||
23 | 19 | |||
24 | 20 | To run a test/class of test/test file/module of test/... | ||
25 | 21 | you just have to launch | ||
26 | 22 | test_manager.run(NAME) | ||
27 | 23 | or | ||
28 | 24 | test_manager.run(LIST_OF_NAME) | ||
29 | 25 | |||
30 | 26 | the NAME can contain regular expression (in python re standard format) | ||
31 | 27 | """ | ||
32 | 28 | |||
33 | 29 | import unittest | ||
34 | 30 | import re | ||
35 | 31 | import os | ||
36 | 32 | import inspect | ||
37 | 33 | import sys | ||
38 | 34 | sys.path+=['.','..'] | ||
39 | 35 | |||
40 | 36 | ############################################################################## | ||
41 | 37 | def run(expression='', re_opt=0, package='./tests/'): | ||
42 | 38 | """ running the test associated to expression. By default, this launch all | ||
43 | 39 | test derivated from TestCase. expression can be the name of directory, | ||
44 | 40 | module, class, function or event standard regular expression (in re format) | ||
45 | 41 | """ | ||
46 | 42 | |||
47 | 43 | #init a test suite | ||
48 | 44 | testsuite = unittest.TestSuite() | ||
49 | 45 | collect = unittest.TestLoader() | ||
50 | 46 | for test_fct in Test_in_module(package=package, expression=expression, \ | ||
51 | 47 | re_opt=re_opt): | ||
52 | 48 | data=collect.loadTestsFromName(test_fct) | ||
53 | 49 | testsuite.addTest(data) | ||
54 | 50 | unittest.TextTestRunner(verbosity=1).run(testsuite) | ||
55 | 51 | |||
56 | 52 | |||
57 | 53 | ############################################################################## | ||
58 | 54 | class Test_in_module(list): | ||
59 | 55 | """ class introspecting the test module to find the available test. | ||
60 | 56 | |||
61 | 57 | The strategy is the following: | ||
62 | 58 | The routine collect_dir looks in all module/file to find the different | ||
63 | 59 | functions in different test class. This produce a list, on wich external | ||
64 | 60 | routines can loop on. | ||
65 | 61 | |||
66 | 62 | In order to authorize definition and loop on this object on the same time, | ||
67 | 63 | i.e: for test in Test_in_module([opt])-. At each time a loop is started, | ||
68 | 64 | we check if a collect_dir runned before, and run it if neccessary. And | ||
69 | 65 | that without any problem with future add-on on this class. | ||
70 | 66 | """ | ||
71 | 67 | |||
72 | 68 | search_class=unittest.TestCase | ||
73 | 69 | class ERRORTestManager(Exception): pass | ||
74 | 70 | |||
75 | 71 | ########################################################################## | ||
76 | 72 | def __init__(self, package='./tests/', expression='', re_opt=0): | ||
77 | 73 | """ initialize global variable for the test """ | ||
78 | 74 | |||
79 | 75 | list.__init__(self) | ||
80 | 76 | |||
81 | 77 | self.package = package | ||
82 | 78 | if self.package[-1] != '/': self.package+='/' | ||
83 | 79 | self.restrict_to(expression, re_opt) | ||
84 | 80 | |||
85 | 81 | ########################################################################## | ||
86 | 82 | def _check_if_obj_build(self): | ||
87 | 83 | """ Check if a collect is already done | ||
88 | 84 | Uses to have smart __iter__ and __contain__ functions | ||
89 | 85 | """ | ||
90 | 86 | if len(self) == 0: | ||
91 | 87 | self.collect_dir(self.package, checking=True) | ||
92 | 88 | |||
93 | 89 | ########################################################################## | ||
94 | 90 | def __iter__(self): | ||
95 | 91 | """ Check that a collect was performed (do it if needed) """ | ||
96 | 92 | self._check_if_obj_build() | ||
97 | 93 | return list.__iter__(self) | ||
98 | 94 | |||
99 | 95 | ########################################################################## | ||
100 | 96 | def __contains__(self,value): | ||
101 | 97 | """ Check that a collect was performed (do it if needed) """ | ||
102 | 98 | self._check_if_obj_build() | ||
103 | 99 | return list.__contains__(self,value) | ||
104 | 100 | |||
105 | 101 | ########################################################################## | ||
106 | 102 | def collect_dir(self, directory, checking=True): | ||
107 | 103 | """ Find the file and the subpackage in this package """ | ||
108 | 104 | |||
109 | 105 | for name in os.listdir(directory): | ||
110 | 106 | local_check = checking | ||
111 | 107 | |||
112 | 108 | status = self.status_file(directory + '/' + name) | ||
113 | 109 | if status is None: | ||
114 | 110 | continue | ||
115 | 111 | |||
116 | 112 | if checking: | ||
117 | 113 | if self.check_valid(directory + '/' + name): | ||
118 | 114 | local_check = False #since now perform all the test | ||
119 | 115 | |||
120 | 116 | if status == 'file': | ||
121 | 117 | self.collect_file(directory + '/' + name, local_check) | ||
122 | 118 | elif status == "module": | ||
123 | 119 | self.collect_dir(directory + '/' + name, local_check) | ||
124 | 120 | |||
125 | 121 | ########################################################################## | ||
126 | 122 | def collect_file(self, file, checking=True): | ||
127 | 123 | """ Find the different class instance derivated of TestCase """ | ||
128 | 124 | |||
129 | 125 | pyname=self.passin_pyformat(file) | ||
130 | 126 | exec('import '+pyname+' as obj') | ||
131 | 127 | |||
132 | 128 | #look at class | ||
133 | 129 | for name in dir(obj): | ||
134 | 130 | exec('class_=obj.'+name) | ||
135 | 131 | if inspect.isclass(class_) and issubclass(class_,unittest.TestCase): | ||
136 | 132 | if checking: | ||
137 | 133 | if self.check_valid(name): | ||
138 | 134 | check_inside=False | ||
139 | 135 | else: | ||
140 | 136 | check_inside=True | ||
141 | 137 | else: | ||
142 | 138 | check_inside=False | ||
143 | 139 | |||
144 | 140 | |||
145 | 141 | self.collect_function(class_, checking=check_inside, \ | ||
146 | 142 | base=pyname) | ||
147 | 143 | |||
148 | 144 | ########################################################################## | ||
149 | 145 | def collect_function(self, class_, checking=True, base=''): | ||
150 | 146 | """ | ||
151 | 147 | Find the different test function in this class | ||
152 | 148 | test functions should start with test | ||
153 | 149 | """ | ||
154 | 150 | |||
155 | 151 | if not inspect.isclass(class_): | ||
156 | 152 | raise self.ERRORTestManager, 'wrong input class_' | ||
157 | 153 | if not issubclass(class_,unittest.TestCase): | ||
158 | 154 | raise self.ERRORTestManager, 'wrong input class_' | ||
159 | 155 | |||
160 | 156 | #devellop the name | ||
161 | 157 | if base: | ||
162 | 158 | base+='.'+class_.__name__ | ||
163 | 159 | else: | ||
164 | 160 | base=class_.__name__ | ||
165 | 161 | |||
166 | 162 | candidate=[base+'.'+name for name in dir(class_) if \ | ||
167 | 163 | name.startswith('test')\ | ||
168 | 164 | and inspect.ismethod(eval('class_.'+name))] | ||
169 | 165 | |||
170 | 166 | if not checking: | ||
171 | 167 | self+=candidate | ||
172 | 168 | else: | ||
173 | 169 | self+=[name for name in candidate if self.check_valid(name)] | ||
174 | 170 | |||
175 | 171 | ########################################################################## | ||
176 | 172 | def restrict_to(self, expression, re_opt=0): | ||
177 | 173 | """ store in global the expression to fill in order to be a valid test """ | ||
178 | 174 | |||
179 | 175 | if isinstance(expression,list): | ||
180 | 176 | pass | ||
181 | 177 | elif isinstance(expression,basestring): | ||
182 | 178 | if expression=='': | ||
183 | 179 | expression=['.*'] #made an re authorizing all regular name | ||
184 | 180 | else: | ||
185 | 181 | expression = [expression] | ||
186 | 182 | else: | ||
187 | 183 | raise self.ERRORTestManager, 'obj should be list or string' | ||
188 | 184 | |||
189 | 185 | self.rule=[] | ||
190 | 186 | for expr in expression: | ||
191 | 187 | if not expr.startswith('^'): expr='^'+expr #fix the begin of the re | ||
192 | 188 | if not expr.endswith('$'): expr=expr+'$' #fix the end of the re | ||
193 | 189 | self.rule.append( re.compile(expr, re_opt) ) | ||
194 | 190 | |||
195 | 191 | ########################################################################## | ||
196 | 192 | def check_valid(self, name): | ||
197 | 193 | """ check if the name correspond to the rule """ | ||
198 | 194 | |||
199 | 195 | if not isinstance(name,basestring): | ||
200 | 196 | raise self.ERRORTestManager, 'check valid take a string argument' | ||
201 | 197 | |||
202 | 198 | for specific_format in self.format_possibility(name): | ||
203 | 199 | for expr in self.rule: | ||
204 | 200 | if expr.search(specific_format): | ||
205 | 201 | return True | ||
206 | 202 | return False | ||
207 | 203 | |||
208 | 204 | ########################################################################## | ||
209 | 205 | @staticmethod | ||
210 | 206 | def status_file(name): | ||
211 | 207 | """ check if a name is a module/a python file and return the status """ | ||
212 | 208 | if os.path.isfile(name): | ||
213 | 209 | if name.endswith('.py') and '__init__' not in name: | ||
214 | 210 | return 'file' | ||
215 | 211 | elif os.path.isdir(name): | ||
216 | 212 | if os.path.isfile(name+'/__init__.py'): | ||
217 | 213 | return 'module' | ||
218 | 214 | |||
219 | 215 | ########################################################################## | ||
220 | 216 | @classmethod | ||
221 | 217 | def passin_pyformat(cls,name): | ||
222 | 218 | """ transform a relative position in a python import format """ | ||
223 | 219 | |||
224 | 220 | if not isinstance(name,basestring): | ||
225 | 221 | raise cls.ERRORTestManager, 'collect_file takes a file position' | ||
226 | 222 | |||
227 | 223 | name=name.replace('//','/') #sanity | ||
228 | 224 | #deal with begin/end | ||
229 | 225 | if name.startswith('./'): | ||
230 | 226 | name=name[2:] | ||
231 | 227 | if not name.endswith('.py'): | ||
232 | 228 | raise cls.ERRORTestManager,'Python files should have .py extension' | ||
233 | 229 | else: | ||
234 | 230 | name=name[:-3] | ||
235 | 231 | |||
236 | 232 | if name.startswith('/'): | ||
237 | 233 | raise cls.ERRORTestManager, 'path must be relative' | ||
238 | 234 | if '..' in name: | ||
239 | 235 | raise cls.ERRORTestManager,'relative position with \'..\' is' + \ | ||
240 | 236 | ' not supported for the moment' | ||
241 | 237 | |||
242 | 238 | #replace '/' by points -> Python format | ||
243 | 239 | name=name.replace('/','.') | ||
244 | 240 | |||
245 | 241 | #position | ||
246 | 242 | return name | ||
247 | 243 | |||
248 | 244 | ########################################################################## | ||
249 | 245 | def format_possibility(self,name): | ||
250 | 246 | """ return the different string derivates from name in order to | ||
251 | 247 | scan all the different format authorizes for a restrict_to | ||
252 | 248 | format authorizes: | ||
253 | 249 | 1) full file position | ||
254 | 250 | 2) name of the file (with extension) | ||
255 | 251 | 3) full file position whithour extension | ||
256 | 252 | 4) name of the file (whithout extension) | ||
257 | 253 | 5) name of the file (but suppose name in python format) | ||
258 | 254 | 6) if name is a python file, try with a './' and with package pos | ||
259 | 255 | """ | ||
260 | 256 | |||
261 | 257 | def add_to_possibility(possibility,val): | ||
262 | 258 | """ add if not exist """ | ||
263 | 259 | if val not in possibility: | ||
264 | 260 | possibility.append(val) | ||
265 | 261 | #end local def | ||
266 | 262 | |||
267 | 263 | #print name | ||
268 | 264 | #sanity | ||
269 | 265 | if name.startswith('./'): name = name[2:] | ||
270 | 266 | name=name.replace('//','/') | ||
271 | 267 | # init with solution # | ||
272 | 268 | out=[name] | ||
273 | 269 | |||
274 | 270 | # add solution 2 | ||
275 | 271 | new_pos=name.split('/')[-1] | ||
276 | 272 | add_to_possibility(out,new_pos) | ||
277 | 273 | |||
278 | 274 | #remove extension and add solution3 and 6 | ||
279 | 275 | if name.endswith('.py'): | ||
280 | 276 | add_to_possibility(out,'./'+name) | ||
281 | 277 | add_to_possibility(out,self.package+name) | ||
282 | 278 | name = name[:-3] | ||
283 | 279 | add_to_possibility(out,name) | ||
284 | 280 | |||
285 | 281 | #add solution 4 | ||
286 | 282 | new_pos=name.split('/')[-1] | ||
287 | 283 | add_to_possibility(out,new_pos) | ||
288 | 284 | |||
289 | 285 | #add solution 5 | ||
290 | 286 | new_pos=name.split('.')[-1] | ||
291 | 287 | add_to_possibility(out,new_pos) | ||
292 | 288 | |||
293 | 289 | |||
294 | 290 | |||
295 | 291 | |||
296 | 292 | return out | ||
297 | 293 | |||
298 | 294 | ############################################################################## | ||
299 | 295 | if __name__ == "__main__": | ||
300 | 296 | |||
301 | 297 | opt=sys.argv | ||
302 | 298 | if len(opt)==1: | ||
303 | 299 | run() | ||
304 | 300 | elif len(opt)==2: | ||
305 | 301 | run(opt[1]) | ||
306 | 302 | else: | ||
307 | 303 | run(opt[1],re_opt=opt[2]) | ||
308 | 304 | |||
309 | 305 | #some example | ||
310 | 306 | # run('iolibs') | ||
311 | 307 | # run('test_test_manager.py') | ||
312 | 308 | # run('./tests/unit_tests/bin/test_test_manager.py') | ||
313 | 309 | # run('IOLibsMiscTest') | ||
314 | 310 | # run('Unittest_on_TestinModule') | ||
315 | 311 | # run('test_check_valid_on_file') | ||
316 | 312 | # run('test_collect_dir.*') # '.*' stands for all possible char (re format) | ||
317 | 313 | |||
318 | 314 | |||
319 | 0 | 315 | ||
320 | === added directory 'tests/unit_tests/bin' | |||
321 | === added file 'tests/unit_tests/bin/__init__.py' | |||
322 | === added file 'tests/unit_tests/bin/test_test_manager.py' | |||
323 | --- tests/unit_tests/bin/test_test_manager.py 1970-01-01 00:00:00 +0000 | |||
324 | +++ tests/unit_tests/bin/test_test_manager.py 2009-10-05 15:15:19 +0000 | |||
325 | @@ -0,0 +1,357 @@ | |||
326 | 1 | ############################################################################## | ||
327 | 2 | # | ||
328 | 3 | # Copyright (c) 2009 The MadGraph Development team and Contributors | ||
329 | 4 | # | ||
330 | 5 | # This file is a part of the MadGraph 5 project, an application which | ||
331 | 6 | # automatically generates Feynman diagrams and matrix elements for arbitrary | ||
332 | 7 | # high-energy processes in the Standard Model and beyond. | ||
333 | 8 | # | ||
334 | 9 | # It is subject to the MadGraph license which should accompany this | ||
335 | 10 | # distribution. | ||
336 | 11 | # | ||
337 | 12 | # For more information, please visit: http://madgraph.phys.ucl.ac.be | ||
338 | 13 | # | ||
339 | 14 | ############################################################################## | ||
340 | 15 | |||
341 | 16 | """Unit test library for the Misc routine library in the I/O package""" | ||
342 | 17 | |||
343 | 18 | import sys | ||
344 | 19 | sys.path+=['../../../bin','.','./bin'] | ||
345 | 20 | |||
346 | 21 | import unittest | ||
347 | 22 | import test_manager | ||
348 | 23 | import inspect | ||
349 | 24 | |||
350 | 25 | class Unittest_on_TestinModule(unittest.TestCase): | ||
351 | 26 | #This class test to find it's own property so it need some shortcut | ||
352 | 27 | test_path='./tests/unit_tests/bin/' | ||
353 | 28 | name='test.manager.Unittest_on_TestinModule' | ||
354 | 29 | |||
355 | 30 | def setUp(self): | ||
356 | 31 | """ basic building of the class to test """ | ||
357 | 32 | self.testmodule=test_manager.Test_in_module(package=self.test_path) | ||
358 | 33 | |||
359 | 34 | def tearDown(self): | ||
360 | 35 | pass | ||
361 | 36 | |||
362 | 37 | def test_buiding_in_iter(self): | ||
363 | 38 | """ Test_in_module.__iter__ is able to build the list """ | ||
364 | 39 | |||
365 | 40 | for i in self.testmodule: | ||
366 | 41 | break | ||
367 | 42 | self.assert_(len(self.testmodule)>1) | ||
368 | 43 | |||
369 | 44 | def test_collect_dir(self): | ||
370 | 45 | """ Test_in_module.collect_dir should detect subdirectory and file """ | ||
371 | 46 | |||
372 | 47 | self.testmodule=test_manager.Test_in_module(package= \ | ||
373 | 48 | './tests/unit_tests') | ||
374 | 49 | self.assert_('tests.unit_tests.bin.test_test_manager.Unittest_on_TestinModule.test_collect_dir' in self.testmodule) | ||
375 | 50 | |||
376 | 51 | def test_collect_dir_with_restriction_file(self): | ||
377 | 52 | """ Test_in_module.collect_dir pass corectly restriction rule on file""" | ||
378 | 53 | self.testmodule.restrict_to('test_test_manager.py') | ||
379 | 54 | # self.testmodule.collect_dir('./tests/unit_tests/bin') | ||
380 | 55 | self.assert_('tests.unit_tests.bin.test_test_manager.Unittest_on_TestinModule.test_collect_dir' in self.testmodule) | ||
381 | 56 | |||
382 | 57 | def test_collect_dir_with_restriction_file2(self): | ||
383 | 58 | """ Test_in_module.collect_dir pass corectly restriction rule on file""" | ||
384 | 59 | self.testmodule.restrict_to('./tests/unit_tests/bin/test_test_manager.py') | ||
385 | 60 | # self.testmodule.collect_dir('./tests/unit_tests/bin') | ||
386 | 61 | # print self.testmodule | ||
387 | 62 | self.assert_('tests.unit_tests.bin.test_test_manager.Unittest_on_TestinModule.test_collect_dir' in self.testmodule) | ||
388 | 63 | |||
389 | 64 | def test_collect_dir_with_restriction_class(self): | ||
390 | 65 | """ Test_in_module.collect_dir pass corectly restriction rule on class""" | ||
391 | 66 | |||
392 | 67 | self.testmodule.restrict_to('Unittest_on_TestinModule') | ||
393 | 68 | # self.testmodule.collect_dir('./tests/unit_tests/bin') | ||
394 | 69 | self.assert_('tests.unit_tests.bin.test_test_manager.Unittest_on_TestinModule.test_collect_dir' in self.testmodule) | ||
395 | 70 | |||
396 | 71 | def test_collect_dir_with_restriction_fct(self): | ||
397 | 72 | """ Test_in_module.collect_dir pass corectly restriction rule on fct""" | ||
398 | 73 | |||
399 | 74 | self.testmodule.restrict_to('test_check_valid.*') | ||
400 | 75 | self.assert_('tests.unit_tests.bin.test_test_manager.Unittest_on_TestinModule.test_collect_dir' not in self.testmodule) | ||
401 | 76 | self.assert_('tests.unit_tests.bin.test_test_manager.Unittest_on_TestinModule.test_check_valid_on_file' in self.testmodule) | ||
402 | 77 | |||
403 | 78 | |||
404 | 79 | def test_collect_file_wrong_arg(self): | ||
405 | 80 | """ Test_in_module.collect_file fails on wrong argument""" | ||
406 | 81 | |||
407 | 82 | for input in [1,{1:2},[1],str,int,list,'alpha']: | ||
408 | 83 | self.assertRaises(test_manager.Test_in_module.ERRORTestManager, \ | ||
409 | 84 | self.testmodule.collect_file, input) | ||
410 | 85 | |||
411 | 86 | def test_collect_file(self): | ||
412 | 87 | """ Test_in_module.collect_file find the different class in a file """ | ||
413 | 88 | |||
414 | 89 | self.testmodule.collect_file('./tests/unit_tests/bin/test_test_manager.py') | ||
415 | 90 | self.assert_('tests.unit_tests.bin.test_test_manager.Unittest_on_TestinModule.test_collect_file' in \ | ||
416 | 91 | self.testmodule) | ||
417 | 92 | |||
418 | 93 | def test_collect_function_wrong_arg(self): | ||
419 | 94 | """ Test_in_module.collect_function fails on wrong argument """ | ||
420 | 95 | |||
421 | 96 | for input in [1,{1:2},[1],'alpha',str,int,list]: | ||
422 | 97 | self.assertRaises(test_manager.Test_in_module.ERRORTestManager, \ | ||
423 | 98 | self.testmodule.collect_function, input) | ||
424 | 99 | |||
425 | 100 | |||
426 | 101 | def test_collect_function(self): | ||
427 | 102 | """ Test_in_module.collect_function find the test function """ | ||
428 | 103 | |||
429 | 104 | self.testmodule.collect_function(Unittest_on_TestinModule) | ||
430 | 105 | self.assert_('Unittest_on_TestinModule.test_collect_function' in \ | ||
431 | 106 | self.testmodule) | ||
432 | 107 | |||
433 | 108 | for name in self.testmodule: | ||
434 | 109 | name=name.split('.')[-1] | ||
435 | 110 | self.assertTrue(name.startswith('test')) | ||
436 | 111 | |||
437 | 112 | def test_output_are_function(self): | ||
438 | 113 | """ Test_in_module.collect_function returns test funcions only """ | ||
439 | 114 | self.testmodule.collect_function(Unittest_on_TestinModule) | ||
440 | 115 | mytest = unittest.TestSuite() | ||
441 | 116 | collect = unittest.TestLoader() | ||
442 | 117 | for test_fct in self.testmodule: | ||
443 | 118 | try: | ||
444 | 119 | collect.loadTestsFromName( \ | ||
445 | 120 | 'tests.unit_tests.bin.test_test_manager.'+test_fct) | ||
446 | 121 | except: | ||
447 | 122 | self.fail('non callable object are returned') | ||
448 | 123 | |||
449 | 124 | |||
450 | 125 | def test_restrict_to_inputcheck(self): | ||
451 | 126 | """ Test_in_module.restrict_to fail on non string/list input """ | ||
452 | 127 | |||
453 | 128 | input1={} | ||
454 | 129 | self.assertRaises(test_manager.Test_in_module.ERRORTestManager, \ | ||
455 | 130 | self.testmodule.restrict_to, input1) | ||
456 | 131 | input1=1 | ||
457 | 132 | self.assertRaises(test_manager.Test_in_module.ERRORTestManager, \ | ||
458 | 133 | self.testmodule.restrict_to, input1) | ||
459 | 134 | import re | ||
460 | 135 | |||
461 | 136 | input1=re.compile('''1''') | ||
462 | 137 | self.assertRaises(test_manager.Test_in_module.ERRORTestManager, \ | ||
463 | 138 | self.testmodule.restrict_to, input1) | ||
464 | 139 | |||
465 | 140 | def test_check_valid_wrong_arg(self): | ||
466 | 141 | """ Test_in_module.check_valid raise error if not str in input """ | ||
467 | 142 | |||
468 | 143 | for input in [1,{1:2},[1]]: | ||
469 | 144 | self.assertRaises(test_manager.Test_in_module.ERRORTestManager, \ | ||
470 | 145 | self.testmodule.check_valid, input) | ||
471 | 146 | |||
472 | 147 | def test_check_valid_on_module(self): | ||
473 | 148 | """ Test_in_module.check_valid recognizes module """ | ||
474 | 149 | |||
475 | 150 | expression = 'bin' | ||
476 | 151 | self.testmodule.restrict_to(expression) | ||
477 | 152 | pos = './bin' | ||
478 | 153 | self.assertTrue(self.testmodule.check_valid(pos)) | ||
479 | 154 | pos = './bin2' | ||
480 | 155 | self.assertFalse(self.testmodule.check_valid(pos)) | ||
481 | 156 | pos = 'bin' | ||
482 | 157 | self.assertTrue(self.testmodule.check_valid(pos)) | ||
483 | 158 | pos = '../test/bin' | ||
484 | 159 | self.assertTrue(self.testmodule.check_valid(pos)) | ||
485 | 160 | |||
486 | 161 | def test_check_valid_on_file(self): | ||
487 | 162 | """ Test_in_module.check_valid recognizes file """ | ||
488 | 163 | |||
489 | 164 | expression = 'test' | ||
490 | 165 | self.testmodule.restrict_to(expression) | ||
491 | 166 | pos = 'test.py' | ||
492 | 167 | self.assertTrue(self.testmodule.check_valid(pos)) | ||
493 | 168 | pos = './test.py' | ||
494 | 169 | self.assertTrue(self.testmodule.check_valid(pos)) | ||
495 | 170 | pos = '../test/test.py' | ||
496 | 171 | self.assertTrue(self.testmodule.check_valid(pos)) | ||
497 | 172 | pos = 'test.pyc' | ||
498 | 173 | self.assertFalse(self.testmodule.check_valid(pos)) | ||
499 | 174 | pos = 'onetest.py' | ||
500 | 175 | self.assertFalse(self.testmodule.check_valid(pos)) | ||
501 | 176 | pos = 'test/file.py' | ||
502 | 177 | self.assertFalse(self.testmodule.check_valid(pos)) | ||
503 | 178 | |||
504 | 179 | expression = 'test.py' | ||
505 | 180 | self.testmodule.restrict_to(expression) | ||
506 | 181 | pos = 'test.py' | ||
507 | 182 | self.assertTrue(self.testmodule.check_valid(pos)) | ||
508 | 183 | pos = './test.py' | ||
509 | 184 | self.assertTrue(self.testmodule.check_valid(pos)) | ||
510 | 185 | pos = '../test/test.py' | ||
511 | 186 | self.assertTrue(self.testmodule.check_valid(pos)) | ||
512 | 187 | pos = 'test.pyc' | ||
513 | 188 | self.assertFalse(self.testmodule.check_valid(pos)) | ||
514 | 189 | pos = 'onetest.py' | ||
515 | 190 | self.assertFalse(self.testmodule.check_valid(pos)) | ||
516 | 191 | pos = 'test/file.py' | ||
517 | 192 | self.assertFalse(self.testmodule.check_valid(pos)) | ||
518 | 193 | |||
519 | 194 | expression = 'test.test.py' | ||
520 | 195 | self.testmodule.restrict_to(expression) | ||
521 | 196 | pos = 'test/test.py' | ||
522 | 197 | self.assertTrue(self.testmodule.check_valid(pos)) | ||
523 | 198 | pos = './test.py' | ||
524 | 199 | self.assertFalse(self.testmodule.check_valid(pos)) | ||
525 | 200 | pos = 'bin/test/test.py' | ||
526 | 201 | self.assertFalse(self.testmodule.check_valid(pos)) | ||
527 | 202 | pos = 'test.pyc' | ||
528 | 203 | self.assertFalse(self.testmodule.check_valid(pos)) | ||
529 | 204 | pos = 'onetest.py' | ||
530 | 205 | self.assertFalse(self.testmodule.check_valid(pos)) | ||
531 | 206 | pos = 'test/file.py' | ||
532 | 207 | self.assertFalse(self.testmodule.check_valid(pos)) | ||
533 | 208 | |||
534 | 209 | expression = './test/test.py' | ||
535 | 210 | self.testmodule.restrict_to(expression) | ||
536 | 211 | pos = 'test/test.py' | ||
537 | 212 | self.assertTrue(self.testmodule.check_valid(pos)) | ||
538 | 213 | pos = './test.py' | ||
539 | 214 | self.assertFalse(self.testmodule.check_valid(pos)) | ||
540 | 215 | pos = 'bin/test/test.py' | ||
541 | 216 | self.assertFalse(self.testmodule.check_valid(pos)) | ||
542 | 217 | pos = 'test.pyc' | ||
543 | 218 | self.assertFalse(self.testmodule.check_valid(pos)) | ||
544 | 219 | pos = 'onetest.py' | ||
545 | 220 | self.assertFalse(self.testmodule.check_valid(pos)) | ||
546 | 221 | pos = 'test/file.py' | ||
547 | 222 | self.assertFalse(self.testmodule.check_valid(pos)) | ||
548 | 223 | |||
549 | 224 | |||
550 | 225 | def test_check_valid_on_class(self): | ||
551 | 226 | """ Test_in_module.check_valid recognizes class of test """ | ||
552 | 227 | |||
553 | 228 | expression = 'Unittest_on_TestinModule' | ||
554 | 229 | self.testmodule.restrict_to(expression) | ||
555 | 230 | name = 'Unittest_on_TestinModule' | ||
556 | 231 | self.assertTrue(self.testmodule.check_valid(name)) | ||
557 | 232 | name = 'test.Unittest_on_TestinModule' | ||
558 | 233 | self.assertTrue(self.testmodule.check_valid(name)) | ||
559 | 234 | name = 'I.am.Your.Father' | ||
560 | 235 | self.assertFalse(self.testmodule.check_valid(name)) | ||
561 | 236 | |||
562 | 237 | def test_check_valid_on_function(self): | ||
563 | 238 | """ Test_in_module.check_valid recognizes functions """ | ||
564 | 239 | |||
565 | 240 | expression='test_search' | ||
566 | 241 | self.testmodule.restrict_to(expression) | ||
567 | 242 | name='test_search' | ||
568 | 243 | self.assertTrue(self.testmodule.check_valid(name)) | ||
569 | 244 | name='test.test_search' | ||
570 | 245 | self.assertTrue(self.testmodule.check_valid(name)) | ||
571 | 246 | name='It.is.impossible' | ||
572 | 247 | self.assertFalse(self.testmodule.check_valid(name)) | ||
573 | 248 | |||
574 | 249 | expression='test_check_valid.*' | ||
575 | 250 | self.testmodule.restrict_to(expression) | ||
576 | 251 | name='module.test_check_valid_on_function' | ||
577 | 252 | self.assertTrue(self.testmodule.check_valid(name)) | ||
578 | 253 | |||
579 | 254 | def test_check_valid_with_re(self): | ||
580 | 255 | """ Test_in_module.check_valid should work with re """ | ||
581 | 256 | |||
582 | 257 | expression = 'test.*' | ||
583 | 258 | self.testmodule.restrict_to(expression,) | ||
584 | 259 | name = 'test_search' | ||
585 | 260 | self.assertTrue(self.testmodule.check_valid(name)) | ||
586 | 261 | name = 'valid.test_search' | ||
587 | 262 | self.assertTrue(self.testmodule.check_valid(name)) | ||
588 | 263 | name = 'one_test' | ||
589 | 264 | self.assertFalse(self.testmodule.check_valid(name)) | ||
590 | 265 | |||
591 | 266 | expression = 'test' | ||
592 | 267 | import re | ||
593 | 268 | re_opt = re.I | ||
594 | 269 | self.testmodule.restrict_to(expression,re_opt) | ||
595 | 270 | name = 'test' | ||
596 | 271 | self.assertTrue(self.testmodule.check_valid(name)) | ||
597 | 272 | name = 'TEST' | ||
598 | 273 | self.assertTrue(self.testmodule.check_valid(name)) | ||
599 | 274 | name = 'one_test' | ||
600 | 275 | self.assertFalse(self.testmodule.check_valid(name)) | ||
601 | 276 | |||
602 | 277 | def test_check_valid_with_list_restriction(self): | ||
603 | 278 | """ Test_in_module.check_valid should work with list in restrict """ | ||
604 | 279 | |||
605 | 280 | expression = ['test.*','iolibs'] | ||
606 | 281 | self.testmodule.restrict_to(expression) | ||
607 | 282 | name = 'test_search' | ||
608 | 283 | self.assertTrue(self.testmodule.check_valid(name)) | ||
609 | 284 | name = 'iolibs' | ||
610 | 285 | self.assertTrue(self.testmodule.check_valid(name)) | ||
611 | 286 | name = 'data' | ||
612 | 287 | self.assertFalse(self.testmodule.check_valid(name)) | ||
613 | 288 | |||
614 | 289 | def test_status_file_on_file(self): | ||
615 | 290 | """ Test_in_module.status_file recognizes file """ | ||
616 | 291 | |||
617 | 292 | status=self.testmodule.status_file(self.test_path+\ | ||
618 | 293 | 'test_test_manager.py') | ||
619 | 294 | self.assertEqual(status,'file') | ||
620 | 295 | status=self.testmodule.status_file(self.test_path+\ | ||
621 | 296 | '../../../README') | ||
622 | 297 | self.assertFalse(status) | ||
623 | 298 | status=self.testmodule.status_file(self.test_path+\ | ||
624 | 299 | '__init__.py') | ||
625 | 300 | self.assertFalse(status) | ||
626 | 301 | status=self.testmodule.status_file(self.test_path+\ | ||
627 | 302 | '__init__.pyc') | ||
628 | 303 | self.assertFalse(status) | ||
629 | 304 | |||
630 | 305 | |||
631 | 306 | def test_status_file_on_dir(self): | ||
632 | 307 | """ Test_in_module.status_file doesn't detect non module dir """ | ||
633 | 308 | |||
634 | 309 | status=self.testmodule.status_file(self.test_path+\ | ||
635 | 310 | '../bin') | ||
636 | 311 | self.assertEqual(status,'module') | ||
637 | 312 | |||
638 | 313 | status=self.testmodule.status_file(self.test_path+\ | ||
639 | 314 | '../../../apidoc') | ||
640 | 315 | self.assertFalse(status) | ||
641 | 316 | |||
642 | 317 | |||
643 | 318 | def test_passin_pyformat(self): | ||
644 | 319 | """ convert from linux position format to python include """ | ||
645 | 320 | |||
646 | 321 | input={'test.py':'test', 'Source/test.py':'Source.test', \ | ||
647 | 322 | 'Source//test.py':'Source.test', './test.py':'test'} | ||
648 | 323 | for key,value in input.items(): | ||
649 | 324 | self.assertEqual( \ | ||
650 | 325 | self.testmodule.passin_pyformat(key), | ||
651 | 326 | value) | ||
652 | 327 | |||
653 | 328 | input=['test','../Source.py','/home/data.py',1,str] | ||
654 | 329 | for data in input: | ||
655 | 330 | self.assertRaises(test_manager.Test_in_module.ERRORTestManager | ||
656 | 331 | ,self.testmodule.passin_pyformat, data) | ||
657 | 332 | |||
658 | 333 | |||
659 | 334 | def test_add_to_possibility(self): | ||
660 | 335 | """ convert name in different matching possibility """ | ||
661 | 336 | # the sanity of the output is checked by check_valid test, | ||
662 | 337 | # this test the format of the output | ||
663 | 338 | |||
664 | 339 | output=self.testmodule.format_possibility('./bin/test.py') | ||
665 | 340 | for name in output: | ||
666 | 341 | self.assertEqual(output.count(name),1) | ||
667 | 342 | self.assert_(len(output)>3) | ||
668 | 343 | |||
669 | 344 | output=self.testmodule.format_possibility('bin.test') | ||
670 | 345 | for name in output: | ||
671 | 346 | self.assertEqual(output.count(name),1) | ||
672 | 347 | self.assert_(len(output)>1) | ||
673 | 348 | |||
674 | 349 | if __name__ == "__main__": | ||
675 | 350 | mytest = unittest.TestSuite() | ||
676 | 351 | suite = unittest.TestLoader() | ||
677 | 352 | suite=suite.loadTestsFromTestCase(Unittest_on_TestinModule) | ||
678 | 353 | mytest.addTest(suite) | ||
679 | 354 | unittest.TextTestRunner(verbosity=2).run(mytest) | ||
680 | 355 | |||
681 | 356 | |||
682 | 357 | # unittest.main() |
Add the test manager to launch test automaticaly