npx skills add ...
npx skills add hashicorp/agent-skills --skill terraform-test
Comprehensive guide for writing and running Terraform tests. Use when creating test files (.tftest.hcl), writing test scenarios with run blocks, validating infrastructure behavior with assertions, mocking providers and data sources, testing module outputs and resource configurations, or troubleshooting Terraform test syntax and execution.
npx skills add hashicorp/agent-skills --skill terraform-test
Terraform's built-in testing framework validates that configuration updates don't introduce breaking changes. Tests run against temporary resources, protecting existing infrastructure and state files.
references/MOCK_PROVIDERS.md — Mock provider syntax, common defaults, when to use mocks (Terraform 1.7.0+ only — skip if the user's version is below 1.7)references/CI_CD.md — GitHub Actions and GitLab CI pipeline examplesreferences/EXAMPLES.md — Complete example test suite (unit, integration, and mock tests for a VPC module)Read the relevant reference file when the user asks about mocking, CI/CD integration, or wants a full example.
.tftest.hcl / .tftest.json): Contains run blocks that validate your configurationapply (default, creates real resources) or plan (validates logic only)Use *_unit_test.tftest.hcl for plan-mode tests and *_integration_test.tftest.hcl for apply-mode tests so they can be filtered separately in CI.
Resources are destroyed in reverse run block order after test completion. This matters for dependencies (e.g., S3 objects before bucket). Use terraform test -no-cleanup to skip cleanup for debugging.
*_unit_test.tftest.hcl for plan mode, *_integration_test.tftest.hcl for apply modecommand = plan unless you need to test real resource behaviorreferences/MOCK_PROVIDERS.md)expect_failures to verify validation rules reject bad inputsparallel = true for independent tests with different state files-no-cleanup for debuggingreferences/CI_CD.md)| Issue | Solution |
|---|---|
| Assertion failures | Use -verbose to see actual vs expected values |
| Missing credentials | Use mock providers for unit tests |
| Unsupported module source | Convert git/HTTP sources to local modules |
| Tests interfering | Use state_key or separate modules for isolation |
| Slow tests | Use command = plan and mocks; run integration tests separately |