Bird
Raised Fist0
PyTesttesting~20 mins

Checking identity (is, is not) in PyTest - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Identity Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this pytest assertion?
Consider the following pytest test function. What will be the result of running this test?
PyTest
def test_identity():
    a = [1, 2, 3]
    b = a
    assert a is b
ATypeError at runtime
BTest fails with AssertionError
CSyntaxError at runtime
DTest passes
Attempts:
2 left
💡 Hint
Remember that 'is' checks if two variables point to the same object in memory.
❓ Predict Output
intermediate
2:00remaining
What happens when comparing two identical lists with 'is'?
What will be the result of this pytest test function?
PyTest
def test_identity_lists():
    a = [1, 2, 3]
    b = [1, 2, 3]
    assert a is not b
ASyntaxError at runtime
BTest passes
CNameError at runtime
DTest fails with AssertionError
Attempts:
2 left
💡 Hint
Two lists with the same content are different objects unless assigned explicitly.
❓ assertion
advanced
2:00remaining
Which assertion correctly checks that two variables do NOT refer to the same object?
You want to write a pytest assertion to confirm that variables 'x' and 'y' are different objects. Which option is correct?
Aassert x is not y
Bassert x != y
Cassert x is y
Dassert x == y
Attempts:
2 left
💡 Hint
Remember 'is not' checks identity, while '!=' checks value equality.
🔧 Debug
advanced
2:00remaining
Why does this pytest assertion fail unexpectedly?
Given the code below, why does the assertion fail?
PyTest
def test_identity_failure():
    a = 256
    b = 256
    assert a is b

def test_identity_failure2():
    x = 257
    y = 257
    assert x is y
AThe assertion fails because 'is' compares values, not objects
BThe assertion fails because variables 'x' and 'y' are not defined
CPython caches small integers, so 'a is b' is True for 256 but not for 257
DThe assertion fails due to a syntax error in the code
Attempts:
2 left
💡 Hint
Python caches small integers from -5 to 256 for performance.
❓ framework
expert
3:00remaining
How to write a pytest test that checks identity and reports a clear message on failure?
You want to write a pytest test that asserts two variables 'obj1' and 'obj2' are the same object. If they are not, the test should fail with the message: "Objects are not identical". Which code snippet achieves this?
A
def test_obj_identity():
    assert obj1 is obj2, "Objects are not identical"
B
def test_obj_identity():
    assert obj1 == obj2, "Objects are not identical"
C
def test_obj_identity():
    assert obj1 is not obj2, "Objects are not identical"
D
def test_obj_identity():
    assert obj1 != obj2, "Objects are not identical"
Attempts:
2 left
💡 Hint
Use 'is' for identity and provide a message after the comma in assert.

Practice

(1/5)
1. What does the is operator check in Python when used in pytest assertions?
easy
A. If two variables have the same value but different objects
B. If two variables point to the exact same object in memory
C. If two variables are equal using the == operator
D. If two variables are both integers

Solution

  1. Step 1: Understand the is operator meaning

    The is operator checks if two variables refer to the same object in memory, not just equal values.
  2. Step 2: Differentiate from ==

    The == operator checks if values are equal, but is checks identity (same object).
  3. Final Answer:

    If two variables point to the exact same object in memory -> Option B
  4. Quick Check:

    is checks identity [OK]
Hint: Remember: is means same object, not just equal value [OK]
Common Mistakes:
  • Confusing is with ==
  • Thinking is checks value equality
  • Using is for comparing numbers or strings incorrectly
2. Which of the following is the correct syntax to assert two variables a and b are not the same object in pytest?
easy
A. assert a is not b
B. assert a !== b
C. assert a is! b
D. assert a != b

Solution

  1. Step 1: Recall correct syntax for identity negation

    The correct syntax to check two variables are not the same object is is not, so assert a is not b.
  2. Step 2: Identify invalid syntax options

    Options B and C use invalid Python syntax. assert a != b uses != which checks value inequality, not identity.
  3. Final Answer:

    assert a is not b -> Option A
  4. Quick Check:

    is not syntax correct [OK]
Hint: Use is not to check different objects, not != [OK]
Common Mistakes:
  • Using != instead of is not
  • Writing is! which is invalid
  • Using JavaScript style !== in Python
3. Given the code:
obj1 = None
obj2 = None

assert obj1 is obj2

What will happen when this pytest assertion runs?
medium
A. SyntaxError due to wrong assertion
B. The assertion fails because None is not an object
C. The assertion passes because both are the same None object
D. The assertion fails because obj1 and obj2 are different None instances

Solution

  1. Step 1: Understand None is a singleton

    In Python, None is a singleton object, so all variables assigned None point to the same object.
  2. Step 2: Check identity assertion

    The assertion assert obj1 is obj2 checks if both variables point to the same object, which they do.
  3. Final Answer:

    The assertion passes because both are the same None object -> Option C
  4. Quick Check:

    None is None always True [OK]
Hint: Remember: None is always the same object in Python [OK]
Common Mistakes:
  • Thinking None creates new objects each time
  • Confusing identity with equality
  • Expecting assertion failure for None identity
4. Identify the error in this pytest assertion:
list1 = [1, 2, 3]
list2 = [1, 2, 3]
assert list1 is list2
medium
A. RuntimeError because lists cannot be compared with is
B. No error; assertion passes because lists have same values
C. SyntaxError due to wrong use of is
D. Assertion fails because lists are different objects despite same values

Solution

  1. Step 1: Understand list object identity

    Two lists with same values are different objects in memory unless explicitly assigned.
  2. Step 2: Check assertion behavior

    The assertion assert list1 is list2 fails because is checks identity, not equality.
  3. Final Answer:

    Assertion fails because lists are different objects despite same values -> Option D
  4. Quick Check:

    Same values ≠ same object [OK]
Hint: Use == for value equality, is for identity [OK]
Common Mistakes:
  • Expecting is to check value equality
  • Confusing object identity with content equality
  • Assuming lists with same content share identity
5. You want to write a pytest test to confirm that a function returns the exact same singleton object config every time it is called. Which assertion is best?
hard
A. assert get_config() is config
B. assert get_config() == config
C. assert get_config() is not config
D. assert get_config() != config

Solution

  1. Step 1: Understand singleton identity check

    To confirm the function returns the exact same object, use is to check identity.
  2. Step 2: Evaluate options

    assert get_config() == config checks value equality, which may be true for different objects. assert get_config() is config correctly asserts identity. Options C and D check for difference, which is not desired.
  3. Final Answer:

    assert get_config() is config -> Option A
  4. Quick Check:

    is for singleton identity [OK]
Hint: Use is to confirm same singleton object returned [OK]
Common Mistakes:
  • Using == which checks value, not identity
  • Using is not by mistake
  • Not understanding singleton pattern in tests
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard