0
0
PyTesttesting~10 mins

Why assert is PyTest's core mechanism - Test Your Understanding

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

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

PyTest
def test_sum():
    assert 2 + 2 [1] 4
Drag options to blanks, or click blank then click option'
A=
B==
C!=
D>
Attempts:
3 left
💡 Hint
Common Mistakes
Using single = instead of == causes syntax error.
Using != will fail the test.
2fill in blank
medium

Complete the code to assert that a list contains the number 5.

PyTest
def test_contains():
    numbers = [1, 3, 5, 7]
    assert [1] in numbers
Drag options to blanks, or click blank then click option'
A5
B6
C4
D7
Attempts:
3 left
💡 Hint
Common Mistakes
Choosing a number not in the list causes test failure.
3fill in blank
hard

Fix the error in the assert statement to correctly check if the string 'hello' starts with 'h'.

PyTest
def test_startswith():
    word = 'hello'
    assert word.[1]('h')
Drag options to blanks, or click blank then click option'
Astartswith
Bstartwith
Cstarts_with
Dbeginwith
Attempts:
3 left
💡 Hint
Common Mistakes
Misspelling the method name causes AttributeError.
4fill in blank
hard

Fill both blanks to create a test that asserts the length of the list is greater than 3.

PyTest
def test_length():
    items = [1, 2, 3, 4, 5]
    assert len(items) [1] [2]
Drag options to blanks, or click blank then click option'
A>
B3
C5
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using < instead of > causes test failure.
Using wrong number causes wrong assertion.
5fill in blank
hard

Fill all three blanks to create a test that asserts a dictionary has a key 'name' and its value is 'Alice'.

PyTest
def test_dict():
    person = {'name': 'Alice', 'age': 30}
    assert '[1]' in person and person['[2]'] [3] 'Alice'
Drag options to blanks, or click blank then click option'
Aname
Bage
C==
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong key name causes test failure.
Using != operator causes test failure.