2017-03-17 65 views
0

我正在OOP中進行測試,我期望創建一個ShoppingCart。修復python/nose2/bin/nose2:找不到

我寫我的代碼通過了所有測試單元測試從,但是當我試圖提交,我得到這個錯誤/缺陷

/bin/sh: 1: python/nose2/bin/nose2: not found

下面我已經表明我的代碼和單元測試。 Unittiest

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) 

mycode的

class ShoppingCart(object): 

    def __init__(self): 
     self.total = 0 
     self.items = {} 

    def add_item(self, item_name, quantity, price): 
     if item_name 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) 
     if quantity >= self.items[item_name]: 
      try: 
       del self.items[item_name] 
      except (KeyError, ValueError): 
       return None 
     self.items[item_name] -= quantity 

    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): 
     ShoppingCart.__init__(self) 
     self.quantity = 100 

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

回答

0

問題的根本原因在於在單元測試類的設置()方法。因此,在python開發環境(如IDLE)上運行unittest類會進一步暴露問題。 完整的錯誤實際上是這樣的

「桌面/實踐蟒蛇/ shoppingcarttestcases.py」 self.cart =購物車() NameError,4號線,在設置:全局名稱「我的購物」沒有定義」

如果你有所幫助,你會得到一個解決方案,請在論壇發貼以及該解決方案可能在於圍繞正確定義/繼承/實現類的購物車。

我希望這個範圍縮小。 。

+0

什麼foru你指的是? –

相關問題