Tuesday 7 August 2012

pyUnit A unit testing framework

Pythons unittest module provides organized tests for our modules. Here i am testing my numTOword conversion module which convert large numbers to corresponding natural language representations.  a 111 will be converted to one hundred eleven consisting all lower-case letters. We use string of digits to represent numbers since we have to convert really huge numbers.  The module converts numbers in a range 1 to 10^100, deliberately missing zero.

The use of unit test module is as follows
 #test.py
import unittest
import convert
class testnumberTOword(unittest.TestCase):
 
 
 numwordmapping={
   '1':'one',
   '11':'eleven',
   '111':'one hundred eleven',
   '1111':'one thousand one hundred eleven',
   '11111':'eleven thousand one hundred eleven',
   '111111':'one lakh eleven thousand one hundred eleven',
   '1111111':'eleven lakh eleven thousand one hundred eleven',
   '11111111':'one crore eleven lakh eleven thousand one hundred eleven',
   '2':'two',
   '22':'twenty two',
   '222':'two hundred twenty two',
   '2222':'two thousand two hundred twenty two',
   '22222':'twenty two thousand two hundred twenty two',
   '222222':'two lakh twenty two thousand two hundred twenty two',
   '2222222':'twenty two lakh twenty two thousand two hundred twenty two',
   '22222222':'two crore twenty two lakh twenty two thousand two hundred twenty two',
   '3':'three',
   '33':'thirty three',
   '333':'three hundred thirty three',
   '3333':'three thousand three hundred thirty three',
   '33333':'thirty three thousand three hundred thirty three',
   '333333':'three lakh thirty three thousand three hundred thirty three',
   '3333333':'thirty three lakh thirty three thousand three hundred thirty three',
   '33333333':'three crore thirty three lakh thirty three thousand three hundred thirty three',
   '4':'four',
   '44':'fourty four',
   '444':'four hundred fourty four',
   '4444':'four thousand four hundred fourty four',
   '44444':'fourty four thousand four hundred fourty four',
   '444444':'four lakh fourty four thousand four hundred fourty four',
   '4444444':'fourty four lakh fourty four thousand four hundred fourty four',
   '44444444':'four crore fourty four lakh fourty four thousand four hundred fourty four',
   '5':'five',
   '55':'fifty five',
   '555':'five hundred fifty five',
   '5555':'five thousand five hundred fifty five',
   '55555':'fifty five thousand five hundred fifty five',
   '555555':'five lakh fifty five thousand five hundred fifty five',
   '5555555':'fifty five lakh fifty five thousand five hundred fifty five',
   '55555555':'five crore fifty five lakh fifty five thousand five hundred fifty five',
   '6':'six',
   '66':'sixty six',
   '666':'six hundred sixty six',
   '6666':'six thousand six hundred sixty six',
   '66666':'sixty six thousand six hundred sixty six',
   '666666':'six lakh sixty six thousand six hundred sixty six',
   '6666666':'sixty six lakh sixty six thousand six hundred sixty six',
   '66666666':'six crore sixty six lakh sixty six thousand six hundred sixty six',
   '7':'seven',
   '77':'seventy seven',
   '777':'seven hundred seventy seven',
   '7777':'seven thousand seven hundred seventy seven',
   '77777':'seventy seven thousand seven hundred seventy seven',
   '777777':'seven lakh seventy seven thousand seven hundred seventy seven',
   '7777777':'seventy seven lakh seventy seven thousand seven hundred seventy seven',
   '77777777':'seven crore seventy seven lakh seventy seven thousand seven hundred seventy seven',
   '8':'eight',
   '88':'eighty eight',
   '888':'eight hundred eighty eight',
   '8888':'eight thousand eight hundred eighty eight',
   '88888':'eighty eight thousand eight hundred eighty eight',
   '888888':'eight lakh eighty eight thousand eight hundred eighty eight',
   '8888888':'eighty eight lakh eighty eight thousand eight hundred eighty eight',
   '88888888':'eight crore eighty eight lakh eighty eight thousand eight hundred eighty eight'
   }
 def testnumtowordmapping(self):
  p=convert.numbertoword()
  for number in self.numwordmapping.keys():
   print 'tested' , number 
   self.assertEqual(p.spel(number),self.numwordmapping[number])
 
 def runTest(self):
  self.checknumtowordmapping()
   

unittest.main()

On running the script we see


----------------------------------------------------------------------
Ran 1 test in 0.004s

OK


To perform unittest, we have to make a subclass for unittest.TestCase. An instance for this class will carry tests.
The num_word_mapping is the dictionary containing few known mappings between numbers and their word representations. We have to test the convert module giving each number and comparing obtained outputs with expected values.

the function testnumtowordmapping() checks convert.numbertoword.spell(number)
by applying each test cases in num_word_mapping.

The function assertEqual(ExpectedResult,ObtainedResult)  will introduce exception when the Obtained_Result differs from Expected_Result and the checknumtowordmapping()
skips with reporting the unittest module an exception

The result of test with taking expected value for 777777777 on convwesion as 'seven crore seventy WRONG VALUE seven lakh seventy seven thousand seven hundred seventy seven' is as follows

AssertionError: 'seven crore seventy seven lakh seventy seven
thousand seven hundred seventy seven' != 'seven crore seventy WRONG
VALUE seven lakh seventy seven thousand seven hundred seventy seven'



 The completion of testnumtowordmapping() means that there are no bugs found in the scope of this test case







No comments:

Post a Comment