Skip to content

Commit 70ff2d3

Browse files
terraform test: variable definitions can now be provided directly in test files (#594)
* terraform test: variable definitions can now be provided directly in test files * Apply suggestions from code review Co-authored-by: Robin Norwood <[email protected]> --------- Co-authored-by: Robin Norwood <[email protected]>
1 parent 666b67e commit 70ff2d3

File tree

1 file changed

+30
-3
lines changed
  • content/terraform/v1.13.x (beta)/docs/language/tests

1 file changed

+30
-3
lines changed

content/terraform/v1.13.x (beta)/docs/language/tests/index.mdx

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ Each test file contains the following root level attributes and blocks:
2626

2727
- Zero to one [`test`](#test-block) blocks.
2828
- One to many [`run`](#run-blocks) blocks.
29+
- Zero to many [`variable`](#variables) blocks.
2930
- Zero to one [`variables`](#variables) block.
3031
- Zero to many [`provider`](#providers) blocks.
3132

@@ -174,12 +175,38 @@ Additionally, test assertions can directly reference outputs from current and pr
174175

175176
## Variables
176177

177-
You can provide values for [Input Variables](/terraform/language/values/variables) within your configuration directly from your test files.
178+
You can both define variables for your test file and provide values for [Input Variables](/terraform/language/values/variables) within your configuration directly from your test files.
178179

179-
The test file syntax supports `variables` blocks at both the root level and within `run` blocks. Terraform passes all variable values from the test file into all `run` blocks within the file. You can override variable values for a particular `run` block with values provided directly within that `run` block.
180+
The test file syntax supports `variable` blocks at the root level. These blocks allow you to define new variable values that can be referenced and shared by providers, other variables, and run blocks defined within your test file.
180181

181182
Adding to the test file from the [example](#example) above:
182183

184+
```hcl
185+
# variables.tftest.hcl
186+
187+
variable "test_input" {
188+
type = string
189+
}
190+
191+
run "uses_test_input" {
192+
193+
command = plan
194+
195+
variables {
196+
bucket_prefix = var.test_input
197+
}
198+
199+
assert {
200+
condition = aws_s3_bucket.bucket.bucket == var.test_input
201+
error_message = "S3 bucket name did not match expected value"
202+
}
203+
}
204+
```
205+
206+
In addition to defining variables at the root level, the test file syntax also supports optional `variables` blocks within `run` blocks. Terraform passes all variable values from the test file into `run` blocks within the same file. You can override variable values for a particular `run` block by defining values within the `run` block's `variables` block.
207+
208+
This allows you to define variable values directly in your test files, instead of requiring external inputs. Modifying the example using external variables above:
209+
183210
```hcl
184211
# variable_precedence.tftest.hcl
185212
@@ -214,7 +241,7 @@ run "overrides_root_level_value" {
214241
}
215242
```
216243

217-
We've added a second `run` block that specifies the `bucket_prefix` variable value as `other`, overriding the value `test` that is provided by the test file and used during the first `run` block.
244+
Instead of needing external input to provide a value for our tests, you can define our variables directly within the test file. This example also adds a second `run` block that specifies the `bucket_prefix` variable value as `other`, overriding the value `test` that is defined in the test file and used during the first `run` block.
218245

219246
### Specify variables with the Command Line or definition files
220247

0 commit comments

Comments
 (0)