0
0
PyTesttesting~10 mins

Checking identity (is, is not) in PyTest - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test checks if two variables point to the exact same object in memory using is and is not operators. It verifies identity, not just equality.

Test Code - pytest
PyTest
import pytest

def test_identity():
    a = [1, 2, 3]
    b = a
    c = [1, 2, 3]

    # Check that a and b are the same object
    assert a is b

    # Check that a and c are not the same object
    assert a is not c

    # Check that a and c have equal content
    assert a == c
Execution Trace - 8 Steps
StepActionSystem StateAssertionResult
1Test startspytest test runner initialized-PASS
2Assign list [1, 2, 3] to variable aVariable a points to a list object in memory-PASS
3Assign variable b to reference the same object as aVariables a and b point to the same list object-PASS
4Assign variable c to a new list [1, 2, 3]Variable c points to a different list object with same content-PASS
5Assert that a is b (identity check)a and b reference the same objectassert a is bPASS
6Assert that a is not c (identity check)a and c reference different objectsassert a is not cPASS
7Assert that a == c (equality check)a and c have equal contentassert a == cPASS
8Test endsAll assertions passed-PASS
Failure Scenario
Failing Condition: If the assertion 'assert a is b' fails because a and b do not reference the same object
Execution Trace Quiz - 3 Questions
Test your understanding
What does the assertion 'assert a is b' verify in this test?
AThat a and b are different objects
BThat a and b have the same content
CThat a and b point to the exact same object in memory
DThat a and b are both lists
Key Result
Use 'is' and 'is not' to check if two variables point to the exact same object, not just if they look equal. This helps catch bugs where object identity matters.