【问题标题】:Having issues with unit test in python在 python 中遇到单元测试问题
【发布时间】:2017-03-15 19:04:24
【问题描述】:

我编写的代码在没有单元测试的情况下运行良好。但是当我对其进行单元测试时失败了。

这里是源代码

"""
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")

这是我运行单元测试程序后得到的输出

非常感谢您。

【问题讨论】:

  • 如果您删除了所有未通过的测试,会发生什么情况?
  • python2 或 python3
  • 你的缩进太远了。如果这就是它在源文件中的外观,那么这就是您的问题之一。
  • 错误是正确的。 ShoppingCartTestCases 类没有名为“shop”的属性,除非您先调用 setUp 方法。你确定这已经完成了吗?

标签: python unit-testing


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    错误是正确的。 ShoppingCartTestCases 没有称为“shop”的属性,除非您先调用 setUp 方法。你确定这已经完成了吗?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-04-19
      • 1970-01-01
      • 2010-11-01
      • 2016-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多