UNIT TESTS


There are two types of engineers in this world(software context):
  1. type-1 just writes code and walks away, they assume everything will work fine.
  2. type-2 does the unit test and brings the piece of mind to type-1.
Now we go through the unit testing in python, This helps us to the verify how well our code works with the required inputs.
If we ask an engineer to write the code for the area of the circle, he writes it with no effort.

 from math import pi  
   
 def circle_area(r):  
      return pi*(r**2)  
 #Test function will not be return by type-1 Engineers  
 #Testing function  
   
 radii = [2, 0, -3, 2+3j,True ,"radius"]  
 message = "Area of circle with r = {radius} is {area}"  
   
 for r in radii:  
      A = circle_area(r)  
      print(message.format(radius=r, area=A))  

Output:

we have written the testing code just to show you how the code goes wrong with the inputs.
engineers just write the function and get relaxed.

From the above inputs, we get the following outputs where some of it are not acceptable
Ex:
  1.    Area of circle with radius = -3 is 28.3
  2.    Area of circle with r = (2+3j) is (-15.707963267948966+37.69911184307752j)
  3.   Area of circle with r = True is 3.141592653589793
  4.    Area of circle with r = “radius”(string) is an error

By having a look at these results how can we accept this code?, so rather than criticizing the work of engineer we now write the unit tests to check whether the code works fine or not before submitting.

Unit Testing:

  1. File naming convention,
    1. If the file name is circles.py then the unit test file name is either test_circles.py. If there are 10 python module files then there will be 10 tests files(all are grouped together).
      1. Circles.py
      2. Parabola.py
      3. Test_circles.py
      4. Test_parabola.py
    2. If the file name is circles.py then the unit test file name is circles_test.py. Here test files appear next to the python module file
      1. Circles.py
      2. Circles_test.py
      3. Parabola.py
      4. Parabola_test.py
    3. It is our choice to choose which naming convention suits. (Now we will following the first convention)
  2. Create circles.py and test_circles.py
    1. In circles.py has the following code:
       from math import pi     
       def circle_area(r):  
            return pi*(r**2)  
      
      
    2. In test_cases.py, import the methods of our circles.py and unittest module. Then define a subclass of TestCases class and the test_area, test_values, test_types method for checking the areas, errors,:
       import unittest  
       from circles import circle_area  
       from math import pi  
         
       class TestCircleArea(unittest.TestCase):  
            def test_areas(self):  
                 #test area when radius > 0  
                 self.assertAlmostEqual(circle_area(1),pi)  
                 self.assertAlmostEqual(circle_area(0),0)  
                 self.assertAlmostEqual(circle_area(2.1),pi*(2.1**2))  
         
            def test_values(self):  #make sure error are raised when necessary  
                 self.assertRaises(ValueError,circle_area,-2)  
         
            def test_types(self):  
                 #make sure type errors are raised when necessary  
                 self.assertRaises(TypeError,circle_area,3+5j)  
                 self.assertRaises(TypeError,circle_area,True)  
                 self.assertRaises(TypeError,circle_area,"radius")  
      
    3. TestCase contain many other functions, to check them visit https://docs.python.org/2/library/unittest.html#
    4. run the test_cricles.py as python scripts:
  3. Then we will get two failures(shown above). Inform the type-1 engineers to resolve them.
    1. To resolve them, edit the circles.py as shown below:
       from math import pi  
         
       def circle_area(r):  
            if type(r) not in [int, float]:  
                 raise TypeError("The radius must be a non-negative real number.")  
            if r < 0:  
                 raise ValueError("The radius can not be negative.")  
         
            return pi*(r**2)  
    2. Now run the test_circles.py:
    3. All the tests run successfully. (Hurry, we can happily submit it) 
  4. If you want to test any other examples then just add them to test_circles.py and try to validate them using the built-in functions of TestCase
  5. To know about a built-in function(how to use), do it as shown below:
Feel free to comment about mistakes and doubts.


Comments

Popular posts from this blog

Splitting criteria

QUOTATION SERVER AND CLIENT

How to Handle Noisy Data in preprocessing of data?