Skip to main content

TEST004-flaky-time — Test Uses Time or Randomness

Dimension: TestSmell Default severity: Medium Languages: All CWE: CWE-362 Last reviewed: 2026-05-06

What it detects​

For every test function (is_test == true), scans the function body for tokens that indicate reliance on wall-clock time or non-deterministic randomness. One finding per unique token match is emitted.

Detected tokens​

TokenContext
sleeptime.sleep (Python), Thread::sleep (Rust)
setTimeoutJavaScript / TypeScript
Date.now()JavaScript / TypeScript
SystemTime::now()Rust
Instant::now()Rust
time.time()Python
time.sleepPython
Math.randomJavaScript / TypeScript
random.randomPython
rand::randomRust

Why it matters​

Tests that depend on elapsed time or random values can pass on one run and fail on another ("flaky tests"), eroding trust in the CI pipeline and wasting developer time investigating spurious failures.

Example — flagged​

Python:

def test_delay():
time.sleep(0.5) # ← flagged
assert do_work() is not None

Rust:

#[test]
fn test_timing() {
let start = Instant::now(); // ← flagged
assert!(start.elapsed().as_nanos() >= 0);
}

Example — not flagged​

#[test]
fn test_pure_logic() {
assert_eq!(1 + 1, 2); // ← no time/randomness, clean
}

Fix guidance​

  • Use dependency injection to pass a clock or RNG into code under test.
  • Replace time.sleep delays with event-driven synchronisation.
  • For randomness, supply a fixed seed in tests.
  • Use a test-specific fake / stub for time-sensitive operations.

Implementation​

References​