Challenge - 5 Problems
Deterministic Testing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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
Attempts:
2 left
💡 Hint
Check the add function and the assertions carefully.
✗ Incorrect
The add function correctly sums two numbers. All assertions match expected sums, so the test passes.
❓ assertion
intermediate1: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'
Attempts:
2 left
💡 Hint
Check string equality and case sensitivity.
✗ Incorrect
Option C correctly asserts the function returns the exact string 'hello'. Option C uses 'is' which is not reliable for string equality.
🔧 Debug
advanced2: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
Attempts:
2 left
💡 Hint
Think about what random.randint returns each time.
✗ Incorrect
The function returns a random number between 1 and 10, so the assertion that it equals 5 is only sometimes true, causing flaky test failures.
❓ framework
advanced2: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()
Attempts:
2 left
💡 Hint
Think about controlling external dependencies in tests.
✗ Incorrect
Mocking time.time() fixes the returned value, making the test deterministic and repeatable.
🧠 Conceptual
expert2:00remaining
Why are deterministic tests important in continuous integration?
Choose the best reason why deterministic tests are critical in a CI pipeline.
Attempts:
2 left
💡 Hint
Think about reliability and trust in automated testing.
✗ Incorrect
Deterministic tests give consistent results, so developers can trust test outcomes and fix real issues without chasing random failures.