4 from optparse import OptionParser
8 testMatch = sre.compile(r'^[Tt]est')
10 class TestSuite(unittest.TestSuite):
11 loader = unittest.defaultTestLoader
13 def __init__(self, names=[]):
15 super(TestSuite,self).__init__()
16 tests=self.collectTests()
19 def _import(self,name):
20 mod = __import__(name,{},{})
21 components = name.split('.')
22 for comp in components[1:]:
23 mod = getattr(mod,comp)
26 def collectTests(self):
30 entries = [ item for item in os.listdir(os.getcwd())
31 if item.lower().find('test') >= 0 ]
35 path=os.path.abspath(os.path.join(os.getcwd(),item))
36 if os.path.isfile(item):
37 root, ext = os.path.splitext(item)
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)
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)
53 tests.append(TestPackageSuite(package))
55 tests.append(TestDirectorySuite(path))
58 class TestDirectorySuite(TestSuite):
60 def __init__(self,path):
62 super(TestDirectorySuite,self).__init__()
64 def collectTests(self):
67 sys.path.insert(0,self.path)
68 entries = os.listdir(self.path)
72 or item in self.ignore
73 or not testMatch.search(item)):
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)
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)
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)
94 tests.append(TestPackageSuite(package))
96 tests.append(TestDirectorySuite(item_path))
97 sys.path.remove(self.path)
100 class TestPackageSuite(TestDirectorySuite):
101 def __init__(self,package):
103 path=os.path.abspath(os.path.dirname(self.package.__file__))
104 super(TestPackageSuite,self).__init__(path)
106 def collectTests(self):
109 sys.path.insert(0,self.path)
110 entries = os.listdir(self.path)
114 or item in self.ignore
115 or not testMatch.search(item)):
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)
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)
131 tests.append(TestPackageSuite(package))
133 tests.append(TestDirectorySuite(item_path))
134 sys.path.remove(self.path)
138 class ModuleTestSuite(TestSuite):
140 def __init__(self, module):
142 super(ModuleTestSuite,self).__init__()
144 def collectTests(self):
146 a_ln = a.func_code.co_firstlineno
147 b_ln = b.func_code.co_firstlineno
148 return cmp(a_ln,b_ln)
150 entries = dir(self.module)
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)
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 ]
170 class TestProgram(unittest.TestProgram):
173 def __init__(self,testRunner=None):
174 self.testRunner = testRunner
176 self.parseArgs(sys.argv)
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. ")
186 options, args = parser.parse_args(argv)
187 self.verbosity = options.verbosity
190 self.names = list(args)
191 if self.names[0] == 'run.py':
192 self.names = self.names[1:]
194 def createTests(self):
195 self.test = TestSuite(self.names)
200 if __name__ == "__main__":