0% found this document useful (0 votes)
9 views5 pages

Unittest

The document defines a Circle class with an initializer that enforces numeric radius constraints and methods for calculating area and circumference. It also includes a TestingCircleCreation class with unit tests to verify the behavior of the Circle class under various conditions, such as valid and invalid radius values. Additionally, there is a TestingCircleCircumference class that tests the circumference method for different radius values.

Uploaded by

Gurram Anurag
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views5 pages

Unittest

The document defines a Circle class with an initializer that enforces numeric radius constraints and methods for calculating area and circumference. It also includes a TestingCircleCreation class with unit tests to verify the behavior of the Circle class under various conditions, such as valid and invalid radius values. Additionally, there is a TestingCircleCircumference class that tests the circumference method for different radius values.

Uploaded by

Gurram Anurag
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

Define a class Circle with method init which initializes a cicle with attribute

radius, having follwing restrictions. a. radius must be numeric value, if not raise
type error with error message "radius must be number".
b. radius must be between 0 to 1000 inclusive on both sides, if not raise the value
error with error message "radius must be between 0 and 1000 inclusive"

c. Define a class method area and circumference which must return values rounded
off to 2 decimals.

Complete the definition of class TestingCircleCreation which tests the behaviour


init method as specification below.

Define the test method of 'test_creating_circle_with_numerical_radius' which


creates circle with radius 2.5 and check if radius matches to value 2.5

Define the test method test_creating_circle_with_negative_radius which checks if


value error exception is raised with the error message "radius must be between 0
and 1000 inclusive", while creating circle of radius 2.5.

Define the test method test_creating_circle_with_greaterthan_radius which checks if


ValueError exception is raised with error message "radius must be between 0 and
1000 inclusive", while creating circle of radius 1000.1.

Define the test method test_creating_circle_with_nonnumeric_radius, which checks if


TypeError exception is raised with error message "radius must be number" while
creating circle of radius 'hello'.
import inspect
import re
import unittest
import math

# Define below the class 'Circle' and it's methods with proper doctests.
class Circle:

def __init__(self, radius):


# Define the initialization method below
self.radius=radius
if not isinstance(self.radius,(int,float)):
raise TypeError("radius must be a number")
elif(self.radius>1000 or self.radius<0):
raise ValueError("radius must be between 0 and 1000 inclusive")
else:
pass
def area(self):
# Define the area functionality below
return math.pi*(self.radius**2)
def circumference(self):
return 2*math.pi*self.radius
# Define the circumference functionality below
class TestCircleCreation(unittest.TestCase):

def test_creating_circle_with_numeric_radius(self):
# Define a circle 'c1' with radius 2.5 and check if
# the value of c1.radius equal to 2.5 or not
c1=Circle(2.5)
self.assertEqual(c1.radius,2.5)

def test_creating_circle_with_negative_radius(self):
# Try Defining a circle 'c' with radius -2.5 and see
# if it raises a ValueError with the message
# "radius must be between 0 and 1000 inclusive"
with self.assertRaises(ValueError) as e:
c=Circle(-2.5)
self.assertEqual(str(e.exception),"radius must be between 0 and 1000
inclusive")
def test_creating_circle_with_greaterthan_radius(self):
# Try Defining a circle 'c' with radius 1000.1 and see
# if it raises a ValueError with the message
# "radius must be between 0 and 1000 inclusive"
with self.assertRaises(ValueError) as e:
c=Circle(1000.1)
self.assertEqual(str(e.exception),"radius must be between 0 and 1000
inclusive")

def test_creating_circle_with_nonnumeric_radius(self):
# Try Defining a circle 'c' with radius 'hello' and see
# if it raises a TypeError with the message
# "radius must be a number"
with self.assertRaises(TypeError) as e:
c=Circle("hello")
self.assertEqual(str(e.exception),"radius must be a number")
********
3 rd
*******
Define a class Circle with method init which initializes a cicle with attribute
radius, having follwing restrictions.

radius must be numeric value, if not raise type error with error message "radius
must be number".
radius must be between 0 to 1000 inclusive on both sides, if not raise the value
error with error message "radius must be between 0 and 1000 inclusive"
Define a class method area and circumference which must return values rounded off
to 2 decimals.
Complete the definition of class TestingCircleCircumference which tests the
behaviour of circumference method as specification below.

Define the test method test_circlecircum_with_random_numerical_radius which creates


circle c1 with radius 2.5 and check if its computed circumference match the value
15.71

Define the test method test_circlecircum_with__min_radius which creates circle c2


with radius 0 and check if its computed circumference match the value 0

Define the test method test_circlecircum_with_max_radius which creates circle c3


with radius 1000 and check if its computed circumference match the value 6283.19

class Circle:

def __init__(self, radius):


# Define the initialization method below
self.radius = radius
if not isinstance(self.radius,(int,float)):
raise TypeError("radius must be a number")
elif self.radius<0 or self.radius>1000:
raise ValueError("radius must be between 0 and 1000 inclusive")
else:
pass
def area(self):
# Define the area functionality below
return round(math.pi*(self.radius**2),2)

def circumference(self):
# Define the circumference functionality below
return round(2*math.pi*self.radius,2)
class TestCircleCircumference(unittest.TestCase):

def test_circlecircum_with_random_numeric_radius(self):
# Define a circle 'c1' with radius 2.5 and check if
# it's circumference is 15.71
c1 = Circle(2.5)
self.assertEqual(c1.circumference(),15.71)

def test_circlecircum_with_min_radius(self):
# Define a circle 'c2' with radius 0 and check if
# it's circumference is 0.
c2 = Circle(0)
self.assertEqual(c2.circumference(),0)

def test_circlecircum_with_max_radius(self):
# Define a circle 'c3' with radius 1000 and check if
# it's circumference is 6283.19.
c3 = Circle(1000)
self.assertEqual(c3.circumference(),6283.19)

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('.')
and line.endswith('.\n')]

pass_count = pass_count[0]

print(str(pass_count))

doc1 =
inspect.getsource(TestCircleCircumference.test_circlecircum_with_random_numeric_rad
ius)
doc2 =
inspect.getsource(TestCircleCircumference.test_circlecircum_with_min_radius)
doc3 =
inspect.getsource(TestCircleCircumference.test_circlecircum_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))

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('.')
and line.endswith('.\n')]

pass_count = pass_count[0]

print(str(pass_count))

doc1 =
inspect.getsource(TestCircleCreation.test_creating_circle_with_numeric_radius)
doc2 =
inspect.getsource(TestCircleCreation.test_creating_circle_with_negative_radius)
doc3 =
inspect.getsource(TestCircleCreation.test_creating_circle_with_greaterthan_radius)
doc4 =
inspect.getsource(TestCircleCreation.test_creating_circle_with_nonnumeric_radius)

assert1_count = len(re.findall(r'assertEqual', doc1))

print(str(assert1_count))

assert1_count = len(re.findall(r'assertEqual', doc2))


assert2_count = len(re.findall(r'assertRaises', doc2))

print(str(assert1_count), str(assert2_count))

assert1_count = len(re.findall(r'assertEqual', doc3))


assert2_count = len(re.findall(r'assertRaises', doc3))

print(str(assert1_count), str(assert2_count))

assert1_count = len(re.findall(r'assertEqual', doc4))


assert2_count = len(re.findall(r'assertRaises', doc4))

print(str(assert1_count), str(assert2_count))

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy