Parallelism & fixtures
Isolated, concurrent tests.
Infra tests are slow, so running them concurrently matters. Go’s testing supports parallelism via t.Parallel(), and Terratest tests can run in parallel — but only if each test is fully isolated, because two tests creating the same-named resource in the same account will collide. Isolation means unique names, unique namespaces, and ideally separate state, all derived from a per-test random ID. Parallelism plus isolation turns a 40-minute serial suite into a few minutes.
func TestModuleA(t *testing.T) {t.Parallel() // run concurrentlyname := "a-" + strings.ToLower(random.UniqueId())opts := &terraform.Options{TerraformDir: "../examples/a",Vars: map[string]interface{}{ "name": name }, // unique -> no collision}defer terraform.Destroy(t, opts)terraform.InitAndApply(t, opts)// assertions}
Fixtures and copying
Two tests running in the same Terraform directory can clobber each other’s .terraform and state, so a common practice is CopyTerraformFolderToTemp — each test copies the module to its own temp dir and runs there, fully isolated. Combined with unique resource names and per-test namespaces, this makes parallel runs safe. The rule is simple: anything two concurrent tests could share (a name, a directory, a state key) must be made unique per test.
testFolder := test_structure.CopyTerraformFolderToTemp(t, "../", "examples/a")opts := &terraform.Options{ TerraformDir: testFolder } // each test in its own copy// now parallel runs cannot clobber each other’s working directory or state