Assert a module contract

Choose an observable module promise such as a naming rule, tag value, or derived output. Provide the relevant variables and assert that promise directly. A test that merely repeats the resource expression may not catch a mistaken requirement, so agree on the expected contract independently of the current configuration before writing the assertion.

Use a run block with command = plan when planned configuration is sufficient for the question. Some values are unknown until apply, so a plan assertion cannot establish every output. Match the chosen command to the evidence needed rather than assuming that a nonmutating plan and an actual provider operation are interchangeable test boundaries.

Control provider behavior where appropriate

Supported mock-provider features can replace provider operations for focused configuration checks. Match the Terraform version to the APIs used by the project and document the mocked values. A mocked result can verify module handling of supplied data, but it cannot establish that a cloud service accepts the real request or behaves the same way.

Use expect_failures for the documented custom-condition cases rather than treating it as a general way to ignore errors. The expected failure must align with the run operation. An apply run that fails during planning has different semantics, so read the diagnostics instead of weakening the test until an unrelated error appears acceptable.

Keep live infrastructure tests explicit

If a test needs apply, use an approved isolated environment with a clear resource and cleanup plan. Do not assume that the test command is harmless because its name includes testing. Provider calls can incur costs or modify resources, and cleanup can fail, so the execution boundary needs deliberate ownership before the run begins.

Keep state and credentials out of published logs and review artifacts. Record the module revision, Terraform version, and provider configuration relevant to the result. Separate configuration-level checks from live integration evidence in reports so reviewers know whether a passing assertion reflects a planned value, a mocked response, or an actual provider operation.

Working sheet

Terraform test boundary choices
QuestionTest modeEvidence limit
Derived namePlan assertionNo live provider proof
Variable validationPlan expected failureSpecific custom condition
Handling provider outputSupported mockControlled value only
Cloud integrationApproved isolated applyReal resource side effects
Download this worksheet (CSV)

Questions about terraform unit testing: checks, mocks and plan tests

Does terraform test always avoid real resources?

No. Apply runs can create infrastructure. Choose the command and environment deliberately.

Do provider mocks prove cloud compatibility?

No. They support controlled configuration checks. Live provider behavior needs separate integration evidence.

How should I use the working sheet?

Download the CSV and replace the sample rows with evidence from your repository. Record a clear acceptance check and an owner for each item. Keep unresolved assumptions visible rather than treating the example as a completed assessment.

Sources and further reading

Examples are illustrative. They are not customer results, security guarantees, or a substitute for checking the documentation for your installed version.

Make the next change verifiable.

Getzilla is an open source workflow for changes to existing repositories. Inspect the project, define acceptance checks, and keep independent review in the loop.

See the repository workflow See current access and pricing