How to optimise this code?
Christof Winter
winter at biotec.tu-dresden.de
Tue Aug 21 15:00:05 EDT 2007
David N Montgomery wrote:
> class testCase:
> def __init__(self, tc):
> if tc == 1:self.testCase1()
> if tc == 2:self.testCase2()
> if tc == 3:self.testCase3()
> if tc == 4:self.testCase4()
> if tc == 5:self.testCase5()
> if tc == 6:self.testCase6()
>
> def testCase1(self):
> print "tc1"
>
> def testCase2(self):
> print "tc2"
>
> def testCase3(self):
> print "tc3"
>
> def testCase4(self):
> print "tc4"
>
> def testCase5(self):
> print "tc5"
>
> def testCase6(self):
> print "tc6"
>
>
> def testCaseX(self):
> print "tcX"
>
> totalNumberOfTestCases = 6
> x = 0
> while x <= totalNumberOfTestCases:
> x += 1
> testCase(x)
>
>
> This template code is working, but I envisage having 100+ test cases and
> am concerned about my useage of if statements. I would be grateful for
> any pointers as to how I can run all tests cases, regardless of how
> many, in a more efficient manner.
>
> Thank you in advance.
To get rid of the if statements, replace __init__ function with:
def __init__(self, tc):
functionToCall = eval("self.testCase%s" % tc)
functionToCall()
HTH,
Chris
More information about the Python-list
mailing list