project-root/
├── tests/
│ ├── test_membership.py
│ └── __init__.py
├── src/
│ └── membership_utils.py
├── conftest.py
├── pytest.ini
└── requirements.txt
Checking membership (in, not in) in PyTest - Framework Patterns
- Source Code Layer (src/): Contains the main code to check membership logic, e.g., functions that verify if an item is in a collection.
- Test Layer (tests/): Contains pytest test files that use assertions with
inandnot into verify membership behavior. - Configuration Layer (conftest.py, pytest.ini): Holds pytest configurations and fixtures for setup or reusable test data.
- Utilities Layer: Could include helper functions or test data generators if needed (not shown here for simplicity).
Use pytest.ini to configure pytest options like test paths and markers.
Use conftest.py to define fixtures that provide test data or setup, for example, a fixture that returns a sample list or dictionary to test membership.
Environment variables or command line options can be added for different test scenarios if needed, but for membership tests, simple fixtures suffice.
Pytest outputs clear pass/fail results in the console.
Use plugins like pytest-html to generate HTML reports for better visualization.
Integrate pytest runs into CI/CD pipelines (GitHub Actions, Jenkins, GitLab CI) to run tests automatically on code changes.
Failing tests with in or not in assertions will cause the pipeline to fail, alerting developers immediately.
- Use clear, simple test names that describe the membership check, e.g.,
test_item_in_list. - Use pytest's assert statements directly with
inandnot infor readability. - Keep test data small and focused to isolate membership logic.
- Use fixtures to avoid repeating test data setup.
- Write both positive (item is in collection) and negative (item is not in collection) tests to cover both cases.
Where in this folder structure would you add a new test file to check membership of keys in a dictionary?
Practice
'apple' is in the list fruits = ['apple', 'banana', 'cherry'] using pytest assertions?Solution
Step 1: Understand the membership check
Theinkeyword checks if an item exists inside a collection like a list.Step 2: Apply the correct assertion
To confirm 'apple' is in the list, useassert 'apple' in fruits. This passes if 'apple' is found.Final Answer:
assert 'apple' in fruits -> Option CQuick Check:
Useinto check presence [OK]
- Using 'not in' instead of 'in' when checking presence
- Reversing the order like 'fruits in apple'
- Using equality instead of membership check
5 is NOT in the list numbers = [1, 2, 3, 4] using pytest?Solution
Step 1: Identify the correct membership operator
To check that 5 is not in the list, usenot inkeyword.Step 2: Write the correct assertion syntax
The correct syntax isassert 5 not in numbers. This passes if 5 is absent.Final Answer:
assert 5 not in numbers -> Option DQuick Check:
Use 'not in' to check absence [OK]
- Misplacing 'not' like 'assert 5 not numbers' (SyntaxError)
- Swapping operands like 'numbers not in 5' which is invalid
- Using 'in' instead of 'not in' for absence check
items = ['pen', 'pencil', 'eraser'] assert 'marker' not in items
Solution
Step 1: Check if 'marker' is in the list
The list contains 'pen', 'pencil', and 'eraser'. 'marker' is not present.Step 2: Evaluate the assertion
The assertion checks that 'marker' is not in items, which is true, so the test passes.Final Answer:
Test passes because 'marker' is not in the list -> Option AQuick Check:
Not in list means assertion passes [OK]
- Assuming test fails if item is missing
- Confusing 'in' and 'not in' logic
- Thinking syntax error occurs with 'not in'
colors = ['red', 'green', 'blue'] assert 'yellow' in colors not
Solution
Step 1: Analyze the assertion syntax
The expression ends with 'colors not' which is invalid syntax in Python.Step 2: Correct the membership operator usage
The correct operator for checking absence is 'not in', written together, not separated.Final Answer:
SyntaxError: 'not' should come before 'in' as 'not in' -> Option BQuick Check:
Use 'not in' together, no trailing 'not' [OK]
- Writing 'in colors not' instead of 'not in colors'
- Using 'assert colors not in 'yellow'' which reverses order
- Omitting 'not' or placing it incorrectly
['spam', 'scam', 'fraud'] appear in the user input string text. Which assertion correctly checks this?Solution
Step 1: Understand the requirement
We want to ensure none of the forbidden words appear in the text, so all must be absent.Step 2: Use a generator with 'all' and 'not in'
The expressionall(word not in text for word in forbidden_words)returns True only if every word is not in text.Step 3: Evaluate other options
assert any(word in text for word in ['spam', 'scam', 'fraud']) checks if any forbidden word is present (opposite). assert 'spam' not in text and 'scam' not in text or 'fraud' not in text has incorrect logic mixing 'and' and 'or'. assert 'spam' in text or 'scam' in text or 'fraud' in text checks presence, not absence.Final Answer:
assert all(word not in text for word in ['spam', 'scam', 'fraud']) -> Option AQuick Check:
Use all() with 'not in' for absence of all items [OK]
- Using any() instead of all() for absence check
- Mixing 'and' and 'or' incorrectly in conditions
- Checking only one forbidden word instead of all
Ready to prove it?
Pass the test and this pattern moves unknown
