2017-03-15 106 views
-1

我寫的代碼運行良好,沒有單元測試。但是當我單元測試時卻失敗了。在python中有單元測試問題

這裏是源代碼

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

後我得到

非常感謝你提前。

+1

https://stackoverflow.com/help/how-to-ask – wom

+0

如果刪除所有失敗的測試,會發生什麼情況? –

+0

python2或python3 – salparadise

回答

0

這應該有助於解決你的子類的問題:

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

的錯誤是正確的。除非先調用setUp方法,否則ShoppingCartTestCases沒有稱爲「商店」。你確定這樣做?