Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to define a test function using pytest.
PyTest
def [1](): assert 1 + 1 == 2
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Naming the function without the 'test_' prefix.
Using names that pytest won't recognize as tests.
✗ Incorrect
Pytest requires test functions to start with test_. So test_addition is correct.
2fill in blank
mediumComplete the code to assert that a function returns the expected value.
PyTest
def test_double(): result = double(3) assert result [1] 6
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
!= instead of ==.Using comparison operators like < or > which do not check equality.
✗ Incorrect
The assertion checks if result equals 6, so == is correct.
3fill in blank
hardFix the error in the test function name so pytest can discover it.
PyTest
def [1](): assert sum([1, 2, 3]) == 6
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using function names without the 'test_' prefix.
Using names that look like tests but don't start with 'test_'.
✗ Incorrect
Pytest discovers tests only if function names start with test_. test_sum is correct.
4fill in blank
hardFill both blanks to complete a test function that checks if a list contains a value.
PyTest
def test_contains(): items = [1, 2, 3] assert [1] in [2]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a value not in the list.
Using an undefined variable for the list.
✗ Incorrect
The test checks if 2 is in the list items.
5fill in blank
hardFill all three blanks to create a test function that checks if a string starts with a prefix.
PyTest
def test_startswith(): text = "hello world" prefix = "he" assert [1].[2]([3])
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
endswith instead of startswith.Swapping the variable names.
✗ Incorrect
The test asserts that the string text starts with prefix using the startswith method.