.tftest.hcl file
A test file for terraform test, written in HCL, containing run blocks that exercise a module and assert blocks that check the results.
In more detail
Test files usually live in a tests directory beside the module. Each run block can set command = plan to check a plan without creating anything, or apply to build real resources and check their outputs.
Example
run "names_are_prefixed" {
command = plan
assert {
condition = output.summary != ""
error_message = "summary must not be empty"
}
}Related terms
- terraform test — The command, added in Terraform 1.6, that runs the scenarios in .tftest.hcl files against a module and reports which assertions passed.
- mock provider — A fake version of a provider, declared with mock_provider in a test file, that returns made-up values instead of calling a real API.
- HCL — HashiCorp Configuration Language: the block-based configuration syntax of Terraform files, which end in .tf.
Taught in
- Running Terraform In CI — Infrastructure as Code with Terraform
- Native Testing With Tftest Files — Infrastructure as Code with Terraform
- Provider Mocking For Tests — Infrastructure as Code with Terraform
- Policy As Code With OPA — Infrastructure as Code with Terraform
- Releasing The Real Cloud Artifact — Infrastructure as Code with Terraform
- Custom Conditions: Validation, Pre And Postconditions, Checks — HashiCorp Terraform Associate (004) Exam Prep