Recall & Review
beginner
What is a custom exception in Python testing?
A custom exception is a user-defined error type created by subclassing the built-in Exception class. It helps to signal specific error conditions in your code.
Click to reveal answer
beginner
How do you test that a custom exception is raised using pytest?
Use pytest.raises() as a context manager around the code that should raise the exception. Then assert the exception type matches your custom exception.
Click to reveal answer
beginner
Example: Write a pytest snippet to check if MyError is raised when calling func().
import pytest
with pytest.raises(MyError):
func()Click to reveal answer
intermediate
Why create custom exceptions instead of using built-in exceptions?
Custom exceptions make your error handling clearer and more specific. They help other developers understand what kind of error occurred and allow targeted exception handling.
Click to reveal answer
intermediate
What is the benefit of checking the exception message in tests?
Checking the exception message ensures the error is not only the right type but also triggered for the expected reason, improving test accuracy.
Click to reveal answer
Which pytest function is used to test if a custom exception is raised?
✗ Incorrect
pytest.raises() is the correct function to check if an exception is raised in pytest.
How do you define a custom exception in Python?
✗ Incorrect
Custom exceptions are defined by subclassing Exception, like 'class MyError(Exception): pass'.
What is the purpose of testing exception messages?
✗ Incorrect
Testing messages confirms the exception was raised for the expected reason.
Which of these is NOT a benefit of custom exceptions?
✗ Incorrect
Custom exceptions help with clarity and debugging but do not fix bugs automatically.
In pytest, what happens if the expected exception is NOT raised inside pytest.raises()?
✗ Incorrect
If the expected exception is not raised, pytest.raises() causes the test to fail.
Explain how to write a pytest test that checks for a custom exception.
Think about how you catch errors in real life and how pytest helps you check for them.
You got /4 concepts.
Why is it useful to create and test custom exceptions in your code?
Consider how specific error messages help you fix problems faster.
You got /4 concepts.