You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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]>
Copy file name to clipboardExpand all lines: content/terraform/v1.13.x (beta)/docs/language/tests/index.mdx
+30-3Lines changed: 30 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -26,6 +26,7 @@ Each test file contains the following root level attributes and blocks:
26
26
27
27
- Zero to one [`test`](#test-block) blocks.
28
28
- One to many [`run`](#run-blocks) blocks.
29
+
- Zero to many [`variable`](#variables) blocks.
29
30
- Zero to one [`variables`](#variables) block.
30
31
- Zero to many [`provider`](#providers) blocks.
31
32
@@ -174,12 +175,38 @@ Additionally, test assertions can directly reference outputs from current and pr
174
175
175
176
## Variables
176
177
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.
178
179
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.
180
181
181
182
Adding to the test file from the [example](#example) above:
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
+
183
210
```hcl
184
211
# variable_precedence.tftest.hcl
185
212
@@ -214,7 +241,7 @@ run "overrides_root_level_value" {
214
241
}
215
242
```
216
243
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.
218
245
219
246
### Specify variables with the Command Line or definition files
0 commit comments