0% found this document useful (0 votes)
120 views7 pages

New Text Document

The document contains test cases for testing different functionalities of a Circle class including initialization, area calculation, and circumference calculation. It defines test cases to test: 1) Initialization of Circle objects with valid numeric radius values 2) Calculation of area and circumference for Circle objects initialized with random, minimum and maximum radius values 3) Raising errors for invalid radius types or values during initialization The test cases utilize unittest and assert methods to test the expected functionality and outputs against actual outputs of the Circle class methods.

Uploaded by

Sumeet Chauhan
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)
120 views7 pages

New Text Document

The document contains test cases for testing different functionalities of a Circle class including initialization, area calculation, and circumference calculation. It defines test cases to test: 1) Initialization of Circle objects with valid numeric radius values 2) Calculation of area and circumference for Circle objects initialized with random, minimum and maximum radius values 3) Raising errors for invalid radius types or values during initialization The test cases utilize unittest and assert methods to test the expected functionality and outputs against actual outputs of the Circle class methods.

Uploaded by

Sumeet Chauhan
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/ 7

"""

>>>isPalindrome(121)
True
>>>isPalindrome(344)
False
>>>isPalindrome(-121)
Traceback (most recent call last):
ValueError : x must be a positive integer
>>>isPalindrome("hello")
Traceback (most recent call last):
TypeError : x must be an integer
"""
# Write the functionality:
x = int(x)
temp=x
rev=0
if(x>0):
while(x>0):
dig=x%10
rev=rev*10+dig
x=x//10
if(temp==rev):
return True
else:
return False
/////////////////////////////////////////////////////////
def __init__(self, radius):
# Define doctests for __init__ method:
"""
>>> c1 = Circle(2.5)
>>> c1.radius
2.5
"""
self.radius = radius

def area(self):
# Define doctests for area method:
"""
>>> c1 = Circle(2.5)
>>> c1.area()
19.63
"""
# Define area functionality:
y=math.pi*(self.radius**2)
return round(y,2)

def circumference(self):
# Define doctests for circumference method:
"""
>>> c1 = Circle(2.5)
>>> c1.circumference()
15.71
"""
# Define circumference functionality:
x=math.pi*2*self.radius
return round(x,2)
/////////////////////////////////////////////////////////////
class Circle:
def __init__(self, radius):

self.radius = 0
if not isinstance(radius,(int,float)):
raise TypeError("radius must be a number")
elif radius < 0 or radius > 1000:
raise ValueError("radius must be between 0 and 1000 inclusive")
else:
self.radius = radius

def area(self):

return round(math.pi*(self.radius**2),2)

def circumference(self):

return round((math.pi*self.radius*2),2)

class TestCircleArea(unittest.TestCase):

def test_circlearea_with_random_numeric_radius(self):

c1 = Circle(2.5)
self.assertEqual(c1.area(), 19.63)

def test_circlearea_with_min_radius(self):

c2 = Circle(0)
self.assertEqual(c2.area(), 0)

def test_circlearea_with_max_radius(self):

c3 = Circle(1000)
self.assertEqual(c3.area(), 3141592.65)
//////////////////////////////////////////////////////////////////////
class Circle:

def __init__(self, radius):


# Define the initialization method below
pattern = re.compile("^\\-?[0-9]")
if(pattern.match(str(radius))):
if(radius >= 0 and radius <= 1000):
self.radius = radius
else:
raise ValueError("radius must be between 0 and 1000 inclusive")
else:
raise TypeError("radius must be a number")

def area(self):
# Define the area functionality below
return round(((self.radius ** 2) * math.pi),2)

def circumference(self):
# Define the circumference functionality below
return round((self.radius * 2 * math.pi),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)
////////////////////////////////////////////////////////////////nosehandson////////
///////////////
class TestingCircleCreation:
def test_creating_circle_with_numeric_radius(self):
c=Circle(2.5)
eq_(c.radius, 2.5)

def test_creating_circle_with_negative_radius(self):
with assert_raises(ValueError) as e:
c = Circle(-2.5)
eq_(str(e.exception), "radius must be between 0 and 1000 inclusive")

def test_creating_circle_with_greaterthan_radius(self):
with assert_raises(ValueError) as e:
c = Circle(1000.1)
eq_(str(e.exception), "radius must be between 0 and 1000 inclusive")

