点击查看代码
#shopping_list.py
class ShoppingList:
#shopping_list是一个字典结构
def __init__(self,shopping_list):
self.shopping_list = shopping_list
def get_item_count(self):
return len(self.shopping_list)
def get_total_price(self):
total_price = 0
for price in self.shopping_list.values():
total_price += price
return total_price
点击查看代码
#test测试类
import unittest
from shopping_list import ShoppingList
class TestShoppingLit:
def setUp(self):
self.shopping_list = ShoppingList({"纸巾":8,"帽子":30,"拖鞋":15})
def test_get_item_count(self):
self.assertEqual(self.shopping_list.get_item_count(),3)
def test_get_total_price(self):
self.assertEqual(self.shopping_list.get_total_price(),53)