Recall & Review
beginner
What is the purpose of testing multiple exceptions in pytest?
To verify that a function or code block raises any one of several expected exceptions under different error conditions.
Click to reveal answer
beginner
How do you test for multiple exceptions using pytest?
Use the
pytest.raises() context manager with a tuple of exception types, like with pytest.raises((TypeError, ValueError)):.Click to reveal answer
beginner
What happens if the code inside
pytest.raises() does not raise any exception?The test fails because pytest expects an exception to be raised inside the
with pytest.raises() block.Click to reveal answer
intermediate
Can you catch multiple exceptions separately in pytest tests?
Yes, but you write separate
with pytest.raises() blocks for each exception to test them individually.Click to reveal answer
beginner
Example: How to test a function that may raise either
ValueError or TypeError?Use
with pytest.raises((ValueError, TypeError)): and call the function inside this block to check if it raises either exception.Click to reveal answer
Which pytest syntax correctly tests for multiple exceptions?
✗ Incorrect
Use a tuple of exceptions inside pytest.raises(), like (ValueError, TypeError).
What happens if no exception is raised inside a pytest.raises() block?
✗ Incorrect
pytest.raises expects an exception; if none is raised, the test fails.
How to test two different exceptions separately in pytest?
✗ Incorrect
Separate pytest.raises() blocks test each exception individually.
Which of these is NOT a valid exception to test with pytest.raises()?
✗ Incorrect
Only exception classes can be tested, not data types like int.
Why test multiple exceptions in one pytest.raises()?
✗ Incorrect
Testing multiple exceptions ensures the code raises at least one expected error.
Explain how to use pytest to test that a function raises either ValueError or TypeError.
Think about how pytest.raises accepts multiple exceptions.
You got /4 concepts.
Describe what happens when no exception is raised inside a pytest.raises() block and why this is important.
Consider the purpose of testing exceptions.
You got /3 concepts.