B
Bruno Desthuilliers
David N Montgomery a écrit :
def __init__(self, tc):
func = getattr(self, "testCase%s" % tc, None)
if callable(func):
func()
for x in range(totalNumberOfTestCases):
testCase(x)
At least learn the use of the 'elif' statement... But in this case, you
just don't need it.
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 ?
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 ?