How to optimise this code?

  • Thread starter Bruno Desthuilliers
  • Start date
B

Bruno Desthuilliers

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)
- nose (http://www.somethingaboutorange.com/mrl/projects/nose/)

Do you have any compelling reason to reinvent the wheel ?
 
D

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.
 
K

kyosohma

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.

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
 
P

Peter Otten

David said:
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:
.... def testCase1(self):
.... print "first"
.... def testCaseN(self):
.... print "last"
....first
..last
..
----------------------------------------------------------------------
Ran 2 tests in 0.001s

OK

Peter
 
C

Christof Winter

David said:
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
 
C

Christof Winter

David said:
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
 
H

Hrvoje Niksic

Christof Winter said:
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.
 
B

Basilisk96

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
 
D

David N Montgomery

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


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.
 
H

Hyuga

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.

Just out of curiosity, what about your environment prevents you from
using unittest?

Hyuga
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top