Write Terraform tests with Terratest and terraform test
✓Works with OpenClaudeYou are a Terraform testing specialist. The user wants to write comprehensive tests for Terraform configurations using Terratest and the native terraform test framework.
What to check first
- Run
terraform versionto ensure you're using Terraform 1.6+ for native testing support - Verify Go is installed with
go version(Terratest requires Go 1.16+) - Check if your Terraform module has a
main.tf,variables.tf, andoutputs.tfstructure
Steps
- Initialize a
tests/directory at your module root and create a Go test file liketests/terraform_test.go - Set up Go module in tests directory with
go mod initand add Terratest dependency viago get github.com/gruntwork-io/terratest/v2 - Import required Terratest packages:
terraform,assert,logger, andtest_structurefor fixture management - Create test fixtures in a
tests/fixtures/subdirectory with minimal Terraform configurations that call your main module - Write test functions using Go's
testing.Tparameter andterraform.InitAndApply()to deploy infrastructure - Use Terratest assertions like
assert.Equal()to validate outputs match expected values - Create
.tftest files in your module root following HashiCorp's native test syntax for simpler unit-style tests - Add
runblocks in native tests to executeterraform planand validate resources without applying - Use
variablesblocks in native tests to override module variables and test different scenarios - Run tests with
go test -v ./tests/for Terratest orterraform testfor native tests
Code
// tests/terraform_test.go - Terratest example
package test
import (
"testing"
"github.com/gruntwork-io/terratest/v2/assert"
"github.com/gruntwork-io/terratest/v2/terraform"
"github.com/gruntwork-io/terratest/v2/test_structure"
)
func TestTerraformExample(t *testing.T) {
t.Parallel()
// Create a temporary directory for Terraform files
tmpDir := test_structure.CopyTerraformFolderToTemp(t, "..", "fixtures/basic")
// Configure Terraform options
terraformOptions := &terraform.Options{
TerraformDir: tmpDir,
Vars: map[string]interface{}{
"name" : "test-resource",
"environment" : "dev",
},
}
// Clean up resources after test
defer terraform.Destroy(t, terraformOptions)
// Init and apply Terraform
terraform.InitAndApply(t, terraformOptions)
// Validate outputs
instanceId := terraform.Output(t, terraformOptions, "instance_id")
assert.NotEmpty(t, instanceId
Note: this example was truncated in the source. See the GitHub repo for the latest full version.
Common Pitfalls
- Treating this skill as a one-shot solution — most workflows need iteration and verification
- Skipping the verification steps — you don't know it worked until you measure
- Applying this skill without understanding the underlying problem — read the related docs first
When NOT to Use This Skill
- When a simpler manual approach would take less than 10 minutes
- On critical production systems without testing in staging first
- When you don't have permission or authorization to make these changes
How to Verify It Worked
- Run the verification steps documented above
- Compare the output against your expected baseline
- Check logs for any warnings or errors — silent failures are the worst kind
Production Considerations
- Test in staging before deploying to production
- Have a rollback plan — every change should be reversible
- Monitor the affected systems for at least 24 hours after the change
Related Terraform Skills
Other Claude Code skills in the same category — free to download.
Terraform Module
Create reusable Terraform modules with variables and outputs
Terraform State
Manage Terraform state with remote backends (S3, Azure, GCS)
Terraform Workspace
Configure Terraform workspaces for multi-environment management
Terraform Provider
Write custom Terraform providers with Go
Terraform Import
Import existing infrastructure into Terraform state
Terraform CI/CD
Set up Terraform CI/CD with GitHub Actions and Atlantis
Terraform Security
Scan Terraform for security issues with tfsec and Checkov
Want a Terraform skill personalized to YOUR project?
This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.