2017-03-15 2 views
-1

Der Code, den ich schrieb, läuft gut ohne Komponententest. Aber es scheitert, wenn ich es Unit-Test. HierProbleme mit Komponententest in Python

ist der Quellcode

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

Hier wird die Unit-Test-Code:

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

Hier ist die Ausgabe I erhalten, nachdem das Gerät Testprogramm läuft

enter image description here

Vielen Dank im Voraus.

+1

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

+0

Was passiert, wenn Sie alle außer dem fehlgeschlagenen Test löschen? –

+0

Python2 oder Python3 – salparadise

Antwort

0

Dies sollte helfen, das Problem mit der Unterklasse zu lösen:

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

Der Fehler ist korrekt. Die ShoppingCartTestCases hat keine Bezeichnung "Shop", es sei denn, Sie rufen zuerst die Methode setUp auf. Bist du sicher, dass das gemacht wird?