0
0
PyTesttesting~10 mins

Single responsibility per test 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 define a test that checks if the sum of two numbers is correct.

PyTest
def test_sum():
    result = 2 + 3
    assert result == [1]
Drag options to blanks, or click blank then click option'
A7
B5
C6
D4
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong expected value in the assertion.
Forgetting to use assert keyword.
2fill in blank
medium

Complete the code to test if a list contains a specific element.

PyTest
def test_contains_element():
    fruits = ['apple', 'banana', 'cherry']
    assert 'banana' [1] fruits
Drag options to blanks, or click blank then click option'
Ain
Bnot in
C==
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' instead of 'in' for membership check.
Using 'not in' which checks the opposite.
3fill in blank
hard

Fix the error in the test function that checks if a string is uppercase.

PyTest
def test_is_uppercase():
    text = 'HELLO'
    assert text.[1]()
Drag options to blanks, or click blank then click option'
Aisupper
Bistitle
Cislower
Disalpha
Attempts:
3 left
💡 Hint
Common Mistakes
Using islower() which checks for lowercase letters.
Using istitle() which checks for title case.
4fill in blank
hard

Complete the code to create a test that checks if a list is sorted in ascending order.

PyTest
def test_sorted_list():
    numbers = [1, 2, 3, 4, 5]
    assert numbers == sorted(numbers)

    assert numbers[1] sorted(numbers)
Drag options to blanks, or click blank then click option'
B!=
C==
D[::-1]
Attempts:
3 left
💡 Hint
Common Mistakes
Adding unnecessary slicing inside sorted().
Using '!=' instead of '==' in the second assertion.
5fill in blank
hard

Fill both blanks to write a test that checks if a dictionary comprehension creates correct squares for numbers greater than 2.

PyTest
def test_squares_dict():
    numbers = [1, 2, 3, 4]
    squares = {n:: n[1] for n in numbers if n [2] 2}
    assert squares == {3: 9, 4: 16}
Drag options to blanks, or click blank then click option'
A**2
B>
C:
D*2
Attempts:
3 left
💡 Hint
Common Mistakes
Using '*' instead of '**' for squaring.
Using '<' instead of '>' in the condition.
Using ':' incorrectly in dictionary comprehension.