0
0
PyTesttesting~10 mins

Deterministic tests 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 make the test deterministic by fixing the seed.

PyTest
import random

def test_random_number():
    random.seed([1])
    num = random.randint(1, 10)
    assert num == 7
Drag options to blanks, or click blank then click option'
A0
Brandom
C42
DNone
Attempts:
3 left
💡 Hint
Common Mistakes
Using random.seed(None) which makes the test non-deterministic.
2fill in blank
medium

Complete the code to ensure the test always checks the same output from a function using a fixed seed.

PyTest
import random

def get_random_choice():
    random.seed([1])
    return random.choice(['apple', 'banana', 'cherry'])

def test_choice():
    assert get_random_choice() == 'banana'
Drag options to blanks, or click blank then click option'
A42
B100
C0
D1
Attempts:
3 left
💡 Hint
Common Mistakes
Not setting the seed inside the function, causing different outputs.
3fill in blank
hard

Fix the error in the test to make it deterministic by setting the seed correctly.

PyTest
import random

def test_sum():
    random.seed([1])
    values = [random.randint(1, 5) for _ in range(3)]
    assert sum(values) == 7
Drag options to blanks, or click blank then click option'
Arandom
B42
C'seed'
DNone
Attempts:
3 left
💡 Hint
Common Mistakes
Using None or a string as seed, which does not fix randomness.
4fill in blank
hard

Fill both blanks to create a deterministic test that filters even numbers from a random list.

PyTest
import random

def test_even_numbers():
    random.seed([1])
    numbers = [random.randint(1, 10) for _ in range(5)]
    evens = [num for num in numbers if num [2] 2 == 0]
    assert evens == [4, 10]
Drag options to blanks, or click blank then click option'
A123
B!=
C%
D==
Attempts:
3 left
💡 Hint
Common Mistakes
Using '!=' instead of '%' causes wrong filtering.
5fill in blank
hard

Fill all three blanks to create a deterministic test that maps uppercase keys to values greater than 5.

PyTest
import random

def test_filtered_dict():
    random.seed([1])
    data = {chr(97 + i): random.randint(1, 10) for i in range(4)}
    filtered = {k[2]: v for k, v in data.items() if v [3] 5}
    assert filtered == {'A': 7, 'D': 6}
Drag options to blanks, or click blank then click option'
A7
B>
C.upper()
D42
Attempts:
3 left
💡 Hint
Common Mistakes
Not converting keys to uppercase or using wrong comparison operator.