Terraform 1.6 introduced a native testing framework with .tftest.hcl files, run blocks, assert conditions, and mock providers. Master vocabulary for test file discovery, plan vs apply commands, per-run variable overrides, assertion failure behavior, and unit testing with mock_provider.
0 / 5 completed
1 / 5
Terraform 1.6 introduced a native testing framework. Which file extension is used for Terraform test files?
Terraform test files use the .tftest.hcl extension. The Terraform CLI discovers these files in the module directory (and optionally in a tests/ subdirectory) when you run terraform test. Regular .tf files are not treated as tests.
2 / 5
A run block in a Terraform test file omits the command attribute. What command does it execute by default?
When command is omitted from a run block, Terraform defaults to apply. This creates real infrastructure. Use command = plan for plan-only assertions that don't create resources, which is faster and cheaper for unit-style tests.
3 / 5
An engineer writes an assert block with condition = aws_s3_bucket.main.bucket_prefix == "prod-". The assertion fails. What does Terraform do?
When an assertion fails, the current run block fails. Terraform continues executing subsequent run blocks (unless the failure is fatal), and crucially, performs cleanup (destroy) of all infrastructure created during the test to avoid resource leaks. The test suite reports the failure in the final summary.
4 / 5
The variables block inside a Terraform run block serves what purpose?
The variables block in a run block provides per-run variable overrides that take precedence over the module's defaults and any global test variables blocks. This allows a single test file to test the same module with different input combinations without multiple test files.
5 / 5
Which Terraform testing approach uses mock_provider to avoid creating real cloud resources?
Mock providers (introduced alongside the testing framework) allow unit tests to run without real cloud credentials or resource creation. A mock_provider block intercepts provider calls and returns configurable mock responses. This enables fast, offline tests of module logic, variable transformations, and conditional resource creation.