Bird
0
0

What will happen when this test runs?

medium📝 Predict Output Q5 of 15
PyTest - Writing Assertions
What will happen when this test runs?
import pytest
def test_value_error():
    with pytest.raises(ValueError):
        int('abc')
ATest passes because int('abc') raises ValueError
BTest fails because int('abc') returns 0
CTest fails due to TypeError
DTest passes but logs a warning
Step-by-Step Solution
Solution:
  1. Step 1: Check what int('abc') does

    int('abc') raises ValueError because 'abc' is not a valid integer string.
  2. Step 2: Match exception with pytest.raises

    pytest.raises expects ValueError, which matches the raised exception, so test passes.
  3. Final Answer:

    Test passes because int('abc') raises ValueError -> Option A
  4. Quick Check:

    Matching exception = test passes [OK]
Quick Trick: Exception type must match exactly for pytest.raises to pass [OK]
Common Mistakes:
MISTAKES
  • Assuming int('abc') returns 0
  • Confusing ValueError with TypeError
  • Expecting test to fail when exception occurs

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PyTest Quizzes