OpenTofu Native Test Framework Module Validation Prompt
Build a real test suite for a reusable OpenTofu module using the native `tofu test` framework — combining fast plan-only assertions, targeted apply-and-verify runs, mocked providers, and variable-validation coverage into a CI gate that catches regressions before they reach state.
- Target user
- Module authors and platform teams maintaining shared OpenTofu modules
- Difficulty
- Advanced
- Tools
- Claude, ChatGPT, Cursor
The prompt
You are a senior IaC engineer who ships versioned, reusable OpenTofu modules that many teams depend on. You use the native `tofu test` framework (`.tftest.hcl` files) to prove a module behaves before tagging a release, mixing fast plan-only checks with a small number of real apply-and-destroy integration runs. I will provide: - The module source (variables, resources, outputs) or a description of it - The provider(s) it uses and whether real cloud credentials are available in CI - The invariants that matter: naming/tagging rules, conditional resource creation, output correctness, and variable validation - The CI system and how long the suite is allowed to run Your job: 1. **Test tiering** — split coverage into: (a) `command = plan` unit tests that assert on planned values with zero cloud calls (fast, run on every PR); (b) a minimal set of `command = apply` integration tests that create, assert against real attributes, and auto-destroy; (c) provider-mocked tests using `mock_provider` for logic that must run without credentials. Justify what belongs in each tier. 2. **Concrete `.tftest.hcl` files** — write the actual test files: `run` blocks with `variables`, `assert` blocks with real `condition`/`error_message`, `expect_failures` for negative tests of `variable` validation, and `mock_provider`/`override_resource` where credentials are unavailable. Cover the happy path, each conditional/`count`/`for_each` branch, and at least one negative case. 3. **Assertion design** — show how to assert on outputs, resource attributes, and computed values, and how to keep assertions meaningful (not just "resource exists") — e.g. verifying a computed name matches a tagging convention or an output ARN has the expected shape. 4. **Fixtures & setup modules** — how to use a `run` block with a helper/setup module to stand up prerequisites (a VPC, a KMS key) that the module under test depends on, and tear them down cleanly. 5. **CI wiring** — the pipeline stages: `tofu init` for the test, plan-only tier on every PR, apply-tier gated to run against a sandbox account on merge, and how to keep the apply tier from leaking real resources when it fails mid-run. 6. **Failure hygiene** — how `tofu test` cleans up (it destroys what a `run` created even on assertion failure), the edge cases where it can leak, and how to detect/sweep orphaned test resources. Output as: (a) the tiered test plan, (b) complete ready-to-run `.tftest.hcl` files, (c) the CI stage definitions, (d) a note on cleanup guarantees and how to sweep leaks. Assume the module is consumed by production stacks, so a regression here is a production regression.
Run this prompt with AI
Test it, get an AI-improved version, or compare models — live in the Prompt Workspace. No copy-paste.
Related prompts
-
OpenTofu Provider-Defined Functions & Early Variable Evaluation Prompt
Adopt OpenTofu's early variable evaluation and provider-defined functions to make module sources, backends, and provider configuration dynamic and DRY where Terraform-style static requirements once blocked you.
-
Helm Chart Testing Strategy Prompt
Build a layered test suite for Helm charts — lint, schema-validated values, golden-file template tests, and helm test hooks against a kind cluster — so a values change can't silently break rendered manifests.
-
IaC Module Documentation Generator Prompt
Generate clear, consumable documentation for an infrastructure-as-code module, role, or chart — inputs, outputs, examples, requirements, and an upgrade note — so consumers adopt the paved road without reading the source, across any IaC tool.
-
Conftest Policy Unit Testing Prompt
Write unit tests for your Rego policies so Conftest rules are themselves covered by passing and failing fixtures before they gate real deployments.
More Infrastructure as Code prompts & error guides
Browse every Infrastructure as Code prompt and troubleshooting guide in one place.
Reading prompts? Get all 500 in one free PDF
500 battle-tested, copy-paste AI prompts engineered by a senior systems engineer — every one with fill-in placeholders and safety/back-out notes. Drop your email and it's yours.
- 500 prompts: Linux · Kubernetes · Terraform · OpenStack · GitLab · Docker · Monitoring · Incident Response
- Instant PDF download — yours free, forever
- Plus one practical AI-workflow email a week (no spam)
Single opt-in · unsubscribe anytime · no spam.