472,096 Members | 1,134 Online

# How to optimise this code?

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.

Aug 21 '07 #1
8 1074
On Aug 21, 10:59 am, "David N Montgomery" <monty.pyt...@fastmail.fm>
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.

You're code doesn't make sense to me. You create a class and then you
call a method within the class without instantiating said class. What
the!?

Can't you just create a function?

<code>
def testCase(x):
print "tc%s" % x

totalNumberOfTestCases = 6
x = 0
while x <= totalNumberOfTestCases:
x += 1
testCase(x)

</code>

Am I missing something?

Mike

Aug 21 '07 #2
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.
Have a look at the unittest module in the standard library

http://docs.python.org/lib/module-unittest.html

It can do the bookkeeping for you:
>>import unittest
class TestCase(unittest.TestCase):
.... def testCase1(self):
.... print "first"
.... def testCaseN(self):
.... print "last"
....
>>unittest.main()
first
..last
..
----------------------------------------------------------------------
Ran 2 tests in 0.001s

OK

Peter
Aug 21 '07 #3
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.

To get rid of the if statements, replace __init__ function with:

def __init__(self, tc):
functionToCall = eval("self.testCase%s" % tc)
functionToCall()

HTH,

Chris
Aug 21 '07 #4
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.

To get rid of the if statements, replace __init__ function with:

def __init__(self, tc):
functionToCall = eval("self.testCase%s" % tc)
functionToCall()

HTH,

Chris

Aug 21 '07 #5
Christof Winter <wi****@biotec.tu-dresden.dewrites:
To get rid of the if statements, replace __init__ function with:

def __init__(self, tc):
functionToCall = eval("self.testCase%s" % tc)
Or functionToCall = getattr(self, "testCase" + tc)

eval can introduce unwanted side effects.
Aug 21 '07 #6
On Tue, 21 Aug 2007 21:56:18 +0200, Hrvoje Niksic <hn*****@xemacs.org>
wrote:
>Christof Winter <wi****@biotec.tu-dresden.dewrites:
>To get rid of the if statements, replace __init__ function with:

def __init__(self, tc):
functionToCall = eval("self.testCase%s" % tc)

Or functionToCall = getattr(self, "testCase" + tc)

eval can introduce unwanted side effects.
Hence the slogan "Do No Eval!"

wwwayne
Aug 21 '07 #7
On Aug 22, 4:52 am, "David N Montgomery" <monty.pyt...@fastmail.fm>
wrote:
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.
using unittest?

Hyuga

Aug 22 '07 #8
David N Montgomery a écrit :
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 __init__(self, tc):
func = getattr(self, "testCase%s" % tc, None)
if callable(func):
func()

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)
for x in range(totalNumberOfTestCases):
testCase(x)
>
This template code is working, but I envisage having 100+ test cases and
am concerned about my useage of if statements.
At least learn the use of the 'elif' statement... But in this case, you
just don't need it.
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.
for name in dir(testCase):
if name.startswith('testCase'):
tc = getattr(testCase(), name)
if callable(tc):
tc()

But all this code smells IMHO (starting with your __init__ method which
is not an initializer...)

There's no shortage of unit-test packages in Python:
- unittest (in the standard lib)
- py.test (http://codespeak.net/py/dist/test.html)

Do you have any compelling reason to reinvent the wheel ?
Aug 23 '07 #9

### This discussion thread is closed

Replies have been disabled for this discussion.