CoursesTerratestIntermediate

Parallelism & fixtures

Isolated, concurrent tests.

Advanced12 min · lesson 8 of 12

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.

parallel test
func TestModuleA(t *testing.T) {
t.Parallel() // run concurrently
name := "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.

isolated fixture
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
Parallel without isolation is a collision, not a speedup
Adding t.Parallel() to tests that share resource names, directories, or state does not speed things up — it makes them fail intermittently as they overwrite each other. Isolate first (unique names, temp folder copies, separate state/namespaces), then parallelize. A parallel suite is only as reliable as its isolation; get isolation wrong and you trade slow-but-stable for fast-and-flaky.