@@ -17,6 +17,45 @@ def tests_old():
1717 print SP .Popen (['./' + test ],stdout = SP .PIPE ).communicate ()[0 ]
1818 print 'Completed' ,test
1919
20+
21+ ###############################################################################
22+
23+ def getFileList (dir ,fileExtension = '' ):
24+ """ Finds all files in the given directory that have the given file extension"""
25+ filesRaw = SP .Popen (['ls' ,dir ],stdout = SP .PIPE ).communicate ()[0 ]
26+ #files separated by endlines
27+ filename = ''
28+ fileList = []
29+ #print 'filesRaw is ',filesRaw
30+ for c in filesRaw :
31+ if c != '\n ' :
32+ filename += c
33+ else : #completed file name
34+ if fileExtension != '' and filename [- len (fileExtension ):] == fileExtension :
35+ fileList .append (filename )
36+ else :
37+ pass #fileList.append(dir+filename)
38+ filename = ''
39+ return fileList
40+
41+ ###############################################################################
42+
43+
44+ def findTests (testdir = './' ):
45+ """Since python <2.7 doesn't have test discovery, this finds tests in the
46+ provided directory. The default is to check the current directory. Any files
47+ that match test* or Test* are considered unittest modules and checked for
48+ a module.suite() function (in tests())."""
49+ fileList = getFileList (testdir ,fileExtension = '.py' )
50+ testModules = []
51+ for fileName in fileList :
52+ if (fileName [:4 ] == 'test' or fileName [:4 ]== 'Test' ) and fileName != 'test.py' :
53+ testModules .append (fileName [:- len ('.py' )])
54+ return testModules
55+
56+ ###############################################################################
57+
58+
2059def tests ():
2160 import unittest
2261 try : #auto test discovery is only implemented in python 2.7+
@@ -43,21 +82,29 @@ def tests():
4382
4483 print 'Tests may be incomplete'
4584 t = unittest .TextTestRunner ()
46-
47- testModules = [ 'TestBDAlg' , 'TestConvert' , 'TestFreqRsp' , 'TestMatlab' , 'TestModelsimp' ,\
48- 'TestStateSp' , 'TestStatefbk' , 'TestXferFcn' ] #add additional tests here, or discovery?
85+
86+ testModules = findTests ()
87+
4988 suite = unittest .TestSuite ()
89+ suiteList = []
5090 for mod in testModules :
5191 exec ('import ' + mod + ' as currentModule' )
52- print 'TEST' ,mod
53- suite = currentModule .suite ()
54- t .run (suite )
5592 #After tests have been debugged and made into unittests, remove
5693 #the above (except the import) and replace with something like this:
57- #suiteList.append(ts.suite())
58- #alltests = unittest.TestSuite(suiteList)
59- #t.run(alltests)
60-
94+ try :
95+ currentSuite = currentModule .suite ()
96+ if isinstance (currentSuite ,unittest .TestSuite ):
97+ suiteList .append (currentModule .suite ())
98+ else :
99+ print mod + '.suite() doesnt return a unittest.TestSuite!, please fix!'
100+ except :
101+ print 'The test module ' + mod + ' doesnt have ' + \
102+ 'a proper suite() function that returns a unittest.TestSuite object' + \
103+ ' Please fix this!'
104+ alltests = unittest .TestSuite (suiteList )
105+ t .run (unittest .TestSuite (alltests ))
106+
107+ ###############################################################################
61108
62109if __name__ == '__main__' :
63110 tests ()
0 commit comments