SyFi
0.3
|
00001 #!/usr/bin/env python 00002 """Run all tests""" 00003 00004 # Modified from test.py in UFL 00005 00006 __author__ = "Martin Alnes (martinal@simula.no) and Anders Logg (logg@simula.no)" 00007 __date__ = "2008-03-12 -- 2011-04-08" 00008 00009 import glob, os, sys, unittest 00010 00011 def discover_packages(): 00012 # Looking at tests in all test_foo/ packages 00013 packages = sorted(f for f in glob.glob("test_*") 00014 if os.path.exists(os.path.join(f,'__init__.py'))) 00015 return packages 00016 00017 def discover_tests(p): 00018 import glob, os 00019 # Running tests from all test_foo.py files in each package 00020 tests = sorted(os.path.basename(f.replace(".py", "")) 00021 for f in glob.glob(os.path.join(p,"test_*.py"))) 00022 return tests 00023 00024 def run_suite(package, tests): 00025 assert tests 00026 loader = unittest.TestLoader() 00027 p = __import__(package, fromlist=tests) 00028 modules = [getattr(p,test) for test in tests] 00029 suite = loader.loadTestsFromModule(modules[0]) 00030 for m in modules[1:]: 00031 suite.addTests(loader.loadTestsFromModule(m)) 00032 runner = unittest.TextTestRunner(verbosity=2) 00033 return runner.run(suite) 00034 00035 def main(args): 00036 s, f = 0, 0 00037 cwd = os.path.abspath(os.curdir) 00038 00039 tfilter = [] 00040 for a in args: 00041 if a.startswith('test_'): 00042 tfilter.append(a) 00043 else: 00044 print "Ignoring argument", a 00045 if tfilter: 00046 print "Running only tests", tfilter 00047 00048 packages = discover_packages() 00049 for p in packages: 00050 tests = discover_tests(p) 00051 if tfilter: 00052 # Use only tests found in args 00053 tests = [t for t in tests if t in tfilter] 00054 if not tests: 00055 continue 00056 result = run_suite(p, tests) 00057 os.chdir(cwd) # In case tests mess up current directory... 00058 if result.wasSuccessful(): 00059 s += 1 00060 else: 00061 f += 1 00062 00063 if not f: 00064 print "All tests finished successfully." 00065 return 0 00066 else: 00067 print "Not all tests finished successfully." 00068 return 1 00069 00070 if __name__ == "__main__": 00071 sys.exit(main(sys.argv[1:]))