67% found this document useful (3 votes)
8K views

Python Qualis Pytest HandsOn

The document describes code for testing a MobileInventory class in Python. It defines the MobileInventory class to manage inventory of different mobile models. Tests are defined to validate initialization of the inventory with valid and invalid input dictionaries, adding new stock by calling the add_stock() method with valid and invalid input dictionaries, and selling stock by calling the sell_stock() method. The tests utilize the Pytest framework and expect exceptions to be raised for invalid inputs.

Uploaded by

ARYAN KUMAR
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
67% found this document useful (3 votes)
8K views

Python Qualis Pytest HandsOn

The document describes code for testing a MobileInventory class in Python. It defines the MobileInventory class to manage inventory of different mobile models. Tests are defined to validate initialization of the inventory with valid and invalid input dictionaries, adding new stock by calling the add_stock() method with valid and invalid input dictionaries, and selling stock by calling the sell_stock() method. The tests utilize the Pytest framework and expect exceptions to be raised for invalid inputs.

Uploaded by

ARYAN KUMAR
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 7

Python Qualis Pytest HandsOn

=============================================================
Step 2
=============================================================

import pytest
class InsufficientException(Exception):
pass
class MobileInventory:
balance_inventory = 'None'
def __init__(self,inventory={}):
if type(inventory) is not dict:
raise TypeError("Input inventory must be a dictionary")
for key in inventory:
if type(key) is not str:
raise ValueError("Mobile model name must be a string")
if type(inventory[key]) is int:
if inventory[key] < 0:
raise ValueError("No. of mobiles must be a positive integer")
else:
raise ValueError("No. of mobiles must be a positive integer")
self.balance_inventory = inventory

def add_stock(self, new_stock):


if type(new_stock) is not dict:
raise TypeError("Input inventory must be a dictionary")
for key in new_stock:
if type(key) is not str:
raise ValueError("Mobile model name must be a string")
if type(new_stock[key]) is int:
if new_stock[key] < 0:
raise ValueError("No. of mobiles must be a positive integer")
else:
raise ValueError("No. of mobiles must be a positive integer")
self.balance_inventory.update(new_stock)

def sell_stock(self,requested_stock):
if type(requested_stock) is not dict:
raise TypeError("Input inventory must be a dictionary")
for key in requested_stock:
if type(key) is not str:
raise ValueError("Mobile model name must be a string")
if type(requested_stock[key]) is int:
if requested_stock[key] < 0:
raise ValueError("No. of mobiles must be a positive integer")
else:
raise ValueError("No. of mobiles must be a positive integer")
try:
if self.balance_inventory[key] < requested_stock[key]:
raise ValueError("Insufficient Stock")
except:
raise ValueError("No Stock. New Model Request")
self.balance_inventory[key] =- requested_stock[key]
===================================================================
Step 3
===================================================================

from proj.inventory import MobileInventory, InsufficientException


import pytest

class TestingInventoryCreation():

def test_creating_empty_inventory(self):
c1 = MobileInventory()
assert c1.balance_inventory == {}

