Framework Mode - Checking exception attributes
Folder Structure
tests/ ├── test_example.py ├── conftest.py utilities/ ├── helpers.py pytest.ini requirements.txt
tests/ ├── test_example.py ├── conftest.py utilities/ ├── helpers.py pytest.ini requirements.txt
test_example.py where tests are written using pytest syntax.conftest.py holds reusable fixtures and setup/teardown code.utilities/helpers.py to support tests.pytest.ini for pytest settings and options.pytest.ini to set default options like markers, test paths, and logging.conftest.py fixtures.pytest-html for detailed HTML reports.pytest.raises() as a context manager to catch exceptions.as exc_info to check attributes like message or custom fields.Where would you add a new test that verifies a custom attribute of a raised exception in this framework structure?
pytest.raises?pytest.raises with as exc_infoexc_info.valueexc_info.value, allowing attribute checks like the message.with pytest.raises(ValueError) as exc_info: and then check exc_info.value. -> Option Dexc_info.valueexc_info.value.str(exc_info.value) returns the exception message text.def divide(x, y):
if y == 0:
raise ZeroDivisionError('Cannot divide by zero')
return x / y
def test_divide():
with pytest.raises(ZeroDivisionError) as exc_info:
divide(5, 0)
assert str(exc_info.value) == 'Cannot divide by zero'pytest.raises captures the exception, and str(exc_info.value) matches the expected message.def test_error():
with pytest.raises(ValueError) as exc_info:
raise ValueError('Invalid input')
assert exc_info.message == 'Invalid input'exc_info.message, but must be accessed via str(exc_info.value).assert exc_info.message == 'Invalid input' with assert str(exc_info.value) == 'Invalid input'.exc_info.message instead of str(exc_info.value) to check message. -> Option AMyError that has an attribute code. Which pytest code correctly checks both the exception type and the code attribute value?as exc_infocode on exc_info.valueexc_info.value, so exc_info.value.code accesses the attribute.Pass the test and this pattern moves unknown