我在python oop中有一個實驗室測試。用Python創建一個購物車OOP
創建一個名爲
ShoppingCart
的類。創建一個構造函數沒有參數,設置
total
屬性爲零,並初始化名爲items
一個空的字典屬性。創建一個方法
add_item
需要item_name
,quantity
和price
參數。此方法應將添加項目 的成本添加到當前總值中。它還應該添加一個條目到 項目字典,這樣的關鍵是item_name
和價值是 數量的項目。創建一個方法
remove_item
需要與add_item
類似的參數。它應該刪除已添加到 購物車中且不需要的商品。此方法應從當前總計中扣除已刪除項目的成本 ,並相應地更新項目 字典。如果要移除的物品的數量超過購物車中該物品的當前數量 ,則假定該物品的所有條目均爲 將被移除。
創建方法
checkout
需要cash_paid
並從付款中返回 餘額值。如果cash_paid
不足以覆蓋總額 ,則返回「支付的現金不足」。創建一個名爲
Shop
類,有一個構造函數沒有 參數並初始化名爲quantity
屬性爲100。請 確保店鋪從ShoppingCart
繼承。在Shop類中,重寫remove_item方法,例如調用 Shop不帶參數的remove_item按數量遞減。
這裏是我的代碼
class ShoppingCart(object):
def __init__(self):
self.total = 0
self.items = dict()
def add_item(self, item_name, quantity, price):
if item_name != None and quantity >= 1:
self.items.update({item_name: quantity})
if quantity and price >= 1:
self.total += (quantity * price)
def remove_item(self, item_name, quantity, price):
self.total -= (quantity * price)
try:
if quantity >= self.items[item_name]:
self.items.pop(item_name, None)
self.items[item_name] -= quantity
except(KeyError, RuntimeError):
pass
def checkout(self, cash_paid):
balance = 0
if cash_paid < self.total:
return "Cash paid not enough"
balance = cash_paid - self.total
return balance
class Shop(ShoppingCart):
def __init__(self):
self.quantity = 100
def remove_item(self):
self.quantity -= 1
和代碼通過了所有測試用例的單元測試
import unittest
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)
。但是當我嘗試提交時,它會失敗,並說它不符合所有測試規範。我已經加倍檢查了它,發現了可能出現的任何錯誤,但仍然一樣。我不知道我錯過了什麼。有沒有解決的辦法?
「提交」 在哪裏?有什麼辦法可以自己嘗試嗎?平臺(或任何你試圖提交你的代碼)給你什麼錯誤信息? –
如果您確定單元測試與提交工作時正在運行的單元測試相同,那麼您最好與負責運行實驗室的人員進行覈對。如果你不確定,那麼與他們覈對也是一件好事。 – bouteillebleu