0
0
PyTesttesting~10 mins

Checking membership (in, not in) in PyTest - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to check if 'apple' is in the fruits list.

PyTest
def test_apple_in_fruits():
    fruits = ['apple', 'banana', 'cherry']
    assert 'apple' [1] fruits
Drag options to blanks, or click blank then click option'
Ain
B!=
C==
Dnot in
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'not in' instead of 'in' will fail the test.
2fill in blank
medium

Complete the code to check that 'grape' is not in the fruits list.

PyTest
def test_grape_not_in_fruits():
    fruits = ['apple', 'banana', 'cherry']
    assert 'grape' [1] fruits
Drag options to blanks, or click blank then click option'
Ain
B==
Cnot in
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'in' instead of 'not in' will cause the test to fail.
3fill in blank
hard

Fix the error in the assertion to check if 'banana' is in the fruits list.

PyTest
def test_banana_in_fruits():
    fruits = ['apple', 'banana', 'cherry']
    assert 'banana' [1] fruits
Drag options to blanks, or click blank then click option'
A==
Bnot in
C!=
Din
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'not in' will cause the test to fail incorrectly.
4fill in blank
hard

Fill both blanks to create a test that checks if 'orange' is not in the fruits list and 'cherry' is in the fruits list.

PyTest
def test_fruits_membership():
    fruits = ['apple', 'banana', 'cherry']
    assert 'orange' [1] fruits
    assert 'cherry' [2] fruits
Drag options to blanks, or click blank then click option'
Anot in
B==
Cin
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up 'in' and 'not in' keywords.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that includes only fruits that are in the allowed list.

PyTest
def test_allowed_fruits():
    fruits = ['apple', 'banana', 'cherry', 'date']
    allowed = ['banana', 'date', 'fig']
    filtered = {fruit: True for fruit in fruits if fruit [1] allowed and fruit [2] 'apple' and fruit [3] 'fig'}
    assert 'banana' in filtered
    assert 'apple' not in filtered
    assert 'fig' not in filtered
Drag options to blanks, or click blank then click option'
Ain
Bnot in
C!=
D==
Attempts:
3 left
💡 Hint
Common Mistakes
Using not in instead of != for string comparisons.