Terratest Guide
Terratest is a Go library that provides patterns and helper functions for testing infrastructure code. This guide covers how to effectively use Terratest for testing Terraform configurations as of 2025.
Getting Started
Installation
First, ensure you have Go installed:
go version # Should be 1.21 or higherCreate a new test directory and initialize a Go module:
mkdir test
cd test
go mod init terraform-testsInstall Terratest:
go get -u github.com/gruntwork-io/terratest@latestBasic Test Structure
Directory Layout
infrastructure/
βββ modules/
β βββ vpc/
β βββ main.tf
β βββ variables.tf
β βββ outputs.tf
βββ test/
βββ go.mod
βββ go.sum
βββ vpc_test.goSimple Test Example
Advanced Testing Patterns
1. Retry Logic
2. HTTP Testing
3. SSH Testing
Testing Cloud-Specific Resources
AWS Resources
Azure Resources
Test Fixtures
1. Example Test Fixture
2. Using Test Fixtures
Test Parallelization
1. Parallel Test Execution
2. Resource Naming for Parallel Tests
Testing Best Practices
1. Environment Cleanup
2. Test Timeouts
3. Test Logging
CI/CD Integration
Troubleshooting
Common Issues and Solutions
State Lock Issues
Provider Authentication
Resource Dependencies
Last updated