Unit testing may be define as testing each units of a program.Unit testing in python has a separate testing python program for original program.
Let us explain with an example:
i want to create a program which accepts a number and check whether odd or even.
In python unit testing testing,we must assume the original program to predict requirements,inputs,outputs,errors etc..
Let us assume original program is oddeven.py and test program is testoddeven.py.
First we must know the requirements,inputs,outputs,errors..
Input may be int,string,float etc..
So errors may arise when input is string,float,zero.
So lets start our testoddeven.py
import oddeven #our original program
import unittest #python inbuilt module
class KnownValues(unittest.TestCase):
knownValues=((1,'odd'),(2,'even'),(11,'odd'))
Here some input and expected output is defined in a class
import oddeven #our original program
import unittest #python inbuilt module
class KnownValues(unittest.TestCase):
knownValues=((1,'odd'),(2,'even'),(11,'odd'))
def testoddeven(self):
for i,j in self.knownValues:
result=oddeven.testoe(i)
self.assertEqual(j,result)
Here checking whether the output equals to output of our oddeven.py Now we have to design errors or assume errors.Here possible errors are
zero,float value,string.Now i am defining expected errors.
import oddeven
import unittest
class KnownValues(unittest.TestCase):
knownValues=((1,'odd'),(2,'even'),(11,'odd'))
def testoddeven(self):
for i,j in self.knownValues:
result=oddeven.testoe(i)
self.assertEqual(j,result)
class OddEvenBadInput(unittest.TestCase):
def testZero(self):
self.assertRaises(oddeven.ZeroError, oddeven.testoe,0)
def testNonInteger(self):
self.assertRaises(oddeven.NonIntegerError, oddeven.testoe,6.5)
def testString(self):
self.assertRaises(oddeven.StringError, oddeven.testoe,"abc")
if __name__ == "__main__":
unittest.main()
Then start coding oddeven.py Then define the errors in original program sa below.
class ZeroError(Exception):pass
class NonIntegerError(Exception):pass
class StringError(Exception):pass
Then define each line and test each time using python testoddeven.py Rewrite the program until 'ok' gets...
class ZeroError(Exception):pass
class NonIntegerError(Exception):pass
class StringError(Exception):pass
def testoe(n):
if type(n)==int:
if n==0:raise ZeroError,"zero..."
elif n%2==0:
return "even"
else:
return "odd"
elif type(n)==str:
raise StringError,"string...!!!!"
else: raise NonIntegerError,"Non int...."
unit testing is a method by which individual units of source code, sets of one or more computer program modules together with associated control data, usage procedures, and operating procedures are tested to determine if they are fit for use. Unit tests are created by programmers or occasionally by white box testers during the development process.One can view a unit as the smallest testable part of an application.Unit tests are typically written and run by software developers to ensure that code meets its design and behaves as intended.
To view the codes.Click here
Let us explain with an example:
i want to create a program which accepts a number and check whether odd or even.
In python unit testing testing,we must assume the original program to predict requirements,inputs,outputs,errors etc..
Let us assume original program is oddeven.py and test program is testoddeven.py.
First we must know the requirements,inputs,outputs,errors..
Input may be int,string,float etc..
So errors may arise when input is string,float,zero.
So lets start our testoddeven.py
import oddeven #our original program
import unittest #python inbuilt module
class KnownValues(unittest.TestCase):
knownValues=((1,'odd'),(2,'even'),(11,'odd'))
Here some input and expected output is defined in a class
import oddeven #our original program
import unittest #python inbuilt module
class KnownValues(unittest.TestCase):
knownValues=((1,'odd'),(2,'even'),(11,'odd'))
def testoddeven(self):
for i,j in self.knownValues:
result=oddeven.testoe(i)
self.assertEqual(j,result)
Here checking whether the output equals to output of our oddeven.py Now we have to design errors or assume errors.Here possible errors are
zero,float value,string.Now i am defining expected errors.
import oddeven
import unittest
class KnownValues(unittest.TestCase):
knownValues=((1,'odd'),(2,'even'),(11,'odd'))
def testoddeven(self):
for i,j in self.knownValues:
result=oddeven.testoe(i)
self.assertEqual(j,result)
class OddEvenBadInput(unittest.TestCase):
def testZero(self):
self.assertRaises(oddeven.ZeroError, oddeven.testoe,0)
def testNonInteger(self):
self.assertRaises(oddeven.NonIntegerError, oddeven.testoe,6.5)
def testString(self):
self.assertRaises(oddeven.StringError, oddeven.testoe,"abc")
if __name__ == "__main__":
unittest.main()
Then start coding oddeven.py Then define the errors in original program sa below.
class ZeroError(Exception):pass
class NonIntegerError(Exception):pass
class StringError(Exception):pass
Then define each line and test each time using python testoddeven.py Rewrite the program until 'ok' gets...
class ZeroError(Exception):pass
class NonIntegerError(Exception):pass
class StringError(Exception):pass
def testoe(n):
if type(n)==int:
if n==0:raise ZeroError,"zero..."
elif n%2==0:
return "even"
else:
return "odd"
elif type(n)==str:
raise StringError,"string...!!!!"
else: raise NonIntegerError,"Non int...."
unit testing is a method by which individual units of source code, sets of one or more computer program modules together with associated control data, usage procedures, and operating procedures are tested to determine if they are fit for use. Unit tests are created by programmers or occasionally by white box testers during the development process.One can view a unit as the smallest testable part of an application.Unit tests are typically written and run by software developers to ensure that code meets its design and behaves as intended.
To view the codes.Click here
No comments:
Post a Comment