How to fix Error: Test run failed in Terraform
Terraform test execution failed due to assertion errors, validation failures, or runtime issues during module testing. Tests fail when custom conditions don't evaluate as expected or infrastructure creation encounters errors.
Quick fix
Fixes Error: Test run failed
terraform validate- Run terraform validate to check syntax errors
- Run terraform test with verbose output
- Review assertion conditions in test file
- Check for expect_failures configuration
terraform validateError: Test run failed
What this error means
The `terraform test` command executes validation tests defined in .tftest.hcl or .tftest.json files. When a test run fails, it means one or more assertions in the test blocks did not pass, or the Terraform plan/apply operation encountered errors. Each run block contains condition assertions that must evaluate to true for the test to pass. If any assertion fails or the underlying Terraform operation encounters an error, the entire test run fails and Terraform reports the failure.
Typical symptoms
- Test command exits with failure status
- Assertion messages display in output
- Test blocks do not complete as expected
- Resource creation or validation fails during test execution
- Plan or apply operations fail within test run blocks
Common causes
- Assertion condition evaluates to false with unmet expectations
- Resource dependencies not properly ordered in test configuration
- Variable or input values don't match test assertions
- Provider or provider configuration errors during test execution
- Resource creation fails due to quota limits, permissions, or conflict
- Validation rules or check blocks fail during plan phase
How to fix "Error: Test run failed"
First, validate your configuration syntax to ensure there are no basic HCL errors:
terraform validateIf validate fails, fix the syntax errors before running tests. This command checks for basic structural issues in your configuration files.
Re-run the test command with the -verbose flag to see detailed information about what each test run is doing:
terraform test -verboseThis prints the plan or state for each run block, helping you understand exactly which assertions are failing and why.
Examine the assert blocks in your .tftest.hcl file. Each assertion has a condition that must evaluate to true. For example:
assert {
condition = aws_s3_bucket.example.bucket != null
error_message = "S3 bucket should be created."
}Verify that your conditions match the actual resource outputs and values being created.
If your test intentionally expects failures, ensure the expect_failures attribute is correctly configured:
run "validate_required_field" {
command = plan
expect_failures = [
var.required_variable
]
}Note: Use expect_failures primarily with command = plan operations, not with apply.
Set the TF_LOG environment variable to DEBUG for detailed execution logs:
export TF_LOG=DEBUG
export TF_LOG_PATH=./terraform-debug.log
terraform testReview the generated log file to understand what's happening during each run block and identify where assertions are failing.
After a test fails, Terraform attempts to destroy created resources. Check the test output to see if cleanup was successful:
terraform testIf resources weren't cleaned up, manually destroy them to avoid accumulating test infrastructure:
terraform destroyEnsure your test is properly configured to clean up test infrastructure between runs.
Advanced notes
Terraform 1.6+ introduced the native test framework for HCL-based testing. When using expect_failures, be aware that combining it with multiple assert blocks can be confusing—prefer using either assert blocks OR expect_failures per run block for clarity. Tests that use command = apply with expect_failures will fail at the plan phase if a condition fails, which is expected behavior. For complex validation testing, consider using separate test runs for success and failure cases rather than mixing them in one run block. When running tests in CI/CD pipelines, use the -verbose flag for better audit trails and troubleshooting. Remote tests in HCP Terraform provide additional debugging context through the web UI.
Frequently asked questions
- What does "Error: Test run failed" mean?
- json files. When a test run fails, it means one or more assertions in the test blocks did not pass, or the Terraform plan/apply operation encountered errors. Each run block contains condition assertions that must evaluate to true for the test to pass.
- How do I fix "Error: Test run failed"?
- Terraform test execution failed due to assertion errors, validation failures, or runtime issues during module testing. Tests fail when custom conditions don't evaluate as expected or infrastructure creation encounters errors.
- What causes "Error: Test run failed"?
- Assertion condition evaluates to false with unmet expectations Resource dependencies not properly ordered in test configuration Variable or input values don't match test assertions
- Is the "Error: Test run failed" error serious?
- Yes. This is a high-severity error that usually blocks work until it is fixed, but the steps above resolve it.