Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
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.
✗ Incorrect
The assert statement can include a message string after a comma to show when the assertion fails.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong number for the length.
Forgetting that len() returns an integer.
✗ Incorrect
The list has 3 items, so the assertion checks if length equals 3.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using colon or semicolon instead of comma.
Omitting the separator between condition and message.
✗ Incorrect
The assert statement uses a comma to separate the condition and the message string.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different punctuation marks.
Forgetting the comma in one of the asserts.
✗ Incorrect
Each assert statement uses a comma to separate the condition and the message.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' or '!=' instead of 'in' or 'not in'.
Mixing up 'in' and 'not in' for presence checks.
✗ Incorrect
To check if a key is present in a dictionary, use 'in'. To check absence, use 'not in'.