0
0
PyTesttesting~10 mins

Why patterns improve test quality in PyTest - 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 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'
A4
B5
C3
D6
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong expected value like 5 or 3.
Forgetting to use assert keyword.
2fill in blank
medium

Complete the code to use a pytest fixture named 'sample_data' in the test function.

PyTest
@pytest.fixture
def sample_data():
    return [1, 2, 3]

def test_sum([1]):
    assert sum(sample_data) == 6
Drag options to blanks, or click blank then click option'
Adata
Bsample_data
Cinput_data
Dnumbers
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different parameter name than the fixture.
Not including the fixture as a parameter.
3fill in blank
hard

Fix the error in the test function by completing the assertion to check if the list is empty.

PyTest
def test_empty_list():
    my_list = []
    assert [1](my_list)
Drag options to blanks, or click blank then click option'
Anot
Blen
Cempty
Dis
Attempts:
3 left
💡 Hint
Common Mistakes
Using len(my_list) without comparison.
Using 'is' which is incorrect here.
Using a non-existent function 'empty'.
4fill in blank
hard

Fill both blanks to create a parametrized pytest test that checks if numbers are even.

PyTest
@pytest.mark.parametrize('num', [2, 3, 4])
def test_even(num):
    assert num [1] 2 == 0
    assert isinstance(num, [2])
Drag options to blanks, or click blank then click option'
A%
Bint
Cfloat
D==
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' instead of '%' for modulus.
Checking type with 'float' instead of 'int'.
5fill in blank
hard

Fill all three blanks to create a test that uses a fixture, asserts a condition, and checks type.

PyTest
@pytest.fixture
def data():
    return {'a': 1, 'b': 2}

def test_data([1]):
    assert [2]['a'] == 1
    assert isinstance([3], dict)
Drag options to blanks, or click blank then click option'
Adata
Dinput
Attempts:
3 left
💡 Hint
Common Mistakes
Using different names for fixture parameter and variable.
Not checking the correct key in the dictionary.