terraform-test

Write Terraform unit tests using .tftest.hcl run blocks and assertions.

Updated Jul 30, 2024
One-click install
npx skills add https://github.com/twidxuga/dotfiles --skill terraform-test-twidxuga
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: terraform-test
Source: https://github.com/twidxuga/dotfiles/tree/main/opencode/dot-config/opencode/skills/terraform-test
Command: npx skills add https://github.com/twidxuga/dotfiles --skill terraform-test-twidxuga

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Terraform testing helps ensure infrastructure changes don't introduce regressions by validating configurations before deployment.

Core Features & Use Cases

  • Test File: A .tftest.hcl or .tftest.json containing test configuration with run blocks.
  • Run Block and Assert Block: Define test scenarios and conditions to verify logic, outputs, and dependencies.
  • Mock Provider: Simulates provider behavior to enable fast, reproducible unit tests.
  • Plan vs Apply: Supports plan-mode unit tests and apply-mode integration tests.
  • Modularity: Supports testing local and registry modules with controlled variable inputs and outputs.

Quick Start

Create a test file with a plan-mode run to validate your Terraform module without creating real resources.

Frequently Asked Questions about terraform-test

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

FAQPage Schema
How do I write unit tests for Terraform modules?

Terraform testing with .tftest.hcl files prevents infrastructure regressions by validating configurations before deployment. You define test scenarios using run blocks and assert blocks to verify logic, outputs, and dependencies safely.

What is a mock provider in Terraform testing?

A mock provider simulates provider behavior to enable fast, reproducible Terraform unit tests. It allows you to validate configuration logic and run assertions without creating real cloud resources.

Can I test Terraform configurations without applying changes?

Yes, you can test Terraform configurations without applying changes using plan-mode run blocks. This validates module logic and assertions without creating real resources, offering a safe unit testing approach.

How do I use assertions in .tftest.hcl files?

You use assert blocks within run blocks in .tftest.hcl files to verify specific conditions. Assertions check Terraform outputs, configuration logic, and dependencies to ensure your module behaves as expected.

What is the difference between plan and apply modes in Terraform testing?

Plan mode runs unit tests to validate configuration logic without creating resources, while apply mode runs integration tests that create real resources. Both modes support variable blocks and provider configurations.

Do I need to put my Terraform test files in a specific directory?

Terraform test files should be located under a tests/ directory. These .tftest.hcl or .tftest.json files can include frontmatter with a name and description, alongside variable blocks and module references.