What if your test says two objects are equal but they are actually different in memory?
Why Checking identity (is, is not) in PyTest? - Purpose & Use Cases
Imagine you have a list of user objects and you want to check if two variables point to the exact same user in memory. Doing this by manually comparing every attribute can be confusing and slow.
Manually comparing objects attribute by attribute is slow and error-prone. You might miss subtle differences or mistakenly think two objects are the same when they just look alike. This wastes time and causes bugs.
Using identity checks like is and is not in tests lets you quickly and clearly verify if two variables refer to the exact same object. This is faster, simpler, and less error-prone.
assert user1.id == user2.id and user1.name == user2.nameassert user1 is user2
This lets you confidently test if two variables point to the same object, catching subtle bugs and improving test clarity.
When testing a cache system, you want to confirm that fetching the same user twice returns the exact same object, not just a copy. Identity checks make this easy.
Manual attribute checks are slow and risky.
is and is not check if two variables point to the same object.
Using identity checks makes tests clearer and more reliable.
Practice
is operator check in Python when used in pytest assertions?Solution
Step 1: Understand the
Theisoperator meaningisoperator checks if two variables refer to the same object in memory, not just equal values.Step 2: Differentiate from
The====operator checks if values are equal, butischecks identity (same object).Final Answer:
If two variables point to the exact same object in memory -> Option BQuick Check:
ischecks identity [OK]
is means same object, not just equal value [OK]- Confusing
iswith== - Thinking
ischecks value equality - Using
isfor comparing numbers or strings incorrectly
a and b are not the same object in pytest?Solution
Step 1: Recall correct syntax for identity negation
The correct syntax to check two variables are not the same object isis not, soassert a is not b.Step 2: Identify invalid syntax options
Options B and C use invalid Python syntax. assert a != b uses!=which checks value inequality, not identity.Final Answer:
assert a is not b -> Option AQuick Check:
is notsyntax correct [OK]
is not to check different objects, not != [OK]- Using
!=instead ofis not - Writing
is!which is invalid - Using JavaScript style
!==in Python
obj1 = None obj2 = None assert obj1 is obj2
What will happen when this pytest assertion runs?
Solution
Step 1: Understand None is a singleton
In Python,Noneis a singleton object, so all variables assignedNonepoint to the same object.Step 2: Check identity assertion
The assertionassert obj1 is obj2checks if both variables point to the same object, which they do.Final Answer:
The assertion passes because both are the same None object -> Option CQuick Check:
None is Nonealways True [OK]
- Thinking None creates new objects each time
- Confusing identity with equality
- Expecting assertion failure for None identity
list1 = [1, 2, 3] list2 = [1, 2, 3] assert list1 is list2
Solution
Step 1: Understand list object identity
Two lists with same values are different objects in memory unless explicitly assigned.Step 2: Check assertion behavior
The assertionassert list1 is list2fails becauseischecks identity, not equality.Final Answer:
Assertion fails because lists are different objects despite same values -> Option DQuick Check:
Same values ≠ same object [OK]
== for value equality, is for identity [OK]- Expecting
isto check value equality - Confusing object identity with content equality
- Assuming lists with same content share identity
config every time it is called. Which assertion is best?Solution
Step 1: Understand singleton identity check
To confirm the function returns the exact same object, useisto check identity.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.Final Answer:
assert get_config() is config -> Option AQuick Check:
isfor singleton identity [OK]
is to confirm same singleton object returned [OK]- Using
==which checks value, not identity - Using
is notby mistake - Not understanding singleton pattern in tests
Ready to prove it?
Pass the test and this pattern moves unknown
