tftest is a Python package that simplifies testing Terraform modules from Python unit tests. It wraps the Terraform executable, providing convenience methods to set up fixtures, execute Terraform commands (like `init`, `plan`, `apply`, `output`, `destroy`), and parse their outputs. The library is actively maintained, with frequent releases addressing bug fixes, new features, and compatibility updates, often leveraging `pytest` for testing frameworks.
pip install tftestVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to use `tftest.TerraformTest` with `pytest` to initialize a simple Terraform module, run `terraform output`, and assert the expected value. Ensure the `terraform` CLI is installed and available in your system's PATH for this example to run successfully.
Migrate Terragrunt-specific logic to native Terraform configurations or use a different testing approach for Terragrunt. For testing Terragrunt, consider using Terratest (Go-based) or a direct Terragrunt wrapper.
Ensure your Terraform modules target Terraform 0.12+ and update your `tftest` code to reflect any necessary API changes (e.g., how outputs are parsed).
Install Terraform (e.g., via `brew install terraform` on macOS, or follow official HashiCorp instructions) and verify it's in your PATH by running `terraform --version` in your terminal.
Handle empty plan scenarios explicitly in your tests, possibly by checking the plan's content before attempting to parse specific outputs, or by ensuring the test case always expects some changes if `output=True` is used.
Align your Python environment with the `pytest` version you're using. If `tftest` is installed in a project, update your `pytest` dependency and Python version accordingly to avoid compatibility issues.
Run `pip install tftest` to install the package.
Install the Terraform CLI and ensure its executable path is added to your system's PATH environment variable.
Modify your test logic to anticipate and handle empty plans, for example, by checking if the plan indicates 'no changes' before attempting to parse resource-specific outputs, or by ensuring your test infrastructure always has a change to report.