← Concepts

阅读中文版本

Hermetic Test Environment

A hermetic test environment has no external dependencies — network, clock, filesystem, sibling process — so the test result depends only on the code under test and the inputs the test controls.

testing

Definition

A hermetic test environment is one in which every external dependency is replaced with an in-process double or a controlled local service, so that the test outcome is a function of the code under test and the test inputs alone, with no contribution from network state, shared filesystem, or ambient process timing.

Context and scope

Applied in integration test suites, CI pipelines, and any test whose flakiness traces to external dependencies the test does not control.

Common misconceptions

  • Mocking everything makes the test hermetic — hermeticity requires the doubles behave like the real dependency; mocks that return canned values without contract are hermetic but wrong
  • Hermetic tests are slow because they start everything — a hermetic environment can be a single process with in-memory doubles; the slowness comes from over-starting

Anti-patterns

  • Calling a shared staging API from a test, so the test fails when staging is down or the data changes
  • Using the real system clock in a test, so the test passes at 11 p.m. and fails at midnight

Related concepts

Authoritative source

https://testing.googleblog.com/2012/10/hermetic-environment.html

What it is

A hermetic test environment is one in which every external dependency is replaced with an in-process double or a controlled local service, so that the test outcome is a function of the code under test and the test inputs alone, with no contribution from network state, shared filesystem, or ambient process timing. Applied in integration test suites, CI pipelines, and any test whose flakiness traces to external dependencies the test does not control. The distinguishing question is what counts as evidence: a test that passes by accident is worse than no test, because it manufactures false confidence that propagates into release decisions.

How it works

Sound tests exercise a specific behavior contract, isolate the unit under test from environmental noise, and fail with a diagnostic that points at the cause rather than the symptom. Determinism is engineered, not assumed: clocks are fixed, randomness is seeded, and external state is replaced with doubles whose interactions are asserted, not merely stubbed. Coverage measures which behaviors were exercised, not which lines were visited.

When to apply and pitfalls

Apply where the cost of an undetected regression exceeds the cost of writing and maintaining the test. The recurring pitfall is testing the implementation rather than the behavior — such tests break on every refactor and teach nothing. Equally damaging are tests that assert only the happy path, leaving the error branches — where most production bugs live — unexercised.