liveBook · Manning

Brittle test

As explained in 100 Go Mistakes and How to Avoid Them

A brittle test is a test whose result is unusually sensitive to incidental conditions such as timing, environment, or implementation details.

A brittle test depends on incidental timing rather than controlled behavior. A cache test that creates events with time.Now(), trims after 15 milliseconds, and expects two events can fail when the test machine becomes busy. section 11.6

Time-dependent cache test

Replace moving timestamps with fixed values and inject the clock: configure Cache.now to return 12:00:00.06, then trim by 15 milliseconds. The test can assert the result without sleeps or races against time.Now(). section 11.6

Reduce brittleness by changing where time enters the design:

  • Inject a clock dependency, using time.Now in production and a fixed function in tests. section 11.6
  • Pass the current time into TrimOlderThan when callers can provide it explicitly. section 11.6
func TestCache_TrimOlderThan(t *testing.T) {
    events := []Event{                                         #1
        {Timestamp: parseTime(t, "2020-01-01T12:00:00.04Z")},
        {Timestamp: parseTime(t, "2020-01-01T12:00:00.05Z")},
        {Timestamp: parseTime(t, "2020-01-01T12:00:00.06Z")},
    }
    cache := &Cache{now: func() time.Time {                    #2
        return parseTime(t, "2020-01-01T12:00:00.06Z")
    }}
    cache.Add(events)
    cache.TrimOlderThan(15 * time.Millisecond)
    // ...
}
 
func parseTime(t *testing.T, timestamp string) time.Time {
    // ...
}

Shows fixed event timestamps and an injected clock producing deterministic cache behavior. section 11.6

How 100 Go Mistakes and How to Avoid Them covers it

How deep it goes
Explains a concrete failure and compares clock injection with explicit-time alternatives, without covering every technique.
Examples
Uses an in-memory event cache, millisecond offsets, fixed timestamps, and an injectable now function.
What it assumes
Assumes familiarity with Go unit tests, time.Time, durations, and basic dependency injection.
What you can do after
Diagnose timing sensitivity and redesign tests around injected or explicitly supplied time.
How it connects
Connects time.Now with dependency injection, unit testing, sleeps, and deterministic timestamps.

Also explained in

Compare how 3 books explain brittle test →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of 100 Go Mistakes and How to Avoid Them. The section links point to the parts of the book it draws on.