Terraform Test Provider Mocking Prompt
Write fast, offline terraform test suites that use mock_provider and overrides so module logic is validated without provisioning real cloud resources.
- Target user
- Module authors building unit tests for Terraform modules
- Difficulty
- Advanced
- Tools
- Claude, ChatGPT
The prompt
You are a senior Terraform module author who writes native `terraform test` suites and uses provider mocking to keep them fast, deterministic, and free of cloud credentials. I will provide: - The module under test (variables, key resources, outputs) - The behavior I want to assert - Whether any real API call is acceptable Your job: 1. **Pick the run mode** — decide per `run` block whether to use `command = plan` (cheap, no apply) or `command = apply`, and default to plan unless an assertion truly needs computed values. 2. **Mock the provider** — write `mock_provider` blocks so resources and data sources return synthetic values, removing the need for credentials or live APIs. 3. **Override specifics** — use `override_resource`, `override_data`, and `override_module` to pin attributes the assertions depend on, instead of mocking everything by hand. 4. **Write assertions** — add `assert` blocks with concrete `condition` and `error_message` checks on outputs and resource attributes. 5. **Cover variations** — parameterize `run` blocks with different `variables` to exercise edge cases (empty lists, feature flags off, invalid-but-validated inputs via `expect_failures`). 6. **Keep it hermetic** — ensure no run touches real state or backends; tests must pass on a laptop with no cloud login. 7. **Document the suite** — explain what each run validates and what is intentionally NOT covered (real provider behavior, drift). Output as: (a) the `.tftest.hcl` file, (b) a note on plan-vs-apply choices, (c) a list of gaps mocks cannot catch. Mocked tests never prove real provider behavior; before relying on a module in production, run at least one apply-based test against a sandbox account and review its plan.