$npx -y skills add hashicorp/agent-skills --skill terraform-testterraform-test is an agent skill published from hashicorp/agent-skills, installable through the skills CLI.
| 1 | # Terraform Test |
| 2 | |
| 3 | Terraform's built-in testing framework validates that configuration updates don't introduce breaking changes. Tests run against temporary resources, protecting existing infrastructure and state files. |
| 4 | |
| 5 | ## Reference Files |
| 6 | |
| 7 | - `references/MOCK_PROVIDERS.md` — Mock provider syntax, common defaults, when to use mocks (Terraform 1.7.0+ only — skip if the user's version is below 1.7) |
| 8 | - `references/CI_CD.md` — GitHub Actions and GitLab CI pipeline examples |
| 9 | - `references/EXAMPLES.md` — Complete example test suite (unit, integration, and mock tests for a VPC module) |
| 10 | |
| 11 | Read the relevant reference file when the user asks about mocking, CI/CD integration, or wants a full example. |
| 12 | |
| 13 | ## Core Concepts |
| 14 | |
| 15 | - **Test file** (`.tftest.hcl` / `.tftest.json`): Contains `run` blocks that validate your configuration |
| 16 | - **Run block**: A single test scenario with optional variables, providers, and assertions |
| 17 | - **Assert block**: Conditions that must be true for the test to pass |
| 18 | - **Mock provider**: Simulates provider behavior without real infrastructure (Terraform 1.7.0+) |
| 19 | - **Test modes**: `apply` (default, creates real resources) or `plan` (validates logic only) |
| 20 | |
| 21 | ## File Structure |
| 22 | |
| 23 | ``` |
| 24 | my-module/ |
| 25 | ├── main.tf |
| 26 | ├── variables.tf |
| 27 | ├── outputs.tf |
| 28 | └── tests/ |
| 29 | ├── defaults_unit_test.tftest.hcl # plan mode — fast, no resources |
| 30 | ├── validation_unit_test.tftest.hcl # plan mode |
| 31 | └── full_stack_integration_test.tftest.hcl # apply mode — creates real resources |
| 32 | ``` |
| 33 | |
| 34 | Use `*_unit_test.tftest.hcl` for plan-mode tests and `*_integration_test.tftest.hcl` for apply-mode tests so they can be filtered separately in CI. |
| 35 | |
| 36 | ## Test File Structure |
| 37 | |
| 38 | ```hcl |
| 39 | # Optional: test-wide settings |
| 40 | test { |
| 41 | parallel = true # Enable parallel execution for all run blocks (default: false) |
| 42 | } |
| 43 | |
| 44 | # Optional: file-level variables (highest precedence, override all other sources) |
| 45 | variables { |
| 46 | aws_region = "us-west-2" |
| 47 | instance_type = "t2.micro" |
| 48 | } |
| 49 | |
| 50 | # Optional: provider configuration |
| 51 | provider "aws" { |
| 52 | region = var.aws_region |
| 53 | } |
| 54 | |
| 55 | # Required: at least one run block |
| 56 | run "test_default_configuration" { |
| 57 | command = plan |
| 58 | |
| 59 | assert { |
| 60 | condition = aws_instance.example.instance_type == "t2.micro" |
| 61 | error_message = "Instance type should be t2.micro by default" |
| 62 | } |
| 63 | } |
| 64 | ``` |
| 65 | |
| 66 | ## Run Block |
| 67 | |
| 68 | ```hcl |
| 69 | run "test_name" { |
| 70 | command = plan # or apply (default) |
| 71 | parallel = true # optional, since v1.9.0 |
| 72 | |
| 73 | # Override file-level variables |
| 74 | variables { |
| 75 | instance_type = "t3.large" |
| 76 | } |
| 77 | |
| 78 | # Reference a specific module |
| 79 | module { |
| 80 | source = "./modules/vpc" # local or registry only (not git/http) |
| 81 | version = "5.0.0" # registry modules only |
| 82 | } |
| 83 | |
| 84 | # Control state isolation |
| 85 | state_key = "shared_state" # since v1.9.0 |
| 86 | |
| 87 | # Plan behavior |
| 88 | plan_options { |
| 89 | mode = refresh-only # or normal (default) |
| 90 | refresh = true |
| 91 | replace = [aws_instance.example] |
| 92 | target = [aws_instance.example] |
| 93 | } |
| 94 | |
| 95 | # Assertions |
| 96 | assert { |
| 97 | condition = aws_instance.example.id != "" |
| 98 | error_message = "Instance should have a valid ID" |
| 99 | } |
| 100 | |
| 101 | # Expected failures (test passes if these fail) |
| 102 | expect_failures = [ |
| 103 | var.instance_count |
| 104 | ] |
| 105 | } |
| 106 | ``` |
| 107 | |
| 108 | ## Common Test Patterns |
| 109 | |
| 110 | ### Validate outputs |
| 111 | |
| 112 | ```hcl |
| 113 | run "test_outputs" { |
| 114 | command = plan |
| 115 | |
| 116 | assert { |
| 117 | condition = output.vpc_id != null |
| 118 | error_message = "VPC ID output must be defined" |
| 119 | } |
| 120 | |
| 121 | assert { |
| 122 | condition = can(regex("^vpc-", output.vpc_id)) |
| 123 | error_message = "VPC ID should start with 'vpc-'" |
| 124 | } |
| 125 | } |
| 126 | ``` |
| 127 | |
| 128 | ### Conditional resources |
| 129 | |
| 130 | ```hcl |
| 131 | run "test_nat_gateway_disabled" { |
| 132 | command = plan |
| 133 | |
| 134 | variables { |
| 135 | create_nat_gateway = false |
| 136 | } |
| 137 | |
| 138 | assert { |
| 139 | condition = length(aws_nat_gateway.main) == 0 |
| 140 | error_message = "NAT gateway should not be created when disabled" |
| 141 | } |
| 142 | } |
| 143 | ``` |
| 144 | |
| 145 | ### Resource counts |
| 146 | |
| 147 | ```hcl |
| 148 | run "test_resource_count" { |
| 149 | command = plan |
| 150 | |
| 151 | variables { |
| 152 | instance_count = 3 |
| 153 | } |
| 154 | |
| 155 | assert { |
| 156 | condition = length(aws_instance.workers) == 3 |
| 157 | error_message = "Should create exactly 3 worker instances" |
| 158 | } |
| 159 | } |
| 160 | ``` |
| 161 | |
| 162 | ### Tags |
| 163 | |
| 164 | ```hcl |
| 165 | run "test_resource_tags" { |
| 166 | command = plan |
| 167 | |
| 168 | variables { |
| 169 | common_tags = { |
| 170 | Environment = "production" |
| 171 | ManagedBy = "Terraform" |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | assert { |
| 176 | condition = aws_instance.example.tags["Environment"] == "production" |
| 177 | error_message = "Environment tag should be set correctly" |
| 178 | } |
| 179 | |
| 180 | assert { |
| 181 | condition = aws_instance.example.tags["ManagedBy"] == "Terraform" |
| 182 | error_message = "ManagedBy tag should be set correctly" |
| 183 | } |
| 184 | } |
| 185 | ``` |
| 186 | |
| 187 | ### Data sources |
| 188 | |
| 189 | ```hcl |
| 190 | run "test_data_sour |