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