All guides

Terraform tests: the built-in test framework

Terraform's test framework is available in v1.6.0 and later. Tests are HCL files made of run blocks and assertions. The terraform test command executes them against real infrastructure or against mocked providers.

An open laptop on a wooden table in front of bookshelves

What the framework is

terraform test is the command that loads Terraform testing files and runs them. HashiCorp's documentation presents the command and the test file syntax as tools for module authors, who use them to validate and test shared modules. It adds that both work on root modules too. The framework is available in Terraform v1.6.0 and later. Test files end in .tftest.hcl or .tftest.json.

A test file holds one to many run blocks, zero to one variables block, zero to many provider blocks and zero to one test block. By default, Terraform executes the run blocks sequentially.

A first test

This is the first example in the documentation, with its provider block left out. The module under test:

HCL

variable "bucket_prefix" {
  type = string
}

resource "aws_s3_bucket" "bucket" {
  bucket = "${var.bucket_prefix}-bucket"
}

output "bucket_name" {
  value = aws_s3_bucket.bucket.bucket
}

The test file:

HCL

variables {
  bucket_prefix = "test"
}

run "valid_string_concat" {
  command = plan

  assert {
    condition     = aws_s3_bucket.bucket.bucket == "test-bucket"
    error_message = "S3 bucket name did not match expected"
  }
}

HashiCorp's tutorial runs terraform init and then terraform test. It says the condition of each assert block must evaluate to true. If it does not, the test fails and displays the error_message.

Plan runs and apply runs

By default, each run block executes with command = apply. Terraform creates real infrastructure and runs the assertions against it, which the documentation calls analogous to integration testing.

command = plan instructs Terraform not to create new infrastructure. The documentation says this lets test authors validate logical operations and custom conditions, in a process analogous to unit testing.

When a test file ends, Terraform tries to destroy each resource the file created, going through the run blocks in reverse order. If it cannot, it reports a list of the resources it created but could not destroy. The command reference adds a warning that terraform test can create real infrastructure that can cost you money.

Variables and earlier runs

Terraform passes the values from the file's variables block into every run block, and a variables block inside a run overrides them for that run. Variables defined in test files take the highest precedence. They override environment variables, variable files and command-line input.

Three more features connect runs to each other:

  • A run block can refer to the outputs of an earlier one, in the form run.<name>.<output>.
  • A module block inside a run loads a different module. It supports only the source and version attributes, and only local and registry modules. The documentation gives two uses. One is a setup module: it builds the infrastructure that the main configuration needs for its tests. The other is a loading module that loads and validates secondary infrastructure, such as data sources.
  • expect_failures lists checkable objects that should fail their custom conditions: resources, data sources, check blocks, input variables and outputs. The test passes if they report an issue and fails if they do not.

Mocks and overrides

Test mocking is available in Terraform v1.7.0 and later. The documentation says mocking lets you test with no infrastructure created and no credentials required.

HCL

mock_provider "aws" {
  mock_resource "aws_s3_bucket" {
    defaults = {
      arn = "arn:aws:s3:::test-bucket"
    }
  }
}

run "sets_correct_name" {
  variables {
    bucket_prefix = "test"
  }

  assert {
    condition     = aws_s3_bucket.bucket.bucket == "test-bucket"
    error_message = "incorrect bucket name"
  }
}

The ARN and the prefix are the example's choice. This test passed on Terraform v1.9.8 against the module above, with no AWS credentials.

A mocked provider generates values for computed attributes. Numbers are 0, booleans are false, strings are a random 8-character alphanumeric string, and collections are empty. mock_resource and mock_data set defaults in place of those values. Three further blocks override single objects: override_resource, override_data and override_module.

Files and CI

By default, the directory that contains test files is named tests. Terraform looks for test files there and in the root directory. -test-directory overrides the directory, and test files in the root configuration directory are loaded regardless of its value. -filter limits the run to the test files you name.

Shell

terraform init -input=false
terraform test -junit-xml=test-results.xml

-junit-xml writes the test report as JUnit XML into a file you name, which is the example's choice here. The v1.11 changelog announces the option's general availability, and Terraform v1.9.8 rejects it as experimental. The command reference says it is incompatible with -cloud-run.

Run the tests that mock every provider on each pull request. They need no credentials. Run the tests that apply where their real infrastructure can be created and destroyed.

The same tests in OpenTofu

OpenTofu has the command as tofu test. Its documentation says the command executes all *.tftest.hcl, *.tofutest.hcl, *.tftest.json and *.tofutest.json files. When a .tftest.hcl file and a .tofutest.hcl file with the same base name are in one directory, OpenTofu uses the .tofutest.hcl file and ignores the other. The page gives the reason: module authors who want to support both OpenTofu and Terraform can create different tests for each.

For a test file, the page lists run blocks, a variables block, provider blocks, mock_provider blocks and the override_resource, override_data and override_module blocks.

Where to start

  • Write a command = plan run with one assert for each value the module computes from its inputs.
  • Add expect_failures runs for your variable validation rules.
  • Add a mock_provider where a run should finish with no credentials.
  • Keep the files in tests/, and run terraform init and terraform test on every pull request.

Sources

Want a second pair of eyes on yours? We do this work for a living.

Book a call

More guides