Bird
0
0

What will be the output of the following test code?

medium📝 Predict Output Q13 of 15
PyTest - Writing Assertions
What will be the output of the following test code?
import pytest

def test_zero_division():
    with pytest.raises(ZeroDivisionError) as exc_info:
        1 / 0
    assert 'division by zero' in str(exc_info.value)
ATest passes because ZeroDivisionError is raised and message matches
BTest fails because exception message is not checked correctly
CTest fails because ZeroDivisionError is not raised
DSyntaxError due to incorrect use of pytest.raises
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the code behavior

    The code inside the with pytest.raises(ZeroDivisionError) as exc_info: block raises a ZeroDivisionError by dividing 1 by 0.
  2. Step 2: Check the assertion

    The assertion verifies that the exception message contains 'division by zero', which is true for this error.
  3. Final Answer:

    Test passes because ZeroDivisionError is raised and message matches -> Option A
  4. Quick Check:

    Exception raised and message checked = D [OK]
Quick Trick: Use exc_info.value to check exception message text [OK]
Common Mistakes:
MISTAKES
  • Not using 'as exc_info' to capture exception details
  • Checking wrong exception message text
  • Expecting no exception raised

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PyTest Quizzes