0
0
PyTesttesting~10 mins

Checking membership (in, not in) in PyTest - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test checks if certain items are present or absent in a list using Python's in and not in operators. It verifies membership conditions with assertions.

Test Code - pytest
PyTest
import pytest

def test_membership():
    fruits = ['apple', 'banana', 'cherry']
    # Check if 'banana' is in the list
    assert 'banana' in fruits
    # Check if 'orange' is not in the list
    assert 'orange' not in fruits
Execution Trace - 5 Steps
StepActionSystem StateAssertionResult
1Test startspytest test runner initialized-PASS
2Defines list fruits with ['apple', 'banana', 'cherry']List fruits created in memory-PASS
3Checks if 'banana' is in fruits using assert 'banana' in fruitsEvaluates membership condition'banana' is found in fruits listPASS
4Checks if 'orange' is not in fruits using assert 'orange' not in fruitsEvaluates membership condition'orange' is not found in fruits listPASS
5Test ends successfullyAll assertions passed-PASS
Failure Scenario
Failing Condition: 'banana' is missing from the fruits list or 'orange' is present in the list
Execution Trace Quiz - 3 Questions
Test your understanding
What does the assertion 'assert "banana" in fruits' verify?
Afruits list is empty
B"banana" is not in the fruits list
C"banana" is an element of the fruits list
Dfruits list contains only "banana"
Key Result
Use simple membership assertions with 'in' and 'not in' to verify presence or absence of items in collections. This makes tests clear and easy to understand.