terraform-test

Guide writing and executing Terraform tests with .tftest.hcl files.

16|3|Updated Oct 3, 2023
One-click install
npx skills add https://github.com/filippolmt/terraform-oci-free-tier --skill terraform-test-filippolmt
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: terraform-test
Source: https://github.com/filippolmt/terraform-oci-free-tier/tree/main/.agents/skills/terraform-test
Command: npx skills add https://github.com/filippolmt/terraform-oci-free-tier --skill terraform-test-filippolmt

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This Skill provides a comprehensive guide to writing and executing tests for Terraform configurations, ensuring infrastructure code quality and preventing regressions.

Core Features & Use Cases

  • Test File Structure: Understand the organization of .tftest.hcl files.
  • Run Blocks: Define test scenarios with apply or plan commands.
  • Assertions: Validate infrastructure state and outputs using assert blocks.
  • Mock Providers: Simulate provider behavior for isolated unit testing.
  • Use Case: You've updated a Terraform module and want to ensure it still deploys correctly and meets all requirements without manual verification. Use this Skill to learn how to write tests that automatically confirm these aspects.

Quick Start

Use the terraform-test skill to learn how to write a basic unit test for a Terraform module.

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 unit test for a Terraform module?

To write a Terraform test, you create a `.tftest.hcl` file and define run blocks that execute `apply` or `plan` commands. You then use assert blocks to validate the resulting infrastructure state and outputs against expected values.

What are mock providers in Terraform testing?

Mock providers in Terraform testing simulate provider behavior to enable isolated unit testing. They allow you to validate module logic and configuration without provisioning actual cloud resources or making external API calls.

How do I validate Terraform plan output without applying changes?

You can validate Terraform plan output by configuring a run block with the `plan` command in your `.tftest.hcl` file. This allows you to use assertions to check expected resource changes and outputs without deploying infrastructure.

When should I use built-in Terraform testing instead of external validation tools?

Use built-in Terraform testing when you need native integration with `.tftest.hcl` files, run blocks, and mock providers. It is ideal for validating infrastructure as code configurations and preventing regressions directly within the Terraform workflow.

Why is my Terraform test failing during the apply block?

Terraform test failures during an apply block often stem from failed assertions or unexpected resource state changes. You should troubleshoot by verifying your assert block conditions and checking the applied infrastructure state against your expected outputs.

Can I run multiple test scenarios in a single Terraform test file?

Yes, you can run multiple test scenarios in a single `.tftest.hcl` file by defining multiple run blocks. Each block can independently execute `apply` or `plan` modes and contain specific assertions for isolated validation.