0
0
PyTesttesting~10 mins

Test functions 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 define a test function using pytest.

PyTest
def [1]():
    assert 1 + 1 == 2
Drag options to blanks, or click blank then click option'
Acheck_add
Btest_addition
Caddition_test
Dverify_sum
Attempts:
3 left
💡 Hint
Common Mistakes
Naming the function without the 'test_' prefix.
Using names that pytest won't recognize as tests.
2fill in blank
medium

Complete 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'
A==
B!=
C<
D>
Attempts:
3 left
💡 Hint
Common Mistakes
Using != instead of ==.
Using comparison operators like < or > which do not check equality.
3fill in blank
hard

Fix 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'
Atest_sum
Bverify_sum
Csum_test
Dcheck_sum
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_'.
4fill in blank
hard

Fill 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'
A2
Bitems
C3
Dlist
Attempts:
3 left
💡 Hint
Common Mistakes
Using a value not in the list.
Using an undefined variable for the list.
5fill in blank
hard

Fill 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'
Atext
Bstartswith
Cprefix
Dendswith
Attempts:
3 left
💡 Hint
Common Mistakes
Using endswith instead of startswith.
Swapping the variable names.