terraform-test

Write and run Terraform test files with run blocks, assertions, and mock providers.

1|Updated Aug 1, 2024
One-click install
npx skills add https://github.com/obispobruno/dotfiles --skill terraform-test-obispobruno
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: terraform-test
Source: https://github.com/obispobruno/dotfiles/tree/main/dot_agents/skills/terraform-test
Command: npx skills add https://github.com/obispobruno/dotfiles --skill terraform-test-obispobruno

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Validating Terraform modules before deployment is difficult without a structured testing approach, and misconfigured infrastructure can cause costly failures. This Skill provides comprehensive guidance for using Terraform's built-in testing framework to catch breaking changes early. ## Core Features & Use Cases - Test File Authoring: Create .tftest.hcl files with run blocks, assertions, variables, and provider configurations for unit and integration tests. - Mock Providers: Simulate provider behavior without creating real infrastructure or requiring cloud credentials (Terraform 1.7.0+). - Advanced Scenarios: Handle expect_failures for negative testing, state_key management, parallel execution, and module source testing. - Use Case: You are authoring a VPC module and want to verify that CIDR blocks, subnet counts, and outputs behave correctly. Use this Skill to write plan-mode unit tests and apply-mode integration tests that run in CI/CD. ## Quick Start Write a Terraform unit test in plan mode that validates my module creates exactly three subnets with the correct CIDR blocks.

Frequently Asked Questions about terraform-test

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I write a Terraform test file?▼

Create a .tftest.hcl file in a tests/ directory containing at least one run block with a command (plan or apply) and assert blocks with conditions and error messages. Run it with the terraform test command.

What is the difference between plan and apply in Terraform tests?▼

Plan mode validates configuration logic without creating real resources, making tests fast and free. Apply mode (the default) creates actual infrastructure for integration testing, with automatic cleanup in reverse run block order.

How do I mock a provider in Terraform tests?▼

Use a mock_provider block with mock_resource and mock_data blocks defining default attribute values, available since Terraform 1.7.0. Reference the mocked provider in your run block via the providers attribute to test without credentials or real API calls.

Can Terraform tests use modules from Git repositories?▼

No, Terraform test files only support local module paths and public or private registry modules. Git, HTTP, S3, and other remote sources are unsupported, so convert them to local modules or registry references.

How do I test that invalid variable values are rejected?▼

Use the expect_failures attribute in a run block listing the checkable objects (such as input variables) that should fail validation. The test passes if the specified objects report issues and fails if they do not.

Why do my Terraform tests interfere with each other?▼

Run blocks sharing the same state file can conflict. Use separate modules for automatic state isolation, set distinct state_key attributes, or use mock providers to keep tests independent.