Skip to main content

Python Test Helper for Terraform and OpenTofu

This simple helper facilitates testing Terraform and OpenTofu modules from Python unit tests by wrapping the terraform (or tofu) executable and exposing convenience methods to set up fixtures, execute commands, and parse their output.

It supports both lightweight tests that only run init and plan to ensure code is valid and creates the expected resources, and full end-to-end tests that run apply, output, and destroy against live environments or local state.

As an additional convenience, the module provides parsed wrappers around JSON plan output (via terraform plan -out and terraform show -json), module outputs (via terraform output -json), and pulled state (terraform state pull).

This module is inspired by Terratest for the lightweight approach to testing Terraform, and python-terraform for wrapping the CLI in Python.

Example Usage

The test folder contains examples of tests for both plan and apply, using synthetic JSON fixtures and minimal root modules. Extensive real-world usage can be found in the Cloud Foundation Fabric repository, for which this module was originally developed.

Using TerraformTest as a context manager ensures linked files and runtime artifacts (.terraform/, state files, and generated lockfiles) are cleaned up deterministically when the block exits:

import pytest
import tftest


@pytest.fixture
def plan(fixtures_dir):
  with tftest.TerraformTest('plan', fixtures_dir) as tf:
    tf.setup(extra_files=['plan.auto.tfvars'])
    yield tf.plan(output=True)


def test_variables(plan):
  assert 'prefix' in plan.variables
  assert plan.variables['names'] == ['one', 'two']


def test_outputs(plan):
  assert sorted(plan.outputs['gcs_buckets'].keys()) == plan.variables['names']


def test_root_resource(plan):
  res = plan.resources['google_project_iam_member.test_root_resource']
  assert res['values']['project'] == plan.variables['project_id']


def test_modules(plan):
  mod = plan.modules['module.gcs-buckets']
  res = mod.resources['google_storage_bucket.buckets[0]']
  assert res['values']['location'] == plan.variables['gcs_location']

To inspect or reuse a binary plan file directly, pass plan_out to plan() and optionally pass plan_file to apply():

def test_saved_plan(fixtures_dir, tmp_path):
  plan_file = tmp_path / 'out.tfplan'
  with tftest.TerraformTest('apply', fixtures_dir) as tf:
    tf.setup()
    tf.plan(plan_out=plan_file, tf_vars={'names': ['one']})
    tf.apply(plan_file=plan_file)
    assert tf.output()['triggers'][0]['name'] == 'one'
    tf.destroy()

To run against OpenTofu instead of Terraform, pass binary='tofu' when instantiating TerraformTest.

Compatibility

Version 2.0.0 requires Python 3.10 or newer, standardizes tf.tfdir as a pathlib.Path object, and removes the experimental method caching layer (enable_cache / use_cache). Terragrunt support was removed in 1.8.6. Check CHANGELOG.md for full release history.

Testing

Tests use pytest and have no runtime dependencies beyond a terraform or tofu binary (pytest --tf-binary=tofu). Development dependencies are listed in DEV-REQUIREMENTS.txt.

Disclaimer

This is not an officially supported Google product.

Metadata

Release files for tftest 2.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for tftest 2.0.0
File Size Uploaded Provenance Actions
tftest-2.0.0.tar.gz 20.4 kB With provenance Details for tftest-2.0.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for tftest 2.0.0
File Interpreter ABI Platform Actions
tftest-2.0.0-py3-none-any.whl Python 3 none any Details for tftest-2.0.0-py3-none-any.whl

Total release size: 34.3 kB

Release files / tftest-2.0.0.tar.gz

Download URL tftest-2.0.0.tar.gz
Size 20.4 kB
Tags Source
SHA-256 checksum
How to use checksums
bcc714539f5016c689d0692979551fdce3f1a46ba13d3362712ce3c964a13b1d
BLAKE2b-256 checksum
How to use checksums
45e20d5c58228b883fe322bda76a199b046e85656adf12be9fb9352013a376fa
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 20, 2026.

Transparency log

Release files / tftest-2.0.0-py3-none-any.whl

Download URL tftest-2.0.0-py3-none-any.whl
Size 14.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
82e7ec0c760b4f315dce2719be83b03f8c97c363c35d59f9eaf8ef7a0566b5e2
BLAKE2b-256 checksum
How to use checksums
47c85b446de278ad0e255d196b6262f3f2888d4781f8ef8efb1230067e344fdb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 20, 2026.

Transparency log

Security for tftest 2.0.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for tftest-2.0.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 20, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

2.0.0 This release

2 release files Full provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page