From the course: Unit Testing and Test Driven Development in Python
Unlock this course with a free trial
Join today to access over 22,600 courses taught by industry experts.
Add multiple items and calculate total - Python Tutorial
From the course: Unit Testing and Test Driven Development in Python
Add multiple items and calculate total
- [Narrator] In this test case, I'm going to add prices for multiple items, add multiple items to the checkout, and then verify the correct total as calculated. I'm in the red phase, so first I'm going to add the failing test case. In this test case, I'll add the price for two items, then actually add two items to the checkout, and verify that the correct total is calculated. I have a failing unit test, so on to the green phase to make it pass. Things start to get a little more complicated with this test, but I want to try and do the simplest thing to get the test to pass. I'll add a Python Dictionary to the checkout class to keep track of the prices per item. Then I'll update "Checkout" to have a total member variable, and to increment the total whenever a new item is added. Then I'll return that total when the "Calculate Total" method is called. Now that my test is passing, I can enter the refactoring phase. Is there anything to refactor, or anything of concern? I don't think so…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
-
(Locked)
Supermarket Checkout Kata overview1m 34s
-
(Locked)
Setup and first test case2m 7s
-
(Locked)
Add items, add items prices, and calculate current total6m 55s
-
(Locked)
Add multiple items and calculate total2m 27s
-
(Locked)
Add and apply discounts13m 31s
-
(Locked)
Throw exception when adding an item with no price2m 5s
-
(Locked)
-
-
-