agentleFS
jonathan-vellajonathan-vella/bmit-2026/.github/skills/terraform-test/SKILL.md

terraform-test

Skill3 starsChanged 8 months agoMITArchived repositoryView on GitHub

Write and run Terraform tests (.tftest.hcl). USE FOR: test files, run blocks, assertions, mock providers, plan-mode unit tests, apply-mode integration tests, test troubleshooting. WHEN: create test, write test, terraform test, .tftest.hcl, mock provider, test module, validate infrastructure, test assertion. DO NOT USE FOR: Bicep code, architecture decisions, deployment.

5 other projects use a version of this.

Adopted by

Most recently taken up by

The file

We split this skill into the rules it gives an agent

8 rules. 0 are common, 3 are distinctive, and 5 are very specific to this project.

  • 0

    Common

    Most of the 5 edited versions we read keep these.

  • 3

    Distinctive

    This author's own choices. Worth a look.

  • 5

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Write, organize, and run Terraform's built-in test framework for Azure infrastructure modules.

    line 9
  2. Very specific:

    Use mock_provider "azurerm" with mock_resource and mock_data blocks.

    line 84
  3. Very specific:

    Plan mode first: Use command = plan for fast, cost-free validation

    line 107
  4. Distinctive:

    Variable coverage: Test multiple combinations for all code paths

    line 110
  5. Distinctive:

    Mock for speed: Use mock providers in CI without Azure access

    line 111
  6. Very specific:

    Negative testing: Use expect_failures for validation rule coverage

    line 112
  7. Very specific:

    Sequential only when needed: Only chain run blocks via run.<name> when required

    line 113
  8. Very specific:

    Use mcp_terraform_search_providers to validate that resource types used in test assertions exist in the target provider version.

    line 117

“Common” is measured: kept by at least 6 in 10 of the 5 edited versions of this file we could read. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Terraform Test Skill

Write, organize, and run Terraform's built-in test framework for Azure infrastructure modules.

Reference: Terraform Testing Documentation


Quick Reference

ConceptDescriptionMin Version
Test file.tftest.hcl in tests/ directory1.6
Run blockSingle test scenario with assertions1.6
Assert blockCondition that must be true for test to pass1.6
Plan modecommand = plan — validates logic, no resources created1.6
Apply modecommand = apply (default) — creates real infrastructure1.6
Mock providerSimulates provider without real API calls1.7
Parallel executionparallel = true on independent run blocks1.9
Expect failuresVerify validation rules reject invalid input1.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 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

AttributeTypeDefaultDescription
commandplan/applyapplyTest mode
variablesblock—Override file-level variables
moduleblock—Test alternate module (local/registry only)
assertblock (1+)—Validation conditions
expect_failureslist—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.

Running Tests

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

Best Practices

  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

FileContents
references/test-examples.mdCanonical example, common patterns, variables, module blocks
references/test-patterns.mdUnit vs integration patterns, CI/CD examples, complex assertions
references/mock-providers.mdMock provider setup, mock resources/data sources, override files
references/test-execution.mdCLI commands, parallel execution, verbose/debug, diagnostics

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.