{"id":9357,"library":"tftest","title":"tftest: Terraform Python Testing Helper","description":"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.","status":"active","version":"1.8.7","language":"en","source_language":"en","source_url":"https://github.com/GoogleCloudPlatform/terraform-python-testing-helper","tags":["terraform","testing","iaac","infrastructure-as-code","pytest","google-cloud-platform"],"install":[{"cmd":"pip install tftest","lang":"bash","label":"Install latest version"}],"dependencies":[{"reason":"Commonly used testing framework with tftest.","package":"pytest","optional":true}],"imports":[{"symbol":"TerraformTest","correct":"from tftest import TerraformTest"}],"quickstart":{"code":"import pytest\nimport tftest\nimport os\n\n# This fixture creates a temporary Terraform module for the test.\n# In a real project, 'tf_module' would point to your actual Terraform module directory.\n@pytest.fixture(scope=\"module\")\ndef tf_module(tmp_path_factory):\n    tf_dir = tmp_path_factory.mktemp(\"terraform_module_for_tftest\")\n    (tf_dir / \"main.tf\").write_text(\"\"\"\noutput \"greeting\" {\n  value = \"Hello from Terraform!\"\n}\n\"\"\")\n    \n    tf = tftest.TerraformTest(tf_dir)\n    tf.setup() # Runs 'terraform init'\n    return tf\n\ndef test_greeting_output(tf_module):\n    # Run 'terraform output' and assert its value\n    output = tf_module.output()\n    assert output[\"greeting\"][\"value\"] == \"Hello from Terraform!\"","lang":"python","description":"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."},"warnings":[{"fix":"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.","message":"Terragrunt support has been dropped in v1.8.7. Projects relying on `tftest` to interact with Terragrunt configurations will need to refactor their tests.","severity":"breaking","affected_versions":">=1.8.7"},{"fix":"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).","message":"tftest versions 1.0.0 and above require Terraform 0.12 or newer. Tests written for older Terraform versions with previous `tftest` versions might be incompatible due to changes in Terraform's output format and command-line interface.","severity":"breaking","affected_versions":">=1.0.0"},{"fix":"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.","message":"The `terraform` CLI binary must be installed and accessible in your system's PATH for `tftest` to function. `tftest` acts as a Python wrapper around the `terraform` executable.","severity":"gotcha","affected_versions":"All"},{"fix":"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.","message":"Calling `tf.plan(output=True)` when the Terraform plan is empty (i.e., no changes to infrastructure) can lead to an error because `tftest` expects a structured output that might not be present in an empty plan.","severity":"gotcha","affected_versions":"All"},{"fix":"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.","message":"As `tftest` is commonly used with `pytest`, be aware of `pytest`'s own Python version compatibility changes. Newer `pytest` versions have dropped support for older Python versions (e.g., `pytest` 8.0 dropped Python 3.7/3.8, and future versions will drop Python 3.9).","severity":"gotcha","affected_versions":"All (indirectly)"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Run `pip install tftest` to install the package.","cause":"The `tftest` library has not been installed in your Python environment or the environment where your script is being run.","error":"ModuleNotFoundError: No module named 'tftest'"},{"fix":"Install the Terraform CLI and ensure its executable path is added to your system's PATH environment variable.","cause":"The `terraform` executable is not found in the system's PATH. `tftest` relies on invoking the `terraform` CLI.","error":"FileNotFoundError: [Errno 2] No such file or directory: 'terraform'"},{"fix":"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.","cause":"This error occurs when `tftest` attempts to parse the output of a `terraform plan` command that results in no changes, and thus produces an unexpected or empty JSON structure.","error":"tftest failed when terraform plan is empty"}]}