0
0
PyTesttesting~10 mins

Why organized tests scale with projects 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 define a simple test function using pytest.

PyTest
def test_addition():
    assert 2 + 2 == [1]
Drag options to blanks, or click blank then click option'
A5
B3
C22
D4
Attempts:
3 left
💡 Hint
Common Mistakes
Using 5 or 3 instead of 4 will cause the test to fail.
Writing the number as a string like '4' instead of 4.
2fill in blank
medium

Complete the code to mark a test as expected to fail using pytest.

PyTest
import pytest

@pytest.mark.[1]()
def test_fail_example():
    assert False
Drag options to blanks, or click blank then click option'
Askip
Bxfail
Cparametrize
Dfixture
Attempts:
3 left
💡 Hint
Common Mistakes
Using @pytest.skip will skip the test instead of marking it as expected fail.
Using @pytest.fixture or @pytest.parametrize are unrelated to expected failure.
3fill in blank
hard

Fix the error in the test function to correctly check if a list is empty.

PyTest
def test_empty_list():
    my_list = []
    assert [1] == 0
Drag options to blanks, or click blank then click option'
Alen(my_list)
Bmy_list.length
Cmy_list[0]
Dmy_list
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to access my_list[0] causes an error if the list is empty.
Using my_list.length is invalid in Python.
4fill in blank
hard

Fill both blanks to create a test that checks if a string contains a substring.

PyTest
def test_substring():
    text = "hello world"
    assert [1] in [2]
Drag options to blanks, or click blank then click option'
A"world"
B"hello"
Ctext
D"planet"
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping the order of substring and string will cause the test to fail.
Using a substring not present in the string will fail the test.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that filters words longer than 3 characters.

PyTest
words = ["cat", "house", "dog", "elephant"]
lengths = { [1]: [2] for [3] in words if len([3]) > 3 }
Drag options to blanks, or click blank then click option'
Aword
Blen(word)
D{
Attempts:
3 left
💡 Hint
Common Mistakes
Not starting with a curly brace causes syntax errors.
Swapping key and value will produce incorrect dictionaries.
Using a variable name not defined in the loop causes errors.