def test_creating_specified_inventory(self):
c2 = MobileInventory({'iPhone Model X':100, 'Xiaomi Model Y': 1000, 'Nokia
Model Z':25})
assert c2.balance_inventory == {'iPhone Model X':100, 'Xiaomi Model Y':
1000, 'Nokia Model Z':25}
#{'iPhone Model X':100, 'Xiaomi Model Y': 1000, 'Nokia Model Z':25}

def test_creating_inventory_with_list(self):
#c3 = MobileInventory(['iPhone Model X', 'Xiaomi Model Y', 'Nokia Model
Z'])
with pytest.raises(TypeError) :
c3 = MobileInventory(['iPhone Model X', 'Xiaomi Model Y', 'Nokia Model
Z'])

def test_creating_inventory_with_numeric_keys(self):
#c4 = MobileInventory({1:'iPhone Model X', 2:'Xiaomi Model Y', 3:'Nokia
Model Z'})
with pytest.raises(ValueError):
c4 = MobileInventory({1:'iPhone Model X', 2:'Xiaomi Model Y', 3:'Nokia
Model Z'})

def test_creating_inventory_with_nonnumeric_values(self):
#c5 = MobileInventory({'iPhone Model X':'100', 'Xiaomi Model Y': '1000',
'Nokia Model Z':'25'})
with pytest.raises(ValueError):
c5 = MobileInventory({'iPhone Model X':'100', 'Xiaomi Model Y': '1000',
'Nokia Model Z':'25'})

def test_creating_inventory_with_negative_value(self):
#c6 = MobileInventory({'iPhone Model X':-45, 'Xiaomi Model Y': 200, 'Nokia
Model Z':25})
with pytest.raises(ValueError) :
c6 = MobileInventory({'iPhone Model X':-45, 'Xiaomi Model Y': 200,
'Nokia Model Z':25})

===================================================================
Step 4[Added new class TestInventoryAddStock in Step 3 code]
===================================================================
from proj.inventory import MobileInventory, InsufficientException
import pytest

class TestingInventoryCreation:

def test_creating_empty_inventory(self):
c1 = MobileInventory()
assert c1.balance_inventory == {}

def test_creating_specified_inventory(self):
c2 = MobileInventory({'iPhone Model X': 100,
'Xiaomi Model Y': 1000,
'Nokia Model Z': 25})
assert c2.balance_inventory == {'iPhone Model X': 100,
'Xiaomi Model Y': 1000, 'Nokia Model Z': 25}

# {'iPhone Model X':100, 'Xiaomi Model Y': 1000, 'Nokia Model Z':25}

def test_creating_inventory_with_list(self):

# c3 = MobileInventory(['iPhone Model X', 'Xiaomi Model Y', 'Nokia Model


Z'])

with pytest.raises(TypeError):
c3 = MobileInventory(['iPhone Model X', 'Xiaomi Model Y',
'Nokia Model Z'])

def test_creating_inventory_with_numeric_keys(self):

# c4 = MobileInventory({1:'iPhone Model X', 2:'Xiaomi Model Y', 3:'Nokia


Model Z'})

with pytest.raises(ValueError):
c4 = MobileInventory({1: 'iPhone Model X',
2: 'Xiaomi Model Y', 3: 'Nokia Model Z'
})

def test_creating_inventory_with_nonnumeric_values(self):

# c5 = MobileInventory({'iPhone Model X':'100', 'Xiaomi Model Y': '1000',


'Nokia Model Z':'25'})

with pytest.raises(ValueError):
c5 = MobileInventory({'iPhone Model X': '100',
'Xiaomi Model Y': '1000',
'Nokia Model Z': '25'})

def test_creating_inventory_with_negative_value(self):

# c6 = MobileInventory({'iPhone Model X':-45, 'Xiaomi Model Y': 200, 'Nokia


Model Z':25})

with pytest.raises(ValueError):
c6 = MobileInventory({'iPhone Model X': -45,
'Xiaomi Model Y': 200,
'Nokia Model Z': 25})
class TestInventoryAddStock:

inventory = None

@classmethod
def setup_class(cls):
cls.inventory = MobileInventory({'iPhone Model X': 100,
'Xiaomi Model Y': 1000, 'Nokia Model Z': 25})

def test_add_new_stock_as_dict(self):
self.inventory.add_stock({'iPhone Model X': 50,
'Xiaomi Model Y': 2000,
'Nokia Model A': 10})

def test_add_new_stock_as_list(self):
with pytest.raises(TypeError):
MobileInventory.add_stock(self, ['iPhone Model X',
'Xiaomi Model Y', 'Nokia Model Z'])

def test_add_new_stock_with_numeric_keys(self):
with pytest.raises(ValueError):
MobileInventory.add_stock(self, {1: 'iPhone Model A',
2: 'Xiaomi Model B', 3: 'Nokia Model C'})

def test_add_new_stock_with_nonnumeric_values(self):
with pytest.raises(ValueError):
MobileInventory.add_stock(self, {'iPhone Model A': '50',
'Xiaomi Model B': '2000', 'Nokia ModelC': '25'})

def test_add_new_stock_with_float_values(self):
with pytest.raises(ValueError):
MobileInventory.add_stock(self, {'iPhone Model A': 50.5,
'Xiaomi Model B': 2000.3, 'Nokia Model C': 25})

================================================================
Step 5[Added class TestInventorySellStock in Step 4 code]
================================================================

from proj.inventory import MobileInventory, InsufficientException


import pytest

class TestingInventoryCreation:

def test_creating_empty_inventory(self):
c1 = MobileInventory()
assert c1.balance_inventory == {}

def test_creating_specified_inventory(self):
c2 = MobileInventory({'iPhone Model X': 100,
'Xiaomi Model Y': 1000,
'Nokia Model Z': 25})
assert c2.balance_inventory == {'iPhone Model X': 100,
'Xiaomi Model Y': 1000, 'Nokia Model Z': 25}

# {'iPhone Model X':100, 'Xiaomi Model Y': 1000, 'Nokia Model Z':25}

def test_creating_inventory_with_list(self):

# c3 = MobileInventory(['iPhone Model X', 'Xiaomi Model Y', 'Nokia Model


Z'])

with pytest.raises(TypeError):
c3 = MobileInventory(['iPhone Model X', 'Xiaomi Model Y',
'Nokia Model Z'])

def test_creating_inventory_with_numeric_keys(self):

# c4 = MobileInventory({1:'iPhone Model X', 2:'Xiaomi Model Y', 3:'Nokia


Model Z'})

with pytest.raises(ValueError):
c4 = MobileInventory({1: 'iPhone Model X',
2: 'Xiaomi Model Y', 3: 'Nokia Model Z'
})

def test_creating_inventory_with_nonnumeric_values(self):

# c5 = MobileInventory({'iPhone Model X':'100', 'Xiaomi Model Y': '1000',


'Nokia Model Z':'25'})

with pytest.raises(ValueError):
c5 = MobileInventory({'iPhone Model X': '100',
'Xiaomi Model Y': '1000',
'Nokia Model Z': '25'})

def test_creating_inventory_with_negative_value(self):

# c6 = MobileInventory({'iPhone Model X':-45, 'Xiaomi Model Y': 200, 'Nokia


Model Z':25})

with pytest.raises(ValueError):
c6 = MobileInventory({'iPhone Model X': -45,
'Xiaomi Model Y': 200,
'Nokia Model Z': 25})

class TestInventoryAddStock:

inventory = None

@classmethod
def setup_class(cls):
cls.inventory = MobileInventory({'iPhone Model X': 100,
'Xiaomi Model Y': 1000, 'Nokia Model Z': 25})

def test_add_new_stock_as_dict(self):
self.inventory.add_stock({'iPhone Model X': 50,
'Xiaomi Model Y': 2000,
'Nokia Model A': 10})
def test_add_new_stock_as_list(self):
with pytest.raises(TypeError):
MobileInventory.add_stock(self, ['iPhone Model X',
'Xiaomi Model Y', 'Nokia Model Z'])

def test_add_new_stock_with_numeric_keys(self):
with pytest.raises(ValueError):
MobileInventory.add_stock(self, {1: 'iPhone Model A',
2: 'Xiaomi Model B', 3: 'Nokia Model C'})

def test_add_new_stock_with_nonnumeric_values(self):
with pytest.raises(ValueError):
MobileInventory.add_stock(self, {'iPhone Model A': '50',
'Xiaomi Model B': '2000', 'Nokia ModelC': '25'})

def test_add_new_stock_with_float_values(self):
with pytest.raises(ValueError):
MobileInventory.add_stock(self, {'iPhone Model A': 50.5,
'Xiaomi Model B': 2000.3, 'Nokia Model C': 25})

class TestInventorySellStock:

inventory = None

@classmethod
def setup_class(cls):
cls.inventory = MobileInventory({
'iPhone Model A': 50,
'Xiaomi Model B': 2000,
'Nokia Model C': 10,
'Sony Model D': 1,
})

def test_sell_stock_as_dict(self):
self.inventory.sell_stock({'iPhone Model A': 2,
'Xiaomi Model B': 20,
'Sony Model D': 1})

def test_sell_stock_as_list(self):
with pytest.raises(TypeError):
MobileInventory.sell_stock(self, ['iPhone Model A',
'Xiaomi Model B', 'Nokia Model C'])

def test_sell_stock_with_numeric_keys(self):
with pytest.raises(ValueError):
MobileInventory.sell_stock(self, {1: 'iPhone Model A',
2: 'Xiaomi Model B', 3: 'Nokia Model C'})

def test_sell_stock_with_nonnumeric_values(self):
with pytest.raises(ValueError):
MobileInventory.sell_stock(self, {'iPhone Model A': '5',
'Xiaomi Model B': '3', 'Nokia Model C': '4'})

def test_sell_stock_with_float_values(self):
with pytest.raises(ValueError):
MobileInventory.sell_stock(self, {'iPhone Model A': 2.5,
'Xiaomi Model B': 3.1, 'Nokia Model C': 4})
def test_sell_stock_of_nonexisting_model(self):
with pytest.raises(ValueError):
MobileInventory.sell_stock(self, {'iPhone Model B': 2,
'Xiaomi Model B': 5})

def test_sell_stock_of_insufficient_stock(self):
with pytest.raises(ValueError):
MobileInventory.sell_stock(self, {'iPhone Model A': 2,
'Xiaomi Model B': 5, 'Nokia Model C': 15})

You might also like