Bird
0
0

Consider this pytest test:

hard🚀 Application Q9 of 15
PyTest - Writing Assertions
Consider this pytest test:
def test_identity():
    a = [1, 2]
    b = a.copy()
    assert a is not b
    assert a == b

What is the purpose of these assertions?
ATo check that <code>a</code> and <code>b</code> are different objects but have equal values
BTo check that <code>a</code> and <code>b</code> are the same object and have equal values
CTo check that <code>a</code> and <code>b</code> are different objects and different values
DTo check that <code>a</code> and <code>b</code> are the same object but different values
Step-by-Step Solution
Solution:
  1. Step 1: Understand copy() behavior

    b = a.copy() creates a new list object with the same values as a.
  2. Step 2: Analyze assertions

    assert a is not b confirms different objects; assert a == b confirms equal values.
  3. Final Answer:

    To check that a and b are different objects but have equal values -> Option A
  4. Quick Check:

    Copy creates equal but distinct objects = B [OK]
Quick Trick: copy() creates equal but distinct objects [OK]
Common Mistakes:
MISTAKES
  • Assuming copy returns the same object
  • Confusing 'is' and '=='
  • Expecting identity for copied objects

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PyTest Quizzes