Building a Shopping Cart with Python and Pytest
Introduction to the Shopping Cart Module
Welcome to your third unit for this course dedicated to practicing Test Driven Development (TDD) utilizing Python and pytest. We're going to start building a new system; this time, we'll create a ShoppingCart class with multiple features.
This course emphasizes hands-on practice, where you'll receive requirements through tests, one at a time. Your task is to write tests AND implement code that makes each test pass, simulating a real-world TDD environment. Previously, tests were provided for you, but this time, it's all up to you!
Remember to use the core concepts of the Red-Green-Refactor cycle while completing these coding exercises. I'm still here to help! Just ask if you encounter issues.
Requirements for `ShoppingCart` Class
In this section, we'll explore the core functionality requirements for our ShoppingCart class. Each requirement builds incrementally on previous ones, following TDD principles: first establishing basic cart initialization, then adding single and multiple items, managing quantities, and finally implementing item removal. These features cover fundamental e-commerce cart operations, providing a comprehensive foundation as we practice test-driven development by writing both tests and implementation code for each requirement.
1. Starting with an Empty Cart
- Description: When a new shopping cart is created, it should start without any items, and the total price should be zero.
- Details
- Initialize a new shopping cart using the
ShoppingCartconstructor - Ensure
get_item_count()returns0for an empty cart - Verify
get_total()returns0for the initial state.
- Initialize a new shopping cart using the
- Examples: A newly created cart should have a count of
0and a total of0.
2. Adding a Single Item
- Description: Verify that when a single item is added to the cart, the cart's item count increases, and the total price reflects the added item's price.
- Details
- Use the
add_item(item)method to add an item to the cart. - Confirm that
get_item_count()returns the correct number of items after an item is added. - Ensure
get_total()accurately calculates and returns the total price of items in the cart.
- Use the
- Examples: Adding an item
{'id': '1', 'name': 'Book', 'price': 10}should result in an item count of1and a total cost of10.
