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

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…

Contents