Test stages & speed
Skip stages; iterate fast.
A full deploy-validate-destroy cycle is slow, which makes iterating on the validation code painful — you wait minutes for a deploy every time you tweak an assertion. Terratest’s test_structure stages solve this: you wrap deploy, validate, and teardown in named stages, and can skip stages via environment variables. So you deploy once, then re-run just the validate stage repeatedly while writing assertions, and run teardown only when done.
import "github.com/gruntwork-io/terratest/modules/test-structure"func TestStaged(t *testing.T) {dir := "../examples/thing"defer test_structure.RunTestStage(t, "teardown", func() {terraform.Destroy(t, test_structure.LoadTerraformOptions(t, dir))})test_structure.RunTestStage(t, "deploy", func() {opts := &terraform.Options{ TerraformDir: dir }test_structure.SaveTerraformOptions(t, dir, opts)terraform.InitAndApply(t, opts)})test_structure.RunTestStage(t, "validate", func() { /* assertions */ })}
Iterate fast, then run it all
With SKIP_teardown=true and SKIP_deploy set after the first run, you keep the infrastructure up and re-run only validate — turning a 5-minute cycle into seconds while you develop tests. When finished, unset the skips to run the whole thing clean (and let teardown destroy). In CI you run all stages; stage-skipping is a local development accelerator. It is the single biggest quality-of-life feature for authoring infra tests.
# first run: deploy + validate, keep it up$ SKIP_teardown=true go test -run TestStaged -timeout 30m# iterate on assertions against the SAME deployed infra, fast:$ SKIP_deploy=true SKIP_teardown=true go test -run TestStaged# done: run teardown$ SKIP_deploy=true go test -run TestStaged