地形测试

运维部署 社区

技能解读

当前原文的自动摘译,尚未人工审校。

快速参考

概念 · 描述 · 最小版本 测试文件 · tests/ 目录中的 .tftest.hcl · 1.6 运行块 · 带有断言的单个测试场景 · 1.6 断言块 · 测试通过必须满足的条件 · 1.6 计划模式 · command = plan — 验证逻辑,不创建资源 · 1.6

文件结构

命名约定:*_unit_test.tftest.hcl(计划模式)、*_integration_test.tftest.hcl(应用模式)。

测试文件组件

0–1 test 块(测试范围设置) 1+ run 块(测试场景,默认为顺序) 0–1 variables 块(文件级输入,最高优先级) 0+ provider 块(提供程序配置) 0+ mock_provider 块(模拟提供程序,TF 1.7+)

典型例子

有关完整的 Azure 资源组测试(单元测试、标签验证、expect_failures),请参阅references/test-examples.md。

运行块属性

属性 · 类型 · 默认值 · 描述 command · plan/apply · apply · 测试模式 variables · 块 · — · 覆盖文件级变量 module · 块 · — · 测试备用模块(仅限本地/注册表) assert · 块 (1+) · —·验证条件

流狐档案 运行与权限由流狐静态检查推断,不代表作者声明或实际测试结果。
领域
运维部署
作者声明的智能体
未找到明确声明;不据此推断已兼容或已测试
静态检查
88 / 100 · 启发式扫描,不代表运行安全
运行环境证据
未执行;当前没有隔离运行测试证据
作者 / 版本 / 许可证
@tomevault-io · 未声明许可证
上下文用量评级
低消耗
接入复杂程度
需简单配置
是否需要外部接口密钥
不需要
兼容的系统
未声明(默认跨平台)
运行环境要求
未检出明确要求
检测到的文件与系统行为
  • 只读
  • 允许写入 / 修改
  • 命令执行
网络行为
仅限本地
安装命令数
无(仅作为资料)

需要注意: 未声明工具权限范围;实际权限由运行环境决定。

作者说明文件(原文) SKILL.md

# 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)
- **0–1** `variables` block (file-level inputs, highest precedence)
- **0+** `provider` blocks (provider configuration)
- **0+** `mock_provider` blocks (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

```hcl
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

```bash
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

1. **Naming**: `*_unit_test.tftest.hcl` / `*_integration_test.tftest.hcl`
2. **Plan mode first**: Use `command = plan` for fast, cost-free validation
3. **Clear error messages**: Describe what went wrong and expected state
4. **Test isolation**: Independent run blocks where possible
5. **Variable coverage**: Test multiple combinations for all code paths
6. **Mock for speed**: Use mock providers in CI without Azure access
7. **Negative testing**: Use `expect_failures` for validation rule coverage
8. **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     |

---
> Source: [aivandelindt/azure-agentic-infraops](https://github.com/aivandelindt/azure-agentic-infraops) — distributed by [TomeVault](https://tomevault.io).
<!-- tomevault:4.0:skill_md:2026-05-22 -->

讨论

基于 GitHub Discussions。登录 GitHub 即可参与讨论、点赞、订阅更新。