Challenge - 5 Problems
Membership Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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
Attempts:
2 left
💡 Hint
Check if the items are actually in the list and how assert works.
✗ Incorrect
The list contains 'banana' so the first assert passes. 'orange' is not in the list, so the second assert also passes. No errors occur.
❓ assertion
intermediate2: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
Attempts:
2 left
💡 Hint
Check which colors are actually in the set.
✗ Incorrect
'blue' is in the set, so asserting it is not in colors will fail.
🔧 Debug
advanced2: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
Attempts:
2 left
💡 Hint
Think about what types support membership tests.
✗ Incorrect
NoneType does not support membership tests with 'in', so Python raises a TypeError.
❓ framework
advanced2: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'?
Attempts:
2 left
💡 Hint
Consider readability and correctness of Python syntax.
✗ Incorrect
Option A is clear, readable, and correct. Option A is incorrect due to operator precedence (not 'error' evaluates to False, so it checks False in log). Option A is functionally correct but redundant (== True). Option A treats log as a function which may cause error.
🧠 Conceptual
expert3: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()
Attempts:
2 left
💡 Hint
Look at how __contains__ is implemented and what it returns.
✗ Incorrect
The __contains__ method returns True only if the item is 'allowed'. So membership test returns True for 'allowed' and False otherwise.