Your first Terraform test
InitAndApply, then assert.
A Terratest test is a normal Go test function that uses the terraform module. You point it at a Terraform directory via Options, call InitAndApply to deploy, read outputs, assert on them, and destroy at the end. The minimal shape is short and readable even if you are not fluent in Go — it is mostly “apply this, check that.”
package testimport ("testing""github.com/gruntwork-io/terratest/modules/terraform""github.com/stretchr/testify/assert")func TestVpcModule(t *testing.T) {opts := &terraform.Options{ TerraformDir: "../examples/vpc" }defer terraform.Destroy(t, opts) // always clean upterraform.InitAndApply(t, opts) // deployvpcId := terraform.Output(t, opts, "vpc_id")assert.NotEmpty(t, vpcId) // assert on the real output}
Options, variables, and outputs
Options carries everything about the run: the directory, input Vars (to test the module with different inputs), EnvVars, and backend config. terraform.Output reads a single output; OutputMap and OutputList read structured ones. You assert with a normal Go assertion library (testify). Run it with go test, and it will really apply your Terraform against whatever cloud your credentials point at — so run it against a sandbox.
$ cd test && go test -v -timeout 30m -run TestVpcModule=== RUN TestVpcModule... terraform apply ...... assertions ...... terraform destroy ...--- PASS: TestVpcModule (214.3s)