Bird
Raised Fist0
PyTesttesting~20 mins

Deterministic tests in PyTest - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1. What does it mean for a test to be deterministic in pytest?
easy
A. The test depends on external services to pass.
B. The test always produces the same result given the same inputs.
C. The test uses random data every time it runs.
D. The test runs faster than non-deterministic tests.

Solution

  1. Step 1: Understand the meaning of deterministic tests

    Deterministic tests produce consistent results every time they run with the same inputs.
  2. Step 2: Compare options with this definition

    Only The test always produces the same result given the same inputs. states that the test always produces the same result given the same inputs, matching the definition.
  3. Final Answer:

    The test always produces the same result given the same inputs. -> Option B
  4. Quick Check:

    Deterministic = Same result every time [OK]
Hint: Deterministic means repeatable results every run [OK]
Common Mistakes:
  • Confusing deterministic with fast tests
  • Thinking randomness is allowed in deterministic tests
  • Assuming external dependencies make tests deterministic
2. Which of the following is the correct way to fix randomness in a pytest test to make it deterministic?
easy
A. Use a fixed seed for the random number generator.
B. Remove all assertions from the test.
C. Run the test multiple times and average the results.
D. Use random data without controlling it.

Solution

  1. Step 1: Identify how to control randomness

    Setting a fixed seed for the random number generator ensures the same random values each run.
  2. Step 2: Evaluate options for fixing randomness

    Only Use a fixed seed for the random number generator. uses a fixed seed, which makes the test deterministic. Other options do not control randomness properly.
  3. Final Answer:

    Use a fixed seed for the random number generator. -> Option A
  4. Quick Check:

    Fixed seed = deterministic randomness [OK]
Hint: Fix random seed to control randomness [OK]
Common Mistakes:
  • Removing assertions does not fix randomness
  • Averaging results does not make test deterministic
  • Using uncontrolled random data causes flaky tests
3. Given this pytest test code, what will be the output when run twice?
import random

def test_random_number():
    random.seed(42)
    num = random.randint(1, 10)
    assert num == 2
medium
A. The test will pass both times because the seed fixes the random number.
B. The test will fail both times because 2 is not the generated number.
C. The test will pass the first time and fail the second time.
D. The test will fail the first time and pass the second time.

Solution

  1. Step 1: Understand the effect of setting random.seed(42)

    Setting the seed to 42 makes random.randint(1, 10) produce the same number every run.
  2. Step 2: Check the generated number and assertion

    With seed 42, random.randint(1, 10) returns 2, so the assertion num == 2 passes every time.
  3. Final Answer:

    The test will pass both times because the seed fixes the random number. -> Option A
  4. Quick Check:

    Seed 42 -> randint = 2 == 2 -> assert passes [OK]
Hint: Seed fixes random output to 2, so assertion num==2 passes consistently [OK]
Common Mistakes:
  • Thinking the test passes because of seed (but checks wrong number)
  • Assuming random changes every run despite seed
  • Not verifying that seed(42) produces 2 for randint(1,10)
4. This pytest test sometimes fails randomly. What is the main issue?
import random

def test_random_fail():
    num = random.randint(1, 10)
    assert num == 5
medium
A. The test function name is invalid.
B. The assertion syntax is incorrect.
C. The test does not fix the random seed, causing non-deterministic results.
D. The random module is not imported.

Solution

  1. Step 1: Identify randomness control in the test

    The test uses random.randint without setting a seed, so output varies each run.
  2. Step 2: Understand why this causes test failure

    Because the number is random, the assertion num == 5 sometimes fails, making the test flaky.
  3. Final Answer:

    The test does not fix the random seed, causing non-deterministic results. -> Option C
  4. Quick Check:

    No seed -> random output varies -> flaky test [OK]
Hint: No seed means random output, causing flaky tests [OK]
Common Mistakes:
  • Thinking assertion syntax is wrong
  • Assuming test name affects determinism
  • Ignoring that random module is correctly imported
5. You want to write a deterministic pytest test that checks if a function returns the current date. Which approach ensures determinism?
hard
A. Call the function without any changes and assert the result.
B. Run the test only once to avoid variability.
C. Use the actual current date and accept test failures on different days.
D. Mock the current date to a fixed value during the test.

Solution

  1. Step 1: Understand why current date causes non-determinism

    The current date changes every day, so tests using it without control will fail on different days.
  2. Step 2: Identify how to fix the date for deterministic testing

    Mocking the current date to a fixed value ensures the test always sees the same date and passes consistently.
  3. Final Answer:

    Mock the current date to a fixed value during the test. -> Option D
  4. Quick Check:

    Mock date -> fixed input -> deterministic test [OK]
Hint: Mock changing data like dates for stable tests [OK]
Common Mistakes:
  • Using real current date causes flaky tests
  • Ignoring mocking leads to non-deterministic results
  • Running test once does not guarantee determinism