Terraform 测试
- 作者仓库星标 0
- 作者仓库 skills-registry
Terraform Test Skill
Write, organize, and run Terraform's built-in test framework for Azure infrastructure modules.
Reference: Terraform Testing Documentation
Quick Reference
| Concept | Description | Min Version |
|---|---|---|
| Test file | .tftest.hcl in tests/ directory |
1.6 |
| Run block | Single test scenario with assertions | 1.6 |
| Assert block | Condition that must be true for test to pass | 1.6 |
| Plan mode | command = plan — validates logic, no resources created |
1.6 |
| Apply mode | command = apply (default) — creates real infrastructure |
1.6 |
| Mock provider | Simulates provider without real API calls | 1.7 |
| Parallel execution | parallel = true on independent run blocks |
1.9 |
| Expect failures | Verify validation rules reject invalid input | 1.6 |
File Structure
my-module/
├── main.tf
├── variables.tf
├── outputs.tf
└── tests/
├── defaults_unit_test.tftest.hcl # Plan mode (fast)
├── validation_unit_test.tftest.hcl # Plan mode (fast)
└── full_stack_integration_test.tftest.hcl # Apply mode (creates resources)
Naming convention: *_unit_test.tftest.hcl (plan mode), *_integration_test.tftest.hcl (apply mode).
Test File Components
- 0–1
testblock (test-wide settings) - 1+
runblocks (test scenarios, sequential by default) - 0–1
variablesblock (file-level inputs, highest precedence) - 0+
providerblocks (provider configuration) - 0+
mock_providerblocks (simulated providers, TF 1.7+)
Canonical Example
See references/test-examples.md for a complete Azure Resource Group test
(unit tests, tag validation, expect_failures).
Key Syntax Rules
Run Block Attributes
| Attribute | Type | Default | Description |
|---|---|---|---|
command |
plan/apply |
apply |
Test mode |
variables |
block | — | Override file-level variables |
module |
block | — | Test alternate module (local/registry only) |
assert |
block (1+) | — | Validation conditions |
expect_failures |
list | — | Expected validation failures |
Assert Syntax
assert {
condition = <boolean expression>
error_message = "Human-readable failure description"
}
Assertions can reference: resource attributes, outputs, run.<name>.<output>, var.*, data sources.
Variables precedence: run-block > file-level > all other sources.
See references/test-examples.md for module blocks, plan options, and prior run references.
Mock Providers (TF 1.7+)
Simulate Azure provider without API calls — ideal for unit tests.
Use mock_provider "azurerm" with mock_resource and mock_data blocks.
When to use: Unit tests, CI without Azure credentials, fast local development.
When NOT to use: Integration tests, validating actual Azure API behavior.
See references/mock-providers.md for full mock patterns and examples.
Common Test Patterns
See references/test-examples.md for: conditional resources, tag validation,
resource count with for_each, variables precedence, and prior run references.
Steps
terraform test # All tests
terraform test tests/defaults.tftest.hcl # Specific file
terraform test -verbose # Detailed output
terraform test -filter=test_resource_group # Filter by name
terraform test -no-cleanup # Debug: keep resources
Rules
- Naming:
*_unit_test.tftest.hcl/*_integration_test.tftest.hcl - Plan mode first: Use
command = planfor fast, cost-free validation - Clear error messages: Describe what went wrong and expected state
- Test isolation: Independent run blocks where possible
- Variable coverage: Test multiple combinations for all code paths
- Mock for speed: Use mock providers in CI without Azure access
- Negative testing: Use
expect_failuresfor validation rule coverage - Sequential only when needed: Only chain run blocks via
run.<name>when required
Terraform MCP Integration
Use mcp_terraform_search_providers to validate that resource types used in
test assertions exist in the target provider version.
Reference Index
| File | Contents |
|---|---|
references/test-examples.md |
Canonical example, common patterns, variables, module blocks |
references/test-patterns.md |
Unit vs integration patterns, CI/CD examples, complex assertions |
references/mock-providers.md |
Mock provider setup, mock resources/data sources, override files |
references/test-execution.md |
CLI commands, parallel execution, verbose/debug, diagnostics |
<!-- tomevault:4.0:skill_md:2026-05-22 -->Source: aivandelindt/azure-agentic-infraops — distributed by TomeVault.
- 流狐分类
- 运维部署
- 作者声明 Agent
- 未找到明确声明;不据此推断已兼容或已测试
- 静态检查
- 88 / 100 · 启发式扫描,不代表运行安全
- 作者 / 版本 / 许可
- @tomevault-io · 未声明 license
- 流狐 Token 估算
- 低消耗
- 流狐接入估算
- 需简单配置
- 是否需要外部 API Key
- 未发现要求
- 检测到的系统要求
- 未声明
- 底层运行要求
- 未声明
- 检测到的文件与系统行为
-
- 只读
- 允许写入 / 修改
- Shell 执行
- 检测到的网络行为
- 仅限本地
- 安装命令数
- 无(仅作为资料)
档案由构建时根据 SKILL.md 与安装命令自动衍生,可能与作者实际意图存在差异。
需要注意: 未限定 allowed-tools,默认拥有全部工具权限。
# Canonical Example
See `references/test-examples.md` for a complete Azure Resource Group test (unit tests, tag validation, expect_failures). Concept · Description · Min Version Test file · .tftest.hcl in tests/ directory · 1.6 Run block · Single test scenario with assertions · 1.6
Naming convention: unittest.tftest.hcl (plan mode), integrationtest.tftest.hcl (apply mode).
0–1 test block (test-wide settings) 1+ run blocks (test scenarios, sequential by default) 0–1 variables block (file-level inputs, highest precedence)
See references/test-examples.md for a complete Azure Resource Group test (unit tests, tag validation, expectfailures).
Key Syntax Rules
Attribute · Type · Default · Description command · plan/apply · apply · Test mode variables · block · — · Override file-level variables
# Terraform Test Skill
Write, organize, and run Terraform's built-in test framework for Azure infrastructure modules.
**Reference:** [Terraform Testing Documentation](https://developer.hashicorp.com/terraform/language/tests)
---
## Quick Reference
| Concept | Description | Min Version |
| ------------------ | --------------------------------------------------------- | ----------- |
| Test file | `.tftest.hcl` in `tests/` directory | 1.6 |
| Run block | Single test scenario with assertions | 1.6 |
| Assert block | Condition that must be true for test to pass | 1.6 |
| Plan mode | `command = plan` — validates logic, no resources created | 1.6 |
| Apply mode | `command = apply` (default) — creates real infrastructure | 1.6 |
| Mock provider | Simulates provider without real API calls | 1.7 |
| Parallel execution | `parallel = true` on independent run blocks | 1.9 |
| Expect failures | Verify validation rules reject invalid input | 1.6 |
## File Structure
```text
my-module/
├── main.tf
├── variables.tf
├── outputs.tf
└── tests/
├── defaults_unit_test.tftest.hcl # Plan mode (fast)
├── validation_unit_test.tftest.hcl # Plan mode (fast)
└── full_stack_integration_test.tftest.hcl # Apply mode (creates resources)
```
**Naming convention**: `*_unit_test.tftest.hcl` (plan mode), `*_integration_test.tftest.hcl` (apply mode).
## Test File Components
- **0–1** `test` block (test-wide settings)
- **1+** `run` blocks (test scenarios, sequential by default)
… 作者原文负责流程事实;流狐只索引当前章节、要点、文件与命令。
章节 -> Quick Reference → File Structure → Test File Components → Canonical Example → Key Syntax Rules → Run Block Attributes
要点 -> Reference · Naming convention · 0–1 · 1+ · 0+ · When to use · When NOT to use · Naming
文件/命令 -> .tftest.hcl · tests/ · command = plan · command = apply · parallel = true · unittest.tftest.hcl · integrationtest.tftest.hcl · test
内容 SHA-256 -> b9fab064687c
方法与流程
适用与边界
原文中的明确线索
.tftest.hcl、tests/、command = plan、command = apply、parallel = true、unittest.tftest.hcl、integrationtest.tftest.hcl、test