Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to assert that the function returns True.
PyTest
def test_function(): result = my_function() assert result [1] True
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using single = instead of == in assert
Using != instead of ==
✗ Incorrect
The assertion must check if result is equal to True using ==.
2fill in blank
mediumComplete the code to skip the test when the condition is True.
PyTest
@pytest.mark.skipif([1], reason="Not supported") def test_feature(): assert feature()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using False so test never skips
Using None which is not a boolean
✗ Incorrect
The test is skipped when the condition is True.
3fill in blank
hardFix the error in the fixture definition to return a value.
PyTest
@pytest.fixture def sample_data(): data = {'key': 'value'} [1] data
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using print instead of return
Using pass which returns None
✗ Incorrect
Fixtures must return data to be used in tests. Use return to provide the data.
4fill in blank
hardFill both blanks to parametrize the test with two values.
PyTest
@pytest.mark.parametrize("input,expected", [ ([1], [2]), ]) def test_double(input, expected): assert double(input) == expected
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping input and expected values
Using wrong numbers that don't match doubling
✗ Incorrect
The test checks if doubling 2 gives 4, so input is 2 and expected is 4.
5fill in blank
hardFill all three blanks to create a test that checks if a list contains an item.
PyTest
def test_contains(): items = [[1], [2], [3]] assert 3 in items
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Not including 3 in the list
Using non-integer values
✗ Incorrect
The list contains 1, 3, and 5. The test asserts that 3 is in the list.