How do you test that a Python function throws an exception?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
In Python, exceptions are events that can modify the flow of control through a program. In many instances, especially in large software projects, ensuring that a function reacts to inputs or situations by raising an exception can be just as important as ensuring it returns the right output. Testing for these exceptions is therefore a critical part of software testing.
Understanding Exceptions in Python
Before delving into testing exceptions, it's crucial to understand what exceptions are. In Python, exceptions are special objects that the program creates when it encounters an error. When an error occurs, Python creates an appropriate exception object and "raises" it. If this raised exception is not "caught" or handled, the program will terminate with an error message.
Using try and except Blocks
A basic method to handle exceptions in Python is through the use of try and except blocks. Here's a quick example:
In the example above, if y is zero, a ZeroDivisionError is raised, and the code in the except block is executed.
Testing Exceptions Using unittest
The unittest framework in Python, which is built into the standard library, provides a way to test whether a function raises an exception as expected using assertRaises. Here's how you can use it:
In the code above, assertRaises(ValueError) checks that ValueError is raised when function_that_raises() is executed. The with statement is used here to wrap the execution of the function within a context managed by assertRaises.
Using pytest to Test Exceptions
Another popular Python testing framework is pytest, which can also be used to assert that exceptions are raised. Here's how you can test exceptions in pytest:
The pytest.raises works similarly to unittest's assertRaises.
What to Test for When Testing Exceptions
When testing exceptions, consider the following:
- Type of Exception: Ensure the exception is specific and informative.
- Message Content: Sometimes checking the message of the exception can also be critical especially if the messages are meaningful to the application's users.
- State After Exception: Check if necessary cleanup or state reset happens after an exception is raised.
Summary Table
Here's a summary table to differentiate how exceptions are handled and tested in unittest and pytest:
| Feature | unittest | pytest |
| Framework | Part of Python's standard library | Third-party module, widely used |
| Syntax | Use assertRaises | Use raises |
| Context Management | Use with a with block | Use with a with block |
| Exception Type Testing | Supported | Supported |
| Exception Message Testing | Requires a different method | Directly supported |
Additional Points
- When writing tests for exceptions, the clarity and specificity of the testing code are as important as the application code.
- Consider using
assertRaisesRegexfromunittestto match a regular expression to the exception message for more finely grained tests. - Always document exception-raising behaviors in both the function's docstring and the test suite to ensure consistency and clarity for future maintenance.
Testing exceptions robustly can save countless hours of debugging and potential issues in production, making it a vital skill for Python developers.

