Command: test
The terraform test
command reads in Terraform testing files and executes the tests within.
The test
command, and the test file syntax, are particularly helpful for module authors who want to validate and test their shared modules. You can also use the test
command to validate root modules.
Usage
Usage: terraform test [options]
This command searches the current directory and the specified testing directory (tests
, by default) for any Terraform testing files, and executes the specified tests. Refer to Tests for more details on test files.
Terraform then executes a series of Terraform plan or apply commands according to the test files' specifications, and also validates the relevant plan and state files according to the test files' specifications.
Warning: The Terraform test command can create real infrastructure than can cost you money. Refer to the Terraform Test Cleanup section for best practices on ensuring created infrastructure is destroyed.
General Options
The following options apply to the Terraform test
command:
-filter=testfile
- Limits theterraform test
operation to the specified test files.-json
- Displays machine-readable JSON output for your testing results.-test-directory=<relative directory>
- Overrides the directory that Terraform looks into for test files. Note that Terraform always loads testing files within the main configuration directory. The default testing directory istests
.-verbose
- Prints out the plan or state for eachrun
block within a test file, based on thecommand
attribute of eachrun
block.
State Management
Each Terraform test file will maintain all Terraform state it requires within memory as it executes, starting empty. This state is entirely separate from any existing state for the configuration under test, so you can safely execute Terraform test commands without affecting any live infrastructure.
Terraform Test Cleanup
The Terraform test
command creates real infrastructure. Once Terraform fully executes each test file, Terraform attempts to destroy any remaining infrastructure. If it cannot do this, Terraform reports a list of resources it created but could not destroy.
You should monitor the output of the test command closely to ensure Terraform removes the infrastructure it created or perform manual cleanup if not. We recommend creating dedicated testing accounts within the target providers that you can routinely and safely purge to ensure any accidental and costly resources aren't left behind.
Terraform also provides diagnostics explaining why it could now automatically clean up. You should resolve these diagnostics to ensure that future clean-up operations are successful.