Framework Mode - Testing multiple exceptions
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 multiple exceptions are tested using pytest.helpers.py.conftest.py for shared fixtures and setup/teardown logic.pytest.ini to configure test markers and default options.conftest.py fixtures.--tb=short for concise tracebacks.pytest-html for HTML reports.pytest.raises() context manager to assert exceptions.Where would you add a new test function that checks if a function raises ValueError and TypeError in this framework structure?
pytest.raises((ValueError, TypeError)) do in a test?def test_func():
with pytest.raises((KeyError, IndexError)):
d = {'a': 1}
value = d['b']
def test_divide():
with pytest.raises((ZeroDivisionError, ValueError)):
result = 10 / 0
int('abc')
IndexError or KeyError depending on input. Which pytest test code correctly checks both exceptions in one test?Pass the test and this pattern moves unknown