Bird
0
0

Find the mistake in this pytest assertion:

medium📝 Debug Q7 of 15
PyTest - Writing Assertions
Find the mistake in this pytest assertion:
a = 1000
b = 1000
assert a is b
ANo mistake; assertion passes
BAssertion fails because integers above 256 are different objects
CSyntax error in assertion
DRuntime error due to integer comparison
Step-by-Step Solution
Solution:
  1. Step 1: Understand integer caching

    Python caches small integers (-5 to 256), so integers above 256 are different objects even if values match.
  2. Step 2: Evaluate the assertion

    assert a is b fails because a and b are different objects.
  3. Final Answer:

    Assertion fails because integers above 256 are different objects -> Option B
  4. Quick Check:

    Integer identity above 256 fails = C [OK]
Quick Trick: Integers >256 are distinct objects; 'is' fails [OK]
Common Mistakes:
MISTAKES
  • Assuming all integers are identical objects
  • Confusing 'is' with '==' for numbers
  • Expecting assertion to pass for large integers

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PyTest Quizzes