def test_creating_circle_with_nonnumeric_radius(self):
with assert_raises(TypeError) as e:
c=Circle("hello")
eq_(str(e.exception), "radius must be a number")

class TestCircleArea:
def test_circlearea_with_random_numeric_radius(self):
c1=Circle(2.5)
eq_(c1.area(), 19.63)

def test_circlearea_with_min_radius(self):
c2=Circle(0)
eq_(int(c2.area()), 0)

def test_circlearea_with_max_radius(self):
c3=Circle(1000)
eq_(c3.area(), 3141592.65)
class TestCircleCircumference:
def test_circlecircum_with_random_numeric_radius(self):
c1=Circle(2.5)
eq_(c1.circumference(), 15.71)

def test_circlecircum_with_min_radius(self):
c2=Circle(0)
eq_(int(c2.circumference()), 0)

def test_circlecircum_with_max_radius(self):
c3=Circle(1000)
eq_(c3.circumference(), 6283.19)
///////////////////////////////////////////////////////////////////////////////////
////
class TestingInventoryCreation:

# Define a pytest test method **'test_creating_empty_inventory'**, which


creates an empty inventory and checks if its 'balance_inventory' is an empty dict
using assert.
def test_creating_empty_inventory(self):
x = MobileInventory()
assert x.balance_inventory == {}
# Define a pytest test method **'test_creating_specified_inventory'**, which
checks if inventory instance with input {'iPhone Model X':100, 'Xiaomi Model Y':
1000, 'Nokia Model Z':25}.
def test_creating_specified_inventory(self):
x = MobileInventory({'iPhone Model X':100 , 'Xiaomi Model Y':1000,'Nokia
Model Z':25})
assert {'iPhone Model X':100 , 'Xiaomi Model Y':1000,'Nokia Model Z':25} ==
x.balance_inventory

# Define a pytest test method **'test_creating_inventory_with_list'**, which


checks if the method raises a TypeError with the message "Input inventory must be
a dictionary" when a list "['iPhone Model X', 'Xiaomi Model Y', 'Nokia Model Z']"
is passed as input using assert.
def test_creating_inventory_with_list(self):
with pytest.raises(TypeError) as excinfo:
MobileInventory(
['iPhone Model X', 'Xiaomi Model Y', 'Nokia Model Z'])
assert "Input inventory must be a dictionary" == str(excinfo.value)

# Define a pytest test method **'test_creating_inventory_with_numeric_keys'**,


which checks if the method raises a ValueError with the message "Mobile model name
must be a string" using assert, when the dict {1:'iPhone Model X', 2:'Xiaomi Model
Y', 3:'Nokia Model Z'} is passed as input.
def test_creating_inventory_with_numeric_keys(self):
with pytest.raises(ValueError) as excinfo:
MobileInventory({1: 'iPhone Model X', 2: 'Xiaomi Model Y', 3: 'Nokia
Model Z'})
assert "Mobile model name must be a string" == str(excinfo.value)

# Define a pytest test method


**'test_creating_inventory_with_nonnumeric_values'**, which checks if the method
raises a ValueError with the message "No. of mobiles must be a positive integer"
using assert, when the dict {'iPhone Model X':'100', 'Xiaomi Model Y': '1000',
'Nokia Model Z':'25'} is passed as input.
def test_creating_inventory_with_nonnumeric_values(self):
with pytest.raises(ValueError) as excinfo:
MobileInventory({'iPhone Model X': '100', 'Xiaomi Model Y': '1000',
'Nokia Model Z': '25'})
assert "No. of mobiles must be a positive integer" == str(excinfo.value)

# Define a pytest test method


**'test_creating_inventory_with_negative_value'**, which checks if the method
raises a ValueError with the message "No. of mobiles must be a positive integer"
using assert, when the dict {'iPhone Model X':-45, 'Xiaomi Model Y': 200, 'Nokia
Model Z':25} is passed as input.
def test_creating_inventory_with_negative_value(self):
with pytest.raises(ValueError) as excinfo:
MobileInventory({'iPhone Model X': -45, 'Xiaomi Model Y': 200, 'Nokia
Model Z': 25})
assert "No. of mobiles must be a positive integer" == str(excinfo.value)
# Define another pytest test class **'TestInventoryAddStock'**, which tests the
behavior of the **'add_stock'** method, with the following tests
class TestInventoryAddStock:

# Define a pytest class fixture 'setup_class', which creates an


**'MobileInventory'** instance with input {'iPhone Model X':100, 'Xiaomi Model Y':
1000, 'Nokia Model Z':25} and assign it to class attribute **'inventory'**.
@classmethod
def setup_class(cls):
cls.inventory = MobileInventory(
{'iPhone Model X': 100, 'Xiaomi Model Y': 1000, 'Nokia Model Z': 25})

# Define a pytest test method **'test_add_new_stock_as_dict'**, which adds the


new stock {'iPhone Model X':50, 'Xiaomi Model Y': 2000, 'Nokia Model A':10} to the
existing inventory, and update the **balance_inventory** attribute. Also, check if
the updated **balance_inventory** equals {'iPhone Model X':150, 'Xiaomi Model Y':
3000, 'Nokia Model Z':25, 'Nokia Model A':10} using assert.
def test_add_new_stock_as_dict(self):
self.inventory.add_stock({"iPhone Model X": 50, "Xiaomi Model Y": 2000,
"Nokia Model A": 10})
assert {"iPhone Model X": 150, "Xiaomi Model Y": 3000, "Nokia Model Z": 25,
"Nokia Model A": 10} == self.inventory.balance_inventory

# Define a pytest test method **'test_add_new_stock_as_list'**, which adds the


new stock ['iPhone Model X', 'Xiaomi Model Y', 'Nokia Model Z'] to the existing
inventory, and which checks if the method raises a TypeError with the message
"Input stock must be a dictionary" using assert.
def test_add_new_stock_as_list(self):
with pytest.raises(TypeError) as excinfo:
self.inventory.add_stock(
['iPhone Model X', 'Xiaomi Model Y', 'Nokia Model Z'])
assert "Input stock must be a dictionary" == str(excinfo.value)

# Define a pytest test method **'test_add_new_stock_with_numeric_keys'**, which


adds the new stock {1:'iPhone Model A', 2:'Xiaomi Model B', 3:'Nokia Model C'} to
the existing inventory, and which checks if the method raises a ValueError with the
message "Mobile model name must be a string" using assert.
def test_add_new_stock_with_numeric_keys(self):
with pytest.raises(ValueError) as excinfo:
self.inventory.add_stock({1: 'iPhone Model A', 2: 'Xiaomi Model B', 3:
'Nokia Model C'})
assert "Mobile model name must be a string" == str(excinfo.value)

# Define a pytest test method **'test_add_new_stock_with_nonnumeric_values'**,


which adds the new stock {'iPhone Model A':'50', 'Xiaomi Model B':'2000', 'Nokia
Model C':'25'} to the existing inventory, and which checks if the method raises a
ValueError with the message "No. of mobiles must be a positive integer" using
assert.
def test_add_new_stock_with_nonnumeric_values(self):
with pytest.raises(ValueError) as excinfo:
self.inventory.add_stock({"iPhone Model A": "50", "Xiaomi Model B":
"2000", "Nokia Model C": "25"})
assert "No. of mobiles must be a positive integer" == str(excinfo.value)

# Define a pytest test method **'test_add_new_stock_with_float_values'**, which


adds the new stock {'iPhone Model A':50.5, 'Xiaomi Model B':2000.3, 'Nokia Model
C':25} to the existing inventory, and which checks if the method raises a
ValueError with the message "No. of mobiles must be a positive integer" using
assert.

def test_add_new_stock_with_float_values(self):
with pytest.raises(ValueError) as excinfo:
self.inventory.add_stock(
{'iPhone Model A': 50.5, 'Xiaomi Model B': 2000.3, 'Nokia Model C':
25})
assert "No. of mobiles must be a positive integer" == str(excinfo.value)

# Define another pytest test class **'TestInventorySellStock'**, which tests the


behavior of the **'sell_stock'** method, with the following tests
class TestInventorySellStock:
# Define a pytest class fixture 'setup_class', which creates an
**'MobileInventory'** instance with the input {'iPhone Model A':50, 'Xiaomi Model
B': 2000, 'Nokia Model C':10, 'Sony Model D':1}, and assign it to the class
attribute **'inventory'**.
@classmethod
def setup_class(cls):
cls.inventory = MobileInventory(
{'iPhone Model A': 50, 'Xiaomi Model B': 2000, 'Nokia Model C': 10,
'Sony Model D': 1})

# Define a pytest test method **'test_sell_stock_as_dict'**, which sells the


requested stock {'iPhone Model A':2, 'Xiaomi Model B':20, 'Sony Model D':1} from
the existing inventory, and update the **balance_inventory** attribute. Also check
if the updated **balance_inventory** equals {'iPhone Model A':48, 'Xiaomi Model B':
1980, 'Nokia Model C':10, 'Sony Model D':0} using assert.
def test_sell_stock_as_dict(self):
self.inventory.sell_stock(
{'iPhone Model A': 2, 'Xiaomi Model B': 20, 'Sony Model D': 1})
assert self.inventory.balance_inventory == {'iPhone Model A': 48, 'Xiaomi
Model B': 1980, 'Nokia Model C': 10,
'Sony Model D': 0}
# Define a pytest test method **'test_sell_stock_as_list'**, which tries
selling the requested stock ['iPhone Model A', 'Xiaomi Model B', 'Nokia Model C']
from the existing inventory, and which checks if the method raises a TypeError with
the message "Requested stock must be a dictionary" using assert.

def test_sell_stock_as_list(self):
with pytest.raises(TypeError) as excinfo:
self.inventory.sell_stock(
["iPhone Model A", "Xiaomi Model B", "Nokia Model C"])
assert "Requested stock must be a dictionary" == str(excinfo.value)

# Define a pytest test method **'test_sell_stock_with_numeric_keys'**, which


tries selling the requested stock {1:'iPhone Model A', 2:'Xiaomi Model B', 3:'Nokia
Model C'} from the existing inventory, and which checks if the method raises
ValueError with the message "Mobile model name must be a string" using assert.
def test_sell_stock_with_numeric_keys(self):
with pytest.raises(ValueError) as excinfo:
self.inventory.sell_stock(MobileInventory(
{1: 'iPhone Model A', 2: 'Xiaomi Model B', 3: 'Nokia Model C'}))
assert "Mobile model name must be a string" == str(excinfo.value)

# Define a pytest test method **'test_sell_stock_with_nonnumeric_values'**,


which tries selling the requested stock {'iPhone Model A':'2', 'Xiaomi Model
B':'3', 'Nokia Model C':'4'} from the existing inventory, and which checks if the
method raises a ValueError with the message "No. of mobiles must be a positive
integer" using assert.
def test_sell_stock_with_nonnumeric_values(self):
with pytest.raises(ValueError) as excinfo:
self.inventory.sell_stock(MobileInventory(
{'iPhone Model A': '2', 'Xiaomi Model B': '3', 'Nokia Model C':
'4'}))
assert "No. of mobiles must be a positive integer" == str(excinfo.value)

# Define a pytest test method **'test_sell_stock_with_float_values'**, which


tries selling the requested stock {'iPhone Model A':2.5, 'Xiaomi Model B':3.1,
'Nokia Model C':4} from the existing inventory, and which checks if the method
raises a ValueError with the message "No. of mobiles must be a positive integer"
using assert.
def test_sell_stock_with_float_values(self):
with pytest.raises(ValueError) as excinfo:
self.inventory.sell_stock(MobileInventory(
{'iPhone Model A': 2.5, 'Xiaomi Model B': 3.1, 'Nokia Model C':
4}))
assert "No. of mobiles must be a positive integer" == str(excinfo.value)

# Define a pytest test method **'test_sell_stock_of_nonexisting_model'**, which


tries selling the requested stock {'iPhone Model B':2, 'Xiaomi Model B':5} from the
existing inventory, and which checks if the method raises an InsufficientException
with the message "No Stock. New Model Request" using assert.
def test_sell_stock_of_nonexisting_model(self):
with pytest.raises(InsufficientException) as excinfo:
self.inventory.sell_stock({'iPhone Model B': 2, 'Xiaomi Model B': 5})
assert "No Stock. New Model Request" == str(excinfo.value)

# Define a pytest test method **'test_sell_stock_of_insufficient_stock'**,


which tries selling the requested stock {'iPhone Model A':2, 'Xiaomi Model B':5,
'Nokia Model C': 15} from the existing inventory, and which checks if the method
raises an InsufficientException with the message "Insufficient Stock" using assert.
def test_sell_stock_of_insufficient_stock(self):
with pytest.raises(InsufficientException) as excinfo:
self.inventory.sell_stock({'iPhone Model A':2, 'Xiaomi Model B':5,
'Nokia Model C': 15})
assert "Insufficient Stock" == str(excinfo.value)

You might also like