0
0
PyTesttesting~20 mins

Deterministic tests in PyTest - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Deterministic Testing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of a deterministic pytest function
What is the output of this pytest test function when run?
PyTest
import pytest

def add(a, b):
    return a + b

def test_add():
    assert add(2, 3) == 5
    assert add(0, 0) == 0
    assert add(-1, 1) == 0
ASyntaxError due to missing colon
BAll assertions pass, test result: PASSED
CAssertionError on add(-1, 1) == 0, test result: FAILED
DTypeError because add expects strings
Attempts:
2 left
💡 Hint
Check the add function and the assertions carefully.
assertion
intermediate
1:30remaining
Choosing the correct assertion for deterministic output
Which assertion correctly tests that a function returns a fixed string 'hello' every time?
PyTest
def greet():
    return 'hello'
Aassert greet() == 'Hello'
Bassert greet() != 'hello'
Cassert greet() == 'hello'
Dassert greet() is 'hello'
Attempts:
2 left
💡 Hint
Check string equality and case sensitivity.
🔧 Debug
advanced
2:30remaining
Identify why this pytest test is non-deterministic
Why does this test sometimes fail unpredictably?
PyTest
import random

def get_random_number():
    return random.randint(1, 10)

def test_random_number():
    assert get_random_number() == 5
AThe test depends on random output, so it is non-deterministic and may fail.
BThe function get_random_number always returns 5, so the test should pass.
CThe test has a syntax error in the assert statement.
DThe test is missing a pytest decorator.
Attempts:
2 left
💡 Hint
Think about what random.randint returns each time.
framework
advanced
2:30remaining
Best practice to make a pytest test deterministic when testing time-dependent code
Which approach ensures a pytest test for a function that returns the current time is deterministic?
PyTest
import time

def get_current_time():
    return time.time()
AMock time.time() to return a fixed timestamp during the test.
BUse assert get_current_time() > 0 to check it returns a positive number.
CCall get_current_time() twice and compare results.
DAdd a sleep(1) before calling get_current_time() to stabilize output.
Attempts:
2 left
💡 Hint
Think about controlling external dependencies in tests.
🧠 Conceptual
expert
2:00remaining
Why are deterministic tests important in continuous integration?
Choose the best reason why deterministic tests are critical in a CI pipeline.
AThey require less code because they do not use assertions.
BThey run faster than non-deterministic tests because they skip setup steps.
CThey allow tests to randomly fail sometimes to catch hidden bugs.
DThey ensure tests produce the same results every run, avoiding flaky failures that waste developer time.
Attempts:
2 left
💡 Hint
Think about reliability and trust in automated testing.