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.
Throw exception when adding an item with no price - Python Tutorial
From the course: Unit Testing and Test Driven Development in Python
Throw exception when adding an item with no price
- [Instructor] In this lecture, I'll implement the last test case, throwing an exception if an item is added to the checkout which does not have a price defined. I'm in the red phase, so I'm going to start by writing a failing unit test. I'll implement a test which tries to add an item which hasn't had a price specified, then use the pytest.raises helper to verify that an exception is thrown. Okay, now that I've got my failing unit test, I'm going to the green phase and I'm going to make it pass. I'll make this pass by modifying the addItem method to check if the passed in item has a price specified. If it doesn't, then I'll throw the exception. Great, my unit test is passing. Now it's time for the refactor phase. Is there anything to refactor? I didn't add much, so I think this is still okay, although there is some cleanup that could be done to make these methods smaller and cleaner. This ends the hands-on TDD session with the checkout kata. In the next lecture, we'll start a new…
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)
-
-
-