在python中遇到单元测试问题

时间:2017-03-15 19:04:25

标签: python unit-testing

我编写的代码运行良好,没有单元测试。但是当我对它进行单元测试时失败了。

这是源代码

"""
shoppingcart.py
This program allows me to manage my shopping cart items
@autohor chuzksy
@version 2017-11-30
"""
class ShoppingCart(object):
    """This class manages shopping cart items in terms of
    adding items, removing items and updating the cart list
    """

    def __init__(self):
        """This is a constructor method belonging to the object of this           ShoppingCart class"""
        self.total = 0
        self.items = {}

    def add_item(self, item_name, quantity, price):
        """This method add items to the shopping cart dictionary"""
        self.total = self.total + (quantity * price)
        self.items.update({item_name: quantity})

    def remove_item(self, item_name, quantity, price):
        """This method removes an item from the shopping cart and updates it"""
        for name, qty in self.items.items():
            if item_name == name:
                if quantity < qty:
                    del self.items[item_name]
                    self.items.update({item_name: qty - quantity})
                    self.total -= (quantity * price)
                else:
                    del self.items[item_name]
                    break

    def checkout(self, cash_paid):
        """This method allows the user to pay for the items in the shopping cart"""
        if cash_paid < self.total:
            return "Cash paid not enough"
        else:
            return cash_paid - self.total

class Shop(ShoppingCart):
    """This is another class inheriting attributes and methods from the ShoppingCart class"""

    def __init__(self):
        super().__init__(self)
        self.quantity = 100

    def remove_item(self):
        self.quantity -= 1

这是单元测试代码:

import unittest
from shoppingcart import ShoppingCart
from shoppingcart import Shop


class ShoppingCartTestCases(unittest.TestCase):
    def setUp(self):
        self.cart = ShoppingCart()
        self.shop = Shop()

    def test_cart_property_initialization(self):
        self.assertEqual(self.cart.total, 0, msg='Initial value of total not  correct')
        self.assertIsInstance(self.cart.items, dict, msg='Items is not a dictionary')

    def test_add_item(self):
       self.cart.add_item('Mango', 3, 10)

       self.assertEqual(self.cart.total, 30, msg='Cart total not correct after adding items')
       self.assertEqual(self.cart.items['Mango'], 3, msg='Quantity of items not correct after adding item')

    def test_remove_item(self):
        self.cart.add_item('Mango', 3, 10)
        self.cart.remove_item('Mango', 2, 10)    
        self.assertEqual(self.cart.total, 10, msg='Cart total not correct after removing item')
        self.assertEqual(self.cart.items['Mango'], 1, msg='Quantity of items not correct after removing item')

    def test_checkout_returns_correct_balance(self):
        self.cart.add_item('Mango', 3, 10)
        self.cart.add_item('Orange', 16, 10)

        self.assertEqual(self.cart.checkout(265), 75, msg='Balance of checkout not correct')
        self.assertEqual(self.cart.checkout(25), 'Cash paid not enough', msg='Balance of checkout not correct')

    def test_shop_is_instance_of_shopping_cart(self):
        self.assertTrue(isinstance(self.shop, ShoppingCart), msg='Shop is not a subclass of ShoppingCart')

    def test_shop_remove_item_method(self):
        for i in range(15):
              self.shop.remove_item()

        self.assertEqual(self.shop.quantity, 85)

    if __name__ == '__main__':
        unittest.main(exit = False)
        print("test pass")

以下是运行单元测试程序后得到的输出

enter image description here

提前非常感谢你。

2 个答案:

答案 0 :(得分:0)

这应该有助于解决子类的问题:

class Shop(ShoppingCart):
  def __init__(self):
    self.quantity = 100
  def remove_item(self):
    self.quantity = self.quantity - 1
    return self.quantity

答案 1 :(得分:0)

错误是正确的。 ShoppingCartTestCases没有引用名为&#34; shop&#34;除非你先调用setUp方法。你确定已经完成了吗?