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.
More on Testing and Policy as Code
How do you test infrastructure code, and what does the testing pyramid look like?BeginnerWhat does the static-analysis layer catch?BeginnerWhat does tflint do, and how is it configured?BeginnerHow do Checkov and Trivy differ?IntermediateHow do mock providers work in `terraform test`?AdvancedWhen do you use Terratest instead of `terraform test`?Intermediate