Complete the code to define a simple test function using pytest.
def test_addition(): assert 2 + 2 == [1]
The test checks if 2 + 2 equals 4, which is true. This simple assertion helps verify basic functionality.
Complete the code to mark a test as expected to fail using pytest.
import pytest @pytest.mark.[1]() def test_fail_example(): assert False
The decorator @pytest.mark.xfail() marks the test as expected to fail, so pytest reports it differently without failing the whole suite.
Fix the error in the test function to correctly check if a list is empty.
def test_empty_list(): my_list = [] assert [1] == 0
my_list[0] causes an error if the list is empty.my_list.length is invalid in Python.Using len(my_list) returns the number of items in the list, which should be 0 if empty. Checking my_list directly won't compare to 0.
Fill both blanks to create a test that checks if a string contains a substring.
def test_substring(): text = "hello world" assert [1] in [2]
The test checks if the substring "world" is inside the string variable text. This is a common way to verify content in strings.
Fill all three blanks to create a dictionary comprehension that filters words longer than 3 characters.
words = ["cat", "house", "dog", "elephant"] lengths = { [1]: [2] for [3] in words if len([3]) > 3 }
The dictionary comprehension syntax starts with a curly brace {. It maps each word to its length len(word) for words longer than 3 characters.