Framework Mode - Matching exception messages
Folder Structure
tests/ ├── test_example.py ├── test_utils.py ├── conftest.py utilities/ ├── helpers.py configs/ ├── config.yaml reports/ ├── latest_report.html
tests/ ├── test_example.py ├── test_utils.py ├── conftest.py utilities/ ├── helpers.py configs/ ├── config.yaml reports/ ├── latest_report.html
test_example.py where tests assert exception messages using pytest.raises().configs/config.yaml or conftest.py fixtures.conftest.py to define fixtures for test setup and teardown.configs/config.yaml.--tb=short for concise tracebacks.pytest-html to generate readable HTML reports showing exception messages.pytest.raises() context manager to catch exceptions and assert their messages.Where in this folder structure would you add a helper function to validate exception messages with regex?
pytest.raises() in a test?pytest.raises() function is used to verify that a block of code raises a specific exception.exc_info object stores the caught exception. To get its message as a string, use str(exc_info.value).assert 'error' in str(exc_info.value). Directly accessing exc_info.value without converting to string won't work.def test_divide_by_zero():
with pytest.raises(ZeroDivisionError) as exc_info:
x = 1 / 0
assert 'division by zero' in str(exc_info.value)ZeroDivisionError as expected.ZeroDivisionError in Python includes 'division by zero', so the assertion passes.def test_key_error():
with pytest.raises(KeyError) as exc_info:
d = {'a': 1}
value = d['b']
assert 'b' in exc_info.valued['b'] raises a KeyError because 'b' is not in the dictionary.exc_info.value directly, but exc_info.value is an exception object, not a string. It should be converted to string first.ValueError with message "Invalid input: 0" when input is zero. Which pytest test code correctly verifies both exception type and exact message?args tuple; the first element args[0] holds the message string.exc_info.value.args[0] allows exact message comparison. Using str(exc_info.value) works but may include extra formatting. exc_info.value.message does not exist.Pass the test and this pattern moves unknown