Unit testing is testing individual "unit" or function of a program. Here we use TDD test driven development that is first write the test case then write the code . In python unit test is possible by "unittest" module .
"isodd.py" is a program to convert any number to odd:
"isodd.py" is a program to convert any number to odd:
class IsoddError(Exception): pass
class TypeError(IsoddError):pass
def isOdd(n):
if type(n)==int :
if n%2 != 0 :
return n
else:
return n+1
else:
raise TypeError,"Invalid"
How to write test cases :
- First write known result with known output
- Check the above cases
- next check failure of isOdd
class TestData(unittest.TestCase):
TestData=( (0,1),
(1,1),
(-1,-1),
(-2,-1),
(8,9),
(10,11),
(55,55),
(100,101) )
def testIsOddTestData(self):
"""isOdd give known result with known output"""
for integer,fn_value in self.TestData:
result=isodd.isOdd(integer)
self.assertEqual(fn_value, result)
class IsOddBadInput(unittest.TestCase):
def testIsOddInput(self):
"""isOdd fails for bad input"""
self.assertRaises(isodd.TypeError, isodd.isOdd, 'hello')
To see the source code click here
unittest is wonderfull... man tell me your contact info....
ReplyDelete