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

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…

Contents