Interview Prep/Terraform/Testing and Policy as Code

How does `terraform test` work?

BeginnerProductionTesting and Policy as Code
Answer

Create tests/*.tftest.hcl with run blocks. Each run plans or applies the configuration (default is apply) with optional variables, evaluates assert blocks and tears down at the end in reverse order.

run "invalid_cidr_rejected" {
  command = plan
  variables { vpc_cidr = "not-a-cidr" }
  expect_failures = [var.vpc_cidr]
}

Run with terraform test or terraform test -filter=tests/vpc.tftest.hcl -verbose.

All Terraform questions Roadmap: Terraform in production

More on Testing and Policy as Code