0
0
PyTesttesting~10 mins

Arrange-Act-Assert pattern 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 arrange the test data correctly.

PyTest
def test_sum():
    numbers = [1]
    result = sum(numbers)
    assert result == 6
Drag options to blanks, or click blank then click option'
A(1, 2, 3)
B[1, 2, 3]
C{1, 2, 3}
D1, 2, 3
Attempts:
3 left
💡 Hint
Common Mistakes
Using a tuple or set instead of a list.
Not using any container for numbers.
2fill in blank
medium

Complete the code to act by calling the function under test.

PyTest
def test_uppercase():
    text = "hello"
    result = text.[1]()
    assert result == "HELLO"
Drag options to blanks, or click blank then click option'
Aupper
Blower
Ccapitalize
Dtitle
Attempts:
3 left
💡 Hint
Common Mistakes
Using lower() which converts to lowercase.
Using capitalize() which only changes the first letter.
3fill in blank
hard

Fix the error in the assertion to correctly check the result.

PyTest
def test_length():
    word = "test"
    length = len(word)
    assert length [1] 5
Drag options to blanks, or click blank then click option'
A==
B>
C<
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using == 5 which fails because length is 4.
Using > 5 which is false.
4fill in blank
hard

Fill both blanks to create a test that asserts a list contains a value.

PyTest
def test_contains():
    items = [1, 2, 3]
    value = 2
    assert value [1] items[2]
Drag options to blanks, or click blank then click option'
Ain
Bnot in
C==
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using == which compares value to the whole list.
Using not in which would fail the test.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that filters items by value.

PyTest
def test_filter_dict():
    data = {'a': 1, 'b': 3, 'c': 2}
    filtered = {k: v for k, v in data.items() if v [1] [2]
    assert filtered == [3]
Drag options to blanks, or click blank then click option'
A>
B1
C{'b': 3, 'c': 2}
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using < instead of > which changes the filter.
Using wrong expected dictionary in the assertion.