Complete worked example
python
import random
def expires_at(ttl, clock):
return clock() + ttl
def choose_two(rng):
return rng.sample(["python", "sql", "pytest"], 2)
def test_expiry_uses_injected_time():
assert expires_at(10, lambda: 100) == 110
def test_owned_generators_repeat():
assert choose_two(random.Random(7)) == choose_two(random.Random(7))- Reasoning
- The worked expiry helper adds a fixed lifetime to an injected clock. A lambda supplies a known instant, so the expected expiry is literal and deterministic. The sampling test constructs two independent generators with the same seed and compares their outputs. The sampling assertion demonstrates reproducibility within this tested runtime, not a promise that every Python version or distribution uses an eternally identical sequence. For algorithmic correctness also assert properties such as population membership and count, rather than treating a seed as the entire specification.