Unit Testing with unittest in Python: Problem 2 and Solutions

Rumman Ansari   Software Engineer   2024-08-05 06:25:08   275  Share
Subject Syllabus DetailsSubject Details
☰ TContent
☰Fullscreen

Table of Content:

2. Problem 2 - Unit Testing using unittest in Python

 

Complete the definition of class 'Circle' with the following specifications:

• Define class method '_init_' which initializes a circle with attribute 'radius' having the following restrictions: • 'radius' must be a numeric value. If not, raise TypeError with error message "radius must be a number".

• 'radius' must be between 0 and 1000 inclusive on both sides. If not, raise ValueError with error message "radius must be between 0 and 1000 inclusive".

• Define class method 'area' which must compute the area of a circle, and return the value rounded off to 2 decimals.

Hint: Use 'pi' value from 'math' module.

• Define class method 'circumference' which must compute the circumference of a circle, and return the value rounded off to 2 decimals. Hint: Use 'pi' value from 'math' module.

 

Complete the definition of class 'TestCircleArea' which tests the behavior of the 'area' method, as specified:

• Define test method 'test_circlearea_with_random_numeric_radius' which creates a circle 'c1' with radius 2.5, and checks if its computed area matches the value 19.63.

• Define test method 'test_circlearea_with_min_radius' which creates a circle 'c2' with radius 0, and checks if its computed area matches the value 0.

• Define test method 'test_circlearea_with_max_radius' which creates a circle 'c3' with radius 1000, and checks if its computed area matches the value 3141592.65.


import inspect
import re
import unittest
import math

class Circle:
    
    def __init__(self, radius):
        # Define initialization method:
        if not isinstance(radius, (int, float)):
            raise TypeError("radius must be a number")
        if not 0 <= radius <= 1000:
            raise ValueError("radius must be between 0 and 1000 inclusive")
        self.radius = radius
        
    def area(self):
        # Define area functionality:
        return round(math.pi * self.radius**2, 2)
               
    def circumference(self):
        # Define circumference functionality:
        return round(2 * math.pi * self.radius, 2)
        
class TestCircleArea(unittest.TestCase):
    
    def test_circlearea_with_random_numeric_radius(self):
        # Define a circle 'c1' with radius 2.5, and check if 
        # its area is 19.63.
        c1 = Circle(2.5)
        self.assertEqual(c1.area(), 19.63)
        
    def test_circlearea_with_min_radius(self):
        # Define a circle 'c2' with radius 0, and check if 
        # its area is 0.
        c2 = Circle(0)
        self.assertEqual(c2.area(), 0)
        
    def test_circlearea_with_max_radius(self):
        # Define a circle 'c3' with radius 1000, and check if 
        # its area is 3141592.65.
        c3 = Circle(1000)
        self.assertEqual(c3.area(), 3141592.65)

if __name__ == '__main__':
    
    fptr = open('output.txt', 'w')
    
    runner = unittest.TextTestRunner(fptr)
    
    unittest.main(testRunner=runner, exit=False)
    
    fptr.close()
    
    with open('output.txt') as fp:
        output_lines = fp.readlines()
    
    
    pass_count = [ len(re.findall(r'\.', line)) for line in output_lines if (line.startswith('.') or line.startswith('E') or line.startswith('F')) and line.endswith('\n')]
    
    pass_count = pass_count[0]
                       
    print(str(pass_count))
                       
    doc1 = inspect.getsource(TestCircleArea.test_circlearea_with_random_numeric_radius)
    doc2 = inspect.getsource(TestCircleArea.test_circlearea_with_min_radius)
    doc3 = inspect.getsource(TestCircleArea.test_circlearea_with_max_radius)
    
    assert1_count = len(re.findall(r'assertEqual', doc1))
    
    print(str(assert1_count))
    
    assert1_count = len(re.findall(r'assertEqual', doc2))
    
    print(str(assert1_count))
    
    assert1_count = len(re.findall(r'assertEqual', doc3))
    
    print(str(assert1_count))