Build fluency with Terratest — Go-based infrastructure testing, retry patterns, and parallel test execution.
0 / 5 completed
1 / 5
At standup, a new engineer asks what Terratest does. Which answer is correct?
Terratest is a Go testing library that drives real Terraform workflows: terraform.InitAndApply() provisions actual infrastructure, your Go test code makes assertions (HTTP checks, AWS SDK calls, DNS lookups), and terraform.Destroy() cleans up. It tests real infrastructure behaviour, not just plan output.
2 / 5
In a PR review, a reviewer notices defer terraform.Destroy(t, terraformOptions). Why is defer used here?
Using defer for terraform.Destroy ensures the cleanup runs no matter how the test exits — whether it completes normally, encounters a test failure, or panics. Without defer, a test failure mid-way would leave cloud resources running indefinitely, causing cost overruns and naming conflicts for future test runs.
3 / 5
A Terratest test is flaky because it fails on DNS propagation delays after provisioning. What is the correct fix?
retry.DoWithRetry is Terratest's built-in retry helper. It retries a function up to maxRetries times with sleepBetweenRetries between attempts, returning an error if all retries fail. This correctly handles eventual-consistency scenarios like DNS propagation, load-balancer health checks, and service startup — without a brittle fixed sleep.
4 / 5
In a design review on test performance, the team discusses parallel test execution with Terratest. What is the correct approach?
Terratest supports parallel test execution via Go's standard t.Parallel(). The critical companion practice is generating unique resource name prefixes per test (e.g. using random.UniqueId()) so that parallel tests don't collide on cloud resource names. Each test manages its own isolated Terraform state, so state locking is per-test-run, not global.
5 / 5
During a code review, a colleague uses terraform.Output(t, opts, "instance_ip"). What does this function do?
terraform.Output runs terraform output -json under the hood and returns the value of a declared output from your Terraform configuration. This lets your Go test code assert against real infrastructure values — IP addresses, ARNs, endpoint URLs — that were produced by the apply step.