Testing for Exceptions
Veeg om het menu te tonen
An exception is an event that occurs during program execution that disrupts the normal flow of instructions.
Testing for exceptions is a crucial part of unit testing because robust code must handle errors gracefully. When you anticipate that certain inputs or situations should trigger an exception, writing tests to confirm this behavior helps you ensure your code fails safely and predictably, rather than crashing unexpectedly or producing incorrect results.
divide.py
test_divide.py
In the code sample above, assertRaises is used to verify that dividing by zero in the divide function correctly raises a ZeroDivisionError. The test passes only if the exception is raised within the context manager block, confirming that the code responds as expected to invalid input.
get_item.py
test_get_item.py
Bedankt voor je feedback!
Vraag AI
Vraag AI
Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.