Terraform native test syntax
Test a contract rather than a command
A reusable module should keep its promised behavior when its implementation changes. For example, a small deployment module might promise that a replica count of two yields two named slots. A test states the input, the operation and an observable result. A test can catch a regression even when configuration validation succeeds. The native framework was introduced in Terraform 1.6; this course retains its provisional 1.14.8 pin. The following complete local configuration creates only synthetic terraform_data objects, not actual servers. Its expected names are slot-1 and slot-2 for input 2.
terraform { required_version = "= 1.14.8"}variable "replicas" { type = number validation { condition = var.replicas >= 1 && var.replicas <= 3 && floor(var.replicas) == var.replicas error_message = "Replicas must be a whole number from 1 to 3." }}resource "terraform_data" "slot" { count = var.replicas input = "slot-${count.index + 1}"}output "slot_names" { value = terraform_data.slot[*].input}Write a plan-only test
Put the following in tests/positive.tftest.hcl relative to main.tf. The file-level variables set a shared input; the second run changes it for that run. Each assertion contains a boolean condition and a diagnostic message. Plan is enough for these outputs because they depend on configured input, not a generated ID. The expected result is two passing runs. A plan with a real provider can still read remote information or execute other code; plan is not a security sandbox.
variables { replicas = 2}run "two_slots" { command = plan assert { condition = length(output.slot_names) == 2 && output.slot_names[0] == "slot-1" error_message = "Expected two slots starting with slot-1." }}run "one_slot" { command = plan variables { replicas = 1 } assert { condition = length(output.slot_names) == 1 error_message = "Expected exactly one slot." }}Discover test files and JSON syntax
Terraform discovers test files in the root and its test directory (normally tests). Native .tftest.hcl syntax is usually easier for people to edit; .tftest.json is useful for generated configuration. Here is a separate JSON test illustration; the expression uses Terraform interpolation syntax. Do not copy it over the HCL file or add it to the core lab without review.
{ "run": { "one_slot_json": { "command": "plan", "variables": { "replicas": 1 }, "assert": [ { "condition": "${length(output.slot_names) == 1}", "error_message": "Expected one slot." } ] } }}Use explicit names and meaningful messages. Test a boundary as well as a typical input. Do not assert incidental ordering unless it is a contract.
Apply tests and test state
An apply test is appropriate when the promised value only exists after execution. The next test creates three local records and then plans again against the same test state. The second assertion checks the number of names; it does not prove a no-change plan. Test runs using the same module normally share state within a file. Different test files have separate test state. A run can select a separate local module using module { source = "./setup" }; outputs from a previous run can be referenced with run.NAME.OUTPUT. The state_key setting can deliberately separate or share state; careless sharing can cause changes to earlier objects. Test-specific provider mappings can select alternate configurations, but they must be reviewed before any run.
run "create_three" { command = apply variables { replicas = 3 } assert { condition = terraform_data.slot[2].output == "slot-3" error_message = "The third slot did not store its expected value." }}run "reuse_state" { command = plan variables { replicas = 3 } assert { condition = length(output.slot_names) == 3 error_message = "Expected three slots in the next plan." }}Run and record evidence safely
Copy the fixture to an isolated directory; confirm the executable matches the provisional pin and no cloud credentials, provider downloads or remote backends are available. Review every file, including tests. Run the following manually only in that fixture. Default test operation is apply when not specified, so use explicit commands. Record exit status and diagnostics, tool version, file hash, run names and cleanup result. Terraform normally attempts to remove test-created objects after tests, but cleanup can fail; never treat a passing assertion as evidence that no resources remain. An interrupted or failed run may need an authorized recovery procedure. Do not run these commands against an existing application root.
# In a fresh copy of the supplied local fixture only.terraform versionterraform init -backend=falseterraform test -filter=tests/positive.tftest.hclterraform test -filter=tests/negative.tftest.hclterraform test -filter=tests/lifecycle.tftest.hclAll results in this package are expected, not observed Terraform results.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Terraform native test syntax — knowledge check
Review native tests and mocked behavior.
Which test is sufficient for a name derived entirely from input?
What happens if a run does not specify command?
Which value is appropriate to test after a local apply?
What does a passing test tell you about teardown?
What normally shares test state?