From the course: Unit Testing in Python
Unlock the full course today
Join today to access over 24,900 courses taught by industry experts.
Exceptions - Python Tutorial
From the course: Unit Testing in Python
Exceptions
- [Instructor] Like mentioned earlier, in test driven Python, we want to think about behaviors including unwanted behaviors. In our case, our program should validate its inputs, output results, and actions to be performed. If any of the aforementioned are unexpected, we have two choices. We can make our system explicitly raise an exception so it avoids completing the wrong behavior. This spares your intended end user from encountering errors and avoids malfunctions that could degrade the larger system itself. In other cases, we may want the system to continue code execution while avoiding the unwanted behavior. In this case, the system would log that it didn't complete an action to the user, and continue running. In our case, we will opt for the former, and explicitly raise an exception. To indicate that an exception is to be expected in pytest, you must use a context manager like so. Here, in this test, we can see on lines 10 and 11 this comment that includes the pytest context…