Complete the code to check if the function raises ValueError.
import pytest def test_raise_custom_exception(): with pytest.raises([1]): raise ValueError('Error!')
pytest.raises() to catch exceptions.The pytest.raises() expects the exception type that the code inside the block will raise. Here, ValueError is raised, so the test passes only if ValueError is used.
Complete the code to define a custom exception class.
class [1](Exception): pass
Exception.Custom exception classes should be named clearly and start with a capital letter. CustomError is a good descriptive name.
Fix the error in the test to correctly check for the custom exception.
import pytest class MyError(Exception): pass def test_my_error(): with pytest.raises([1]): raise MyError('Oops!')
The exception class name is case-sensitive. The correct name is MyError with capital E.
Fill both blanks to raise and test a custom exception with a message.
import pytest class CustomError(Exception): pass def test_custom_error_message(): with pytest.raises([1]) as exc_info: raise CustomError([2]) assert str(exc_info.value) == 'Error occurred'
pytest.raises().The test checks that CustomError is raised with the exact message 'Error occurred'. The pytest.raises() captures the exception to verify its message.
Fill all three blanks to define, raise, and test a custom exception with a message.
import pytest class [1](Exception): pass def test_[2](): with pytest.raises([3]) as exc: raise CustomException('Failed') assert str(exc.value) == 'Failed'
The class name is CustomException. The test function name is test_custom_exception (snake_case). The exception caught is CustomException. This matches the raised exception and message.