0
0
PyTesttesting~10 mins

First PyTest test - Interactive Code Practice

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

Complete the code to write a simple PyTest test function that checks if 2 + 2 equals 4.

PyTest
def test_addition():
    assert 2 + 2 == [1]
Drag options to blanks, or click blank then click option'
A5
B4
C3
D6
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong number like 5 or 3 causes the test to fail.
Forgetting to use assert keyword.
2fill in blank
medium

Complete the code to write a PyTest test function that checks if the string 'hello' starts with 'h'.

PyTest
def test_startswith():
    assert 'hello'.[1]('h')
Drag options to blanks, or click blank then click option'
Acontains
Bendswith
Cstartswith
Dfind
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'endswith' checks the end, not the start.
Using 'contains' is not a string method in Python.
3fill in blank
hard

Fix the error in the PyTest test function that checks if the length of list [1, 2, 3] is 3.

PyTest
def test_list_length():
    assert len([1, 2, 3]) == [1]
Drag options to blanks, or click blank then click option'
A3
B4
C2
D5
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong number like 2 or 4 causes the test to fail.
Forgetting to use len() function.
4fill in blank
hard

Fill both blanks to write a PyTest test that checks if the last character of 'world' is 'd'.

PyTest
def test_last_char():
    assert 'world'[[1]] == [2]
Drag options to blanks, or click blank then click option'
A-1
B0
C'd'
D'w'
Attempts:
3 left
💡 Hint
Common Mistakes
Using index 0 gets the first character, not the last.
Using wrong character in comparison.
5fill in blank
hard

Fill all three blanks to write a PyTest test that checks if the dictionary {'a': 1, 'b': 2} has key 'a' with value 1.

PyTest
def test_dict_key_value():
    data = [1]
    assert data[[2]] == [3]
Drag options to blanks, or click blank then click option'
A{'a': 1, 'b': 2}
B'a'
C1
D{'x': 10, 'y': 20}
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong dictionary or key causes test failure.
Comparing to wrong value.