$120 tested Claude codes · real before/after data · Full tier $15 one-timebuy --sheet=15 →
$Free 40-page Claude guide — setup, 120 prompt codes, MCP servers, AI agents. download --free →
clskills.sh — terminal v2.4 — 2,347 skills indexed● online
[CL]Skills_
TerraformintermediateNew

Terraform Testing

Share

Write Terraform tests with Terratest and terraform test

Works with OpenClaude

You 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 version to 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, and outputs.tf structure

Steps

  1. Initialize a tests/ directory at your module root and create a Go test file like tests/terraform_test.go
  2. Set up Go module in tests directory with go mod init and add Terratest dependency via go get github.com/gruntwork-io/terratest/v2
  3. Import required Terratest packages: terraform, assert, logger, and test_structure for fixture management
  4. Create test fixtures in a tests/fixtures/ subdirectory with minimal Terraform configurations that call your main module
  5. Write test functions using Go's testing.T parameter and terraform.InitAndApply() to deploy infrastructure
  6. Use Terratest assertions like assert.Equal() to validate outputs match expected values
  7. Create .tf test files in your module root following HashiCorp's native test syntax for simpler unit-style tests
  8. Add run blocks in native tests to execute terraform plan and validate resources without applying
  9. Use variables blocks in native tests to override module variables and test different scenarios
  10. Run tests with go test -v ./tests/ for Terratest or terraform test for 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

Quick Info

CategoryTerraform
Difficultyintermediate
Version1.0.0
AuthorClaude Skills Hub
terraformtestingterratest

Install command:

curl -o ~/.claude/skills/terraform-testing.md https://clskills.in/skills/terraform/terraform-testing.md

Related Terraform Skills

Other Claude Code skills in the same category — free to download.

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.