0
0
PyTesttesting~10 mins

Assert with messages 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 add a message to the assertion.

PyTest
def test_sum():
    result = 2 + 2
    assert result == 4, [1]
Drag options to blanks, or click blank then click option'
Aassert result == 4
BSum should be 4
Cprint('Sum is 4')
D"Sum should be 4"
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to put the message in quotes.
Using print instead of an assert message.
Not adding a comma before the message.
2fill in blank
medium

Complete the code to assert that a list contains 3 items with a message.

PyTest
def test_list_length():
    items = [1, 2, 3]
    assert len(items) == [1], "List should have 3 items"
Drag options to blanks, or click blank then click option'
A3
B2
C4
D5
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong number for the length.
Forgetting that len() returns an integer.
3fill in blank
hard

Fix the error in the assertion message syntax.

PyTest
def test_value():
    value = 10
    assert value > 5 [1] "Value should be greater than 5"
Drag options to blanks, or click blank then click option'
A:
B,
C;
D.
Attempts:
3 left
💡 Hint
Common Mistakes
Using colon or semicolon instead of comma.
Omitting the separator between condition and message.
4fill in blank
hard

Fill both blanks to assert two conditions with messages.

PyTest
def test_multiple():
    x = 7
    y = 3
    assert x > y[1] "x should be greater than y"
    assert y > 0[2] "y should be positive"
Drag options to blanks, or click blank then click option'
A,
B:
C;
D.
Attempts:
3 left
💡 Hint
Common Mistakes
Using different punctuation marks.
Forgetting the comma in one of the asserts.
5fill in blank
hard

Fill all three blanks to assert dictionary key presence with messages.

PyTest
def test_dict_keys():
    data = {'name': 'Alice', 'age': 30}
    assert 'name' [1] data, "Key 'name' missing"
    assert 'age' [2] data, "Key 'age' missing"
    assert 'email' [3] data, "Key 'email' missing"
Drag options to blanks, or click blank then click option'
Ain
Bnot in
C==
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' or '!=' instead of 'in' or 'not in'.
Mixing up 'in' and 'not in' for presence checks.