O'Reilly logo

Python Testing Cookbook. by Bhaskar N. Das, Greg L. Turnquist

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

How to do it...

  1. Create a new file named recipe14.py to store the code for this recipe.
  2. Create a shopping cart application that we can build some tests around:
class ShoppingCart(object):   def __init__(self):     self.items = []    def add(self, item, price):     self.items.append(Item(item, price))     return self   def item(self, index):     return self.items[index-1].item   def price(self, index):     return self.items[index-1].price   def total(self, sales_tax):     sum_price = sum([item.price for item in self.items])     return sum_price*(1.0 + sales_tax/100.0)   def __len__(self):     return len(self.items)class Item(object):   def __init__(self, item, price):     self.item = item     self.price = price
  1. Create a test case that contains several test methods, including one deliberately ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required