Bird
0
0

Which of the following correctly describes this test code?

medium📝 Debug Q7 of 15
PyTest - Writing Assertions
Which of the following correctly describes this test code?
import pytest
def test_type_error():
    with pytest.raises(TypeError):
        sum(5)
Apytest.raises is used incorrectly without 'with'
Bsum(5) does not raise any exception
Csum(5) raises TypeError because sum expects an iterable
DTypeError is not the correct exception
Step-by-Step Solution
Solution:
  1. Step 1: Understand sum(5) behavior

    sum expects an iterable, but 5 is an int, so sum(5) raises TypeError.
  2. Step 2: Check pytest.raises usage

    pytest.raises expects TypeError, which matches the raised exception, so usage is correct.
  3. Final Answer:

    sum(5) raises TypeError because sum expects an iterable -> Option C
  4. Quick Check:

    Exception matches pytest.raises = test passes [OK]
Quick Trick: Check function argument types to predict exceptions [OK]
Common Mistakes:
MISTAKES
  • Assuming sum(5) returns 5
  • Thinking TypeError is wrong exception here
  • Misusing pytest.raises without 'with'

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PyTest Quizzes