0
0
PyTesttesting~20 mins

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

Choose your learning style9 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.