vendredi 15 janvier 2021

Getting AttributeError: 'TestInventorySellStock' object has no attribute 'balance_inventory' while testing below pytest framework

I am trying to test a framework containing 3 classes as shown below. While running the test using pytest, i am getting 'AttributeError: 'TestInventorySellStock' object has no attribute 'balance_inventory'' as shown below. 'balance_inventory' has been defined in 2nd class 'MobileInventory', but somehow i am not able to reference it into 3rd class 'TestInventorySellStock'. Can someone please help in pointing where i am doing wrong?

P.S: Apologies in advance if i missed to add any info. Please let me know incase if i need to add any other details.

1st Class:

class InsufficientException(Exception):
    pass

2nd class:

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

    def sell_stock(self, requested_stock):
        if not isinstance(requested_stock, dict):
            raise TypeError("Requested stock must be a dictionary")
        for model in requested_stock:
            if not isinstance(model, str):
                raise ValueError("Mobile model name must be a string")
            if not isinstance(requested_stock[model], int) or requested_stock[model] < 0:
                raise ValueError("No. of mobiles must be a positive integer")
            if model not in self.balance_inventory:
                raise InsufficientException("No Stock. New Model Request")
            if requested_stock[model] > self.balance_inventory[model]:
                raise InsufficientException("Insufficient Stock")
            self.balance_inventory[model] -= requested_stock[model]

3rd Class:

class TestInventorySellStock:
    
    @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})
        assert self.inventory.balance_inventory == {'iPhone Model A': 48, 'Xiaomi Model B': 1980, 'Nokia Model C': 10,
                                                'Sony Model D': 0}

    def test_sell_stock_with_float_values(self):
        with pytest.raises(ValueError) as e:
            MobileInventory.sell_stock(self, {'iPhone Model A': 2.5, 'Xiaomi Model B': 3.1, 'Nokia Model C': 4})
        assert str(e.value) == 'No. of mobiles must be a positive integer'

    def test_sell_stock_of_nonexisting_model(self):
        with pytest.raises(InsufficientException) as e:
            MobileInventory.sell_stock(self, {'iPhone Model B': 2, 'Xiaomi Model B': 5})
        assert str(e.value) == 'No Stock. New Model Request'

    def test_sell_stock_of_insufficient_stock(self):
        with pytest.raises(InsufficientException) as e:
            MobileInventory.sell_stock(self, {'iPhone Model A': 2, 'Xiaomi Model B': 5, 'Nokia Model C': 15})
        assert str(e.value) == 'Insufficient Stock'

Complete Error details:

Complete Error details:

Aucun commentaire:

Enregistrer un commentaire