Bird
Raised Fist0
PyTesttesting~20 mins

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

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Membership Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of membership test in pytest assertion
What will be the result of running this pytest test function?
PyTest
def test_membership():
    fruits = ['apple', 'banana', 'cherry']
    assert 'banana' in fruits
    assert 'orange' not in fruits
ASyntaxError due to incorrect assert syntax
BTest passes without errors
CAssertionError because 'orange' is in fruits
DAssertionError because 'banana' is not in fruits
Attempts:
2 left
💡 Hint
Check if the items are actually in the list and how assert works.
❓ assertion
intermediate
2:00remaining
Identify the failing assertion
Given the following test code, which assertion will fail when the test runs?
PyTest
def test_colors():
    colors = {'red', 'green', 'blue'}
    assert 'yellow' not in colors
    assert 'green' in colors
    assert 'blue' not in colors
    assert 'red' in colors
Aassert 'yellow' not in colors
Bassert 'green' in colors
Cassert 'blue' not in colors
Dassert 'red' in colors
Attempts:
2 left
💡 Hint
Check which colors are actually in the set.
🔧 Debug
advanced
2:00remaining
Find the error in membership test
Why does this pytest test raise a TypeError?
PyTest
def test_membership_error():
    data = None
    assert 'key' in data
ABecause 'in' operator cannot be used with NoneType
BBecause 'key' is not a string
CBecause assert syntax is incorrect
DBecause data is an empty dictionary
Attempts:
2 left
💡 Hint
Think about what types support membership tests.
❓ framework
advanced
2:00remaining
Best practice for membership assertion in pytest
Which pytest assertion is the best practice to check that the string 'error' is NOT in the log message variable 'log'?
Aassert 'error' not in log
Bassert not 'error' in log
Cassert 'error' not in log == True
Dassert 'error' not in log()
Attempts:
2 left
💡 Hint
Consider readability and correctness of Python syntax.
🧠 Conceptual
expert
3:00remaining
Understanding membership test behavior with custom objects
Consider a class with a custom __contains__ method. Which statement about membership tests using 'in' is true?
PyTest
class Container:
    def __contains__(self, item):
        return item == 'allowed'

c = Container()
A'allowed' in c returns True, 'denied' in c raises AttributeError
B'allowed' in c returns False, 'denied' in c returns True
C'allowed' in c raises TypeError, 'denied' in c returns False
D'allowed' in c returns True, 'denied' in c returns False
Attempts:
2 left
💡 Hint
Look at how __contains__ is implemented and what it returns.

Practice

(1/5)
1. Which of the following statements correctly checks if the string 'apple' is in the list fruits = ['apple', 'banana', 'cherry'] using pytest assertions?
easy
A. assert fruits in 'apple'
B. assert 'apple' not in fruits
C. assert 'apple' in fruits
D. assert 'apple' == fruits

Solution

  1. Step 1: Understand the membership check

    The in keyword checks if an item exists inside a collection like a list.
  2. Step 2: Apply the correct assertion

    To confirm 'apple' is in the list, use assert 'apple' in fruits. This passes if 'apple' is found.
  3. Final Answer:

    assert 'apple' in fruits -> Option C
  4. Quick Check:

    Use in to check presence [OK]
Hint: Use 'in' to check presence, 'not in' for absence [OK]
Common Mistakes:
  • Using 'not in' instead of 'in' when checking presence
  • Reversing the order like 'fruits in apple'
  • Using equality instead of membership check
2. Which of the following is the correct syntax to assert that the number 5 is NOT in the list numbers = [1, 2, 3, 4] using pytest?
easy
A. assert 5 in numbers
B. assert numbers not in 5
C. assert 5 not numbers
D. assert 5 not in numbers

Solution

  1. Step 1: Identify the correct membership operator

    To check that 5 is not in the list, use not in keyword.
  2. Step 2: Write the correct assertion syntax

    The correct syntax is assert 5 not in numbers. This passes if 5 is absent.
  3. Final Answer:

    assert 5 not in numbers -> Option D
  4. Quick Check:

    Use 'not in' to check absence [OK]
Hint: Remember 'not in' checks absence, syntax: assert x not in y [OK]
Common Mistakes:
  • 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
3. What will be the result of this pytest assertion?
items = ['pen', 'pencil', 'eraser']
assert 'marker' not in items
medium
A. Test passes because 'marker' is not in the list
B. Test fails because 'marker' is in the list
C. SyntaxError due to wrong assertion
D. Test passes because 'marker' is in the list

Solution

  1. Step 1: Check if 'marker' is in the list

    The list contains 'pen', 'pencil', and 'eraser'. 'marker' is not present.
  2. Step 2: Evaluate the assertion

    The assertion checks that 'marker' is not in items, which is true, so the test passes.
  3. Final Answer:

    Test passes because 'marker' is not in the list -> Option A
  4. Quick Check:

    Not in list means assertion passes [OK]
Hint: If item absent and 'not in' used, test passes [OK]
Common Mistakes:
  • Assuming test fails if item is missing
  • Confusing 'in' and 'not in' logic
  • Thinking syntax error occurs with 'not in'
4. Identify the error in this pytest assertion and select the correct fix:
colors = ['red', 'green', 'blue']
assert 'yellow' in colors not
medium
A. Use 'assert not 'yellow' in colors' instead
B. SyntaxError: 'not' should come before 'in' as 'not in'
C. Replace 'in' with 'not in' and remove 'not' at the end
D. No error, assertion is correct

Solution

  1. Step 1: Analyze the assertion syntax

    The expression ends with 'colors not' which is invalid syntax in Python.
  2. Step 2: Correct the membership operator usage

    The correct operator for checking absence is 'not in', written together, not separated.
  3. Final Answer:

    SyntaxError: 'not' should come before 'in' as 'not in' -> Option B
  4. Quick Check:

    Use 'not in' together, no trailing 'not' [OK]
Hint: Write 'not in' together, never split or reorder [OK]
Common Mistakes:
  • Writing 'in colors not' instead of 'not in colors'
  • Using 'assert colors not in 'yellow'' which reverses order
  • Omitting 'not' or placing it incorrectly
5. You want to write a pytest test to confirm that none of the forbidden words ['spam', 'scam', 'fraud'] appear in the user input string text. Which assertion correctly checks this?
hard
A. assert all(word not in text for word in ['spam', 'scam', 'fraud'])
B. assert any(word in text for word in ['spam', 'scam', 'fraud'])
C. assert 'spam' not in text and 'scam' not in text or 'fraud' not in text
D. assert 'spam' in text or 'scam' in text or 'fraud' in text

Solution

  1. Step 1: Understand the requirement

    We want to ensure none of the forbidden words appear in the text, so all must be absent.
  2. Step 2: Use a generator with 'all' and 'not in'

    The expression all(word not in text for word in forbidden_words) returns True only if every word is not in text.
  3. 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.
  4. Final Answer:

    assert all(word not in text for word in ['spam', 'scam', 'fraud']) -> Option A
  5. Quick Check:

    Use all() with 'not in' for absence of all items [OK]
Hint: Use all() with 'not in' to confirm all items absent [OK]
Common Mistakes:
  • Using any() instead of all() for absence check
  • Mixing 'and' and 'or' incorrectly in conditions
  • Checking only one forbidden word instead of all
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