Integration test strategy
What integration testing adds
A test of one expression can establish its result; integration tests exercise interactions between components, such as a module, provider, API and application. A real plan can expose provider or input issues without proving actual deployment success. An apply can succeed while the application returns an error. Choose the smallest test that provides the missing evidence. Use mocked plans for fast contracts, local integration tests for real local state, and optional authorized remote integration only for behavior that requires a real service.
Build a bounded local test
The supplied fixture creates a synthetic inventory record, not a server. It uses Terraform 1.14.8 provisionally and the built-in terraform_data resource. The output is read from the resource after apply.
terraform { required_version = "= 1.14.8"}variable "environment" { type = string default = "dev" validation { condition = contains(["dev", "test"], var.environment) error_message = "Use dev or test in this lab." }}resource "terraform_data" "record" { input = "inventory-${var.environment}"}output "record_name" { value = terraform_data.record.output}The native test file uses an apply run to assert the stored output. It does not need a third-party provider.
run "test_inventory" { command = apply variables { environment = "test" } assert { condition = output.record_name == "inventory-test" error_message = "Unexpected inventory name." }}In a fresh reviewed lab copy, initialize with backend disabled and run terraform test -filter=tests/inventory.tftest.hcl. Expected result: the assertion succeeds and Terraform attempts test cleanup. This result is predicted, not observed. Inspect any reported cleanup failures. A test apply is real execution, even when the target is local.
Inspect actual local state manually
Alternatively, use a separate fresh copy of the same fixture for an explicit CLI workflow. Check the version and confirm the default local backend, no external plugins and no real credentials. Do not mix the test-managed state with this manual run.
terraform init -backend=falseterraform plan -var=environment=test -out=lab.tfplan# Inspect the plan; proceed only after verifying one local create.terraform show lab.tfplanterraform apply lab.tfplanterraform output -raw record_nameterraform state listExecute one command at a time; stop if plan review differs from the stated expectation. The expected output is inventory-test (raw output may have no trailing newline), and state list should include terraform_data.record. These assertions establish that the local record exists and exposes the correct value, not that any cloud service works.
Design a service-level test
For a fictional private inventory service, define an actual request contract rather than merely asserting that a resource exists. Use controlled synthetic identities and approved test endpoints. The following is a specification, not an executable network test.
ALLOW: synthetic reader -> GET /inventory -> 200, expected recordDENY: synthetic guest -> GET /inventory -> 403DENY: synthetic reader -> POST /inventory -> 403CLEANUP: test-owned resource inventory is emptyAssert both authorized and denied operations with actual response status and body; verify denied writes leave data unchanged. Confirm the selected identity is the intended test principal. Include a bounded readiness retry for documented eventual consistency, a timeout and a maximum number of requests. Never accept all error responses as proof of correct authorization: a connection failure or server error is different from an expected denial.
Cleanup is part of the test
Terratest is a Go-based integration harness that can drive infrastructure workflows and service assertions. Register cleanup before creating resources, and make cleanup errors visible. Deferred cleanup is useful but cannot guarantee recovery after runner termination, lost credentials or provider failures. Use explicit ownership, isolated state, independent cleanup monitoring, maximum runtime and reviewed cost limits for optional live tests. The core lab uses no cloud account. For the manual local fixture only, after reviewing the state and confirming ownership, use the following steps:
terraform plan -destroy -var=environment=test -out=cleanup.tfplan# Review the destroy plan; it must contain only the lab record.terraform show cleanup.tfplanterraform apply cleanup.tfplanterraform state listThe final state list should be empty; retain sanitized evidence of that check. If cleanup fails, stop and inspect the lab state, preserve it for recovery and report the failure. Never delete state as a substitute for destroying real resources.
Choose the release gates
A useful pipeline runs formatting and validation first, then lint and policy checks, then mocked contract tests, then approved real integration tests for changes that require them. Test compatibility across a deliberately selected CLI and provider matrix, not every possible combination. Keep evidence of the inputs, revision, tool versions, rules, assertions, skipped checks and cleanup. A passing integration test is evidence for its bounded scenario, not a universal proof. The next chapter discusses secrets and state protection.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Integration test strategy — knowledge check
Review static analysis and integration test strategy.
Which evidence best demonstrates a deployed endpoint behaves correctly?
What is true about an apply-based native test?
An unauthorized request times out. Is this proof of correct authorization?
When should cleanup be registered in an integration harness?
What should happen if cleanup fails?