[Tutor] Tutor Digest, Vol 157, Issue 24
Alan Gauld
alan.gauld at yahoo.co.uk
Fri Mar 10 12:29:11 EST 2017
On 10/03/17 10:50, Eloka Chima via Tutor wrote:
Please post in plain text. Your email is unreadable...
And please don't reply to the digest, and if you must,
then at least delete the irrelevant bits. We've all
seen these messages already and some people pay
by the byte.
> Thank you so much. Your guidance have been helpful, the bugs have been gotten rid of after I reviewed the code. But 3 out of 6 of the test is correct, the other 3 is incorrect. Take a look at it and tell me where I went wrong.
>>> Below is my new code:
> class ShoppingCart(object): def __init__(self): self.total = 0 self.items = {} def add_item(self, item_name, quantity, price): for item_name in self.items: if not item_name in self.items: self.items["item_name"] = quantity self.total += price return self.total return self.items def remove_item(self, item_name, quantity, price): for item_name in self.items: if item_name in self.items: del self.items["item_name"] self.total -= price else: if quantity > self.items[quantity]: self.items = 0 return self.items def checkout(self, cash_paid): if cash_paid < self.total : self.total -= cash_paid return "Cash paid not enough"class Shop(ShoppingCart): def __init__(self): self.quantity = 100 def remove_item(self): self.quantity -=1 return self.quantity
>>> This is the test for the program:
> 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)
>
>>> I get this test result after running it:
> 1 . test_add_itemFailure in line 20, in test_add_item self.assertEqual(self.cart.total, 30, msg='Cart total not correct after adding items') AssertionError: Cart total not correct after adding items2 . test_checkout_returns_correct_balanceFailure in line 34, in test_checkout_returns_correct_balance self.assertEqual(self.cart.checkout(265), 75, msg='Balance of checkout not correct') AssertionError: Balance of checkout not correct3 . test_remove_itemFailure in line 27, in test_remove_item self.assertEqual(self.cart.total, 10, msg='Cart total not correct after removing item') AssertionError: Cart total not correct after removing item.
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos
More information about the Tutor
mailing list