Testing code that uses time and randomness

Testing backends Junior Python 3.13+ PostgreSQL 17+ Reviewed By Best Latest Apps

Predict the output

What do the two lines print?

Python 3.13
from collections.abc import Callable
from datetime import UTC, datetime, timedelta


def is_expired(issued: datetime,
               now: Callable[[], datetime]) -> bool:
    return now() - issued > timedelta(minutes=15)


issued = datetime(2026, 10, 31, 12, 0, tzinfo=UTC)
print(is_expired(issued, lambda: issued + timedelta(minutes=10)))
print(is_expired(issued, lambda: issued + timedelta(minutes=16)))

Options
Hint 1 of 1

Compare each delta with 15 minutes.

Choose an answer, then open Show answer below to check it.

Show answer

Answer: A False then True

Correct. 10 minutes is inside 15; 16 is past it.

Passing the clock in as a function lets a test choose 'now' exactly. 10 minutes is under the 15-minute limit and 16 is over, with no sleeps or real time involved.

Why not the others?
  • B True then True. 10 minutes hasn't expired.
  • C False then False. 16 minutes exceeds 15.
  • D A timezone error. Both datetimes are UTC-aware.

30-second answer

Inject them. Pass a clock (a function returning now) and a seeded random generator into the code, so tests control them; production passes the real clock and an unseeded generator. Then a test can step the clock past an expiry or reproduce a jitter sequence exactly, without sleeping. Store and compare times in UTC with timezone-aware datetimes.

Practise saying it · 25 s. No recording, just a timer

Test time and randomness.

Say your answer out loud, then check which of these points you covered:

  • Inject the clock
  • Seeded RNG
  • No sleeps
  • UTC-aware datetimes

Deep dive

Libraries like time-machine or freezegun patch the clock when you can't inject it.

Never sleep in tests to wait for expiry.

Log the seed when a randomised test fails so it can be replayed.

Database now() is fixed for the whole transaction in Postgres.

An injected clock Ran on Python 3.13.5 · mypy --strict
Python 3.13
from collections.abc import Callable
from datetime import UTC, datetime, timedelta


def is_expired(issued: datetime,
               now: Callable[[], datetime]) -> bool:
    return now() - issued > timedelta(minutes=15)


issued = datetime(2026, 10, 31, 12, 0, tzinfo=UTC)
print(is_expired(issued, lambda: issued + timedelta(minutes=10)))
print(is_expired(issued, lambda: issued + timedelta(minutes=16)))

Common mistakes

  • Tests that pass only at certain times of day.
  • sleep(16 * 60) in a test.
  • Unseeded randomness in assertions.

Senior take & follow-ups

Inject clock and RNG, control them in tests, UTC everywhere.

Follow-up questions

Think of your answer first, then open the hint.

  1. What does Postgres now() return inside a transaction?

    Show hint

    The transaction start time, constant within it.

  2. How do you replay a random failure?

    Show hint

    Log and reuse the seed.

Suggest a fix if something here is wrong or out of date.