How to optimise this code?

2007-08-21 Thread David N Montgomery
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.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to optimise this code?

2007-08-22 Thread David N Montgomery
On Wed, 22 Aug 2007 02:39:45 -, "Basilisk96" <[EMAIL PROTECTED]>
said:
> David,
> 
> If your true design intent is to run X number of test cases, unittest
> is the biggest bang for your buck, like shown by Peter's example. You
> just subclass unittest.TestCase, and def your test methods in the
> class body; they will simply be executed in the order you list them.
> It's just nice how it works that way.
> 
> Otherwise, the function factory approach like Hrvoje's
>functionToCall = getattr(self, "testCase%s" % tc)
> is the best optimization.
> 
> Cheers,
> -Basilisk96
> 
> -- 
> http://mail.python.org/mailman/listinfo/python-list


Thank you all - the assistance is much appreciated.

unittest is the best choice for my needs and works perfectly in Eclipse.
Unfortunately though it (and many other things) does not work under the
application we have to use to run our python scripts.

This leaves me with 'functionToCall = getattr(self, "testCase%s" % tc)'.
This achieves the optimisation/simplification I had been looking for.

Thank you once again.
-- 
http://mail.python.org/mailman/listinfo/python-list