CoursesTerratestIntermediate

Test stages & speed

Skip stages; iterate fast.

Advanced12 min · lesson 7 of 12

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.

STAGE-SKIPPING: DEPLOY ONCE, ITERATE FAST
1Deploy once
SKIP_teardown=true — leave infra up
2Iterate validate
SKIP_deploy + SKIP_teardown, re-run assertions
3Repeat until green
seconds per cycle against the same infra
4Teardown
SKIP_deploy=true — destroy stage runs
Local accelerator only. In CI run all stages; never leave SKIP_teardown set or you leak resources.
staged test
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.

terminal
# 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
Skipped teardown means you own the cleanup
SKIP_teardown leaves real infrastructure running so you can iterate — which is the point, but it also means you must remember to run the teardown stage (or destroy manually) when done, or you have leaked resources exactly as if a test failed. Stage-skipping is a local tool; never leave SKIP_teardown set in CI, and reconcile your sandbox account after a local session that used it.