Skip to content

Commit 180080d

Browse files
author
Kevin Chen
committed
Upgraded the testing further to avoid manually updates
I changed some things in test.py and the individual tests. In test.py, in no circumstance do you need to add the test modules manually. It finds them itself based on their name. I made some of the tests more unittest-oriented as well but a lot still to be done on that. bbelson@rainier.princeton.edu
1 parent 8339dea commit 180080d

5 files changed

Lines changed: 107 additions & 27 deletions

File tree

src/TestConvert.py

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ def setUp(self):
2525
"""Set up testing parameters."""
2626

2727
# Number of times to run each of the randomized tests.
28-
self.numTests = 10
28+
self.numTests = 10 #almost guarantees failure
2929
# Maximum number of states to test + 1
30-
self.maxStates = 3
30+
self.maxStates = 20
3131
# Maximum number of inputs and outputs to test + 1
32-
self.maxIO = 3
32+
self.maxIO = 20
3333
# Set to True to print systems to the output.
34-
self.debug = True
34+
self.debug = False
3535

3636
def printSys(self, sys, ind):
3737
"""Print system to the standard output."""
@@ -42,23 +42,47 @@ def printSys(self, sys, ind):
4242

4343
def testConvert(self):
4444
"""Test state space to transfer function conversion."""
45+
#Currently it only tests that a TF->SS->TF generates an unchanged TF
4546

46-
print __doc__
47+
#print __doc__
4748

4849
for states in range(1, self.maxStates):
4950
for inputs in range(1, self.maxIO):
5051
for outputs in range(1, self.maxIO):
51-
sys1 = matlab.rss(states, inputs, outputs)
52-
self.printSys(sys1, 1)
53-
54-
sys2 = matlab.tf(sys1)
55-
self.printSys(sys2, 2)
56-
57-
sys3 = matlab.ss(sys2)
58-
self.printSys(sys3, 3)
59-
60-
sys4 = matlab.tf(sys3)
61-
self.printSys(sys4, 4)
52+
#start with a random SS system and transform to TF
53+
#then back to SS, check that the matrices are the same.
54+
ssOriginal = matlab.rss(states, inputs, outputs)
55+
self.printSys(ssOriginal, 1)
56+
57+
tfOriginal = matlab.tf(ssOriginal)
58+
self.printSys(tfOriginal, 2)
59+
60+
ssTransformed = matlab.ss(tfOriginal)
61+
self.printSys(ssTransformed, 3)
62+
63+
tfTransformed = matlab.tf(ssTransformed)
64+
self.printSys(tfTransformed, 4)
65+
66+
for inputNum in range(inputs):
67+
for outputNum in range(outputs):
68+
np.testing.assert_array_almost_equal(\
69+
tfOriginal.num[outputNum][inputNum],\
70+
tfTransformed.num[outputNum][inputNum])
71+
72+
np.testing.assert_array_almost_equal(\
73+
tfOriginal.den[outputNum][inputNum],\
74+
tfTransformed.den[outputNum][inputNum])
75+
76+
#To test the ss systems is harder because they aren't the same
77+
#realization. This could be done with checking that they have the
78+
#same freq response with bode, but apparently it doesn't work
79+
#the way it should right now:
80+
## Bode should work like this:
81+
#[mag,phase,freq]=bode(sys)
82+
#it doesn't seem to......
83+
#This should be added.
84+
85+
6286

6387
def suite():
6488
return unittest.TestLoader().loadTestsFromTestCase(TestConvert)

src/TestFreqRsp.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,5 +56,8 @@
5656
#plt.figure(4)
5757
#bode(sysMIMO,omega)
5858

59+
5960
def suite():
60-
return unittest.TestLoader().loadTestsFromTestCase(TestConvert)
61+
pass
62+
#Uncomment this once it is a real unittest
63+
#return unittest.TestLoader().loadTestsFromTestCase(TestFreqRsp)

src/TestModelsimp.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ def testBalredTruncate(self):
8888
np.testing.assert_array_almost_equal(rsys.C, Crtrue,decimal=4)
8989
np.testing.assert_array_almost_equal(rsys.D, Drtrue,decimal=4)
9090

91+
def suite():
92+
return unittest.TestLoader().loadTestsFromTestCase(TestModelsimp)
9193

9294

9395
if __name__ == '__main__':

src/TestStatefbk.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,5 +80,9 @@ def testGramsys(self):
8080
self.assertRaises(ValueError, gram, sys, 'o')
8181
self.assertRaises(ValueError, gram, sys, 'c')
8282

83+
def suite():
84+
return unittest.TestLoader().loadTestsFromTestCase(TestStatefbk)
85+
86+
8387
if __name__ == '__main__':
8488
unittest.main()

src/test.py

Lines changed: 57 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
2059
def 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

62109
if __name__=='__main__':
63110
tests()

0 commit comments

Comments
 (0)