修复python/nose2/bin/nose2:未找到

修复python/nose2/bin/nose2:未找到,python,nose2,Python,Nose2,我正在进行面向对象的测试,希望我能创建一个购物车 我编写的代码通过了unittest的所有测试,但是当我尝试提交时,我得到了这个错误/bug /bin/sh:1:python/nos2/bin/nos2:未找到 下面我展示了我的代码和unittest。 统一的 import unittest class ShoppingCartTestCases(unittest.TestCase): def setUp(self): self.cart = ShoppingCart()

我正在进行面向对象的测试,希望我能创建一个购物车

我编写的代码通过了unittest的所有测试,但是当我尝试提交时,我得到了这个错误/bug

/bin/sh:1:python/nos2/bin/nos2:未找到

下面我展示了我的代码和unittest。 统一的

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)
麦可德

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
类ShoppingCart(对象):
定义初始化(自):
self.total=0
self.items={}
def添加项目(自身、项目名称、数量、价格):
如果项目名称和数量>=1:
self.items.update({item\u name:quantity})
如果数量和价格>=1:
self.total+=(数量*价格)
def删除项目(自身、项目名称、数量、价格):
self.total-=(数量*价格)
如果数量>=自身项目[项目名称]:
尝试:
删除自身项目[项目名称]
除了(KeyError、ValueError):
一无所获
self.items[项目名称]-=数量
def结帐(自助、现金支付):
余额=0
如果支付的现金
问题的根本原因在于unittest类中的setUp()方法。因此,在python开发环境(如IDLE)上运行unittest类会进一步暴露这个问题。 完全错误实际上是这样的

“Desktop/practice python/shoppingcarttestcases.py”,第4行,在安装程序中 self.cart=ShoppingCart() NameError:未定义全局名称“ShoppingCart”

解决方案可能在于正确定义/继承/实现类购物车

我希望这能缩小范围。 如果你觉得这有帮助,你得到了一个解决方案,请张贴在论坛上以及