How to fix Error: Test assertion failed in Terraform

TerraformINTERMEDIATEHIGHUpdated Dec 31, 2025ยท 117 views

Terraform test assertions fail when conditions evaluate to false during test execution. This prevents test files from passing and indicates mismatched expected vs actual values in your test configuration.

Quick fix

Fixes Error: Test assertion failed

terraform plan -json | grep -i name
  1. Read the assertion error message carefully
  2. Verify the expected value is correct
  3. Check for type mismatches in comparisons
  4. Ensure assertions only check computed values
terraform โ€” error
terraform plan -json | grep -i name
Error: Test assertion failed

What this error means

A test assertion failure occurs when the condition expression in a Terraform test run block evaluates to false. Terraform's testing framework (available in v1.6+) validates your infrastructure configuration by creating temporary resources and checking them against defined assertions. When any assertion condition returns false, the test fails and displays the associated error message. This is the primary way Terraform reports test failures.

Typical symptoms

  • terraform test command exits with failure status
  • Error output shows specific line number and assertion condition that failed
  • Test execution halts after first failed assertion block
  • Custom error message from assertion is displayed in output
  • CI/CD pipeline fails when running terraform test

Common causes

  1. Condition expression evaluates to false because actual value does not match expected value
  2. Incorrect variable or output value passed to assertion compared to expected result
  3. Type mismatch between expected and actual values (e.g., comparing string to boolean)
  4. Assertion run block executes before dependent resources are fully created
  5. Expression syntax error within condition or error_message arguments
  6. Timing issues when mixing expect_failures with assert blocks that depend on the same values

How to fix "Error: Test assertion failed"

1Read the assertion error message carefully

When a test fails, Terraform displays the line number, the condition that failed, and the custom error_message. Write down the exact value that was received and what was expected. This will help you identify the root cause quickly.

2Verify the expected value is correct

Double-check your test assumptions. For example, if asserting resource.name == "expected-name", verify that your configuration actually produces "expected-name". Test your Terraform plan separately to see actual values:

bash
terraform plan -json | grep -i name
3Check for type mismatches in comparisons

Ensure you are comparing compatible types. A common error is comparing a boolean to a string: local.enabled == "true" will fail because the actual value is boolean true, not string "true". Fix this by removing quotes or using tostring():

hcl
# Wrong
assert {
  condition = local.enabled == "true"
}

# Correct
assert {
  condition = local.enabled == true
}
4Ensure assertions only check computed values

If your assert block references a resource that fails a validation, the assertion won't be reached. Make sure all values you're asserting on are actually computed. If using expect_failures, separate those from assert blocks:

hcl
# Bad: mixing expect_failures with assert on same resource
run "test" {
  expect_failures = [ var.name ]
  assert {
    condition = aws_instance.example.id != null  # Never evaluated
  }
}

# Good: separate test runs
run "expect_invalid" {
  expect_failures = [ var.name ]
}

run "assert_valid" {
  assert {
    condition = aws_instance.example.id != null
  }
}
5Add meaningful error messages with string interpolation

Update your error_message to include the actual value received for better debugging:

hcl
assert {
  condition     = aws_instance.example.tags["Name"] == "prod-server"
  error_message = "Expected tag Name=prod-server but got ${aws_instance.example.tags["Name"]}"
}
6Run terraform validate and plan before running tests

Catch configuration syntax errors early before running tests. These tools should never fail in your pipeline:

bash
terraform fmt -check .
terraform validate
terraform plan -out=tfplan
terraform test

Advanced notes

For complex assertions with multiple conditions, use the alltrue() function to combine them cleanly. When dealing with sensitive values, note that error messages may not display the actual sensitive value for security reasons, making debugging harder. Consider using nonsensitive() in test code only. In Terraform versions before 1.12.0, assertions with complex variable references could crash; ensure you're on a recent version. The test framework creates and destroys real infrastructure per test, so use testing providers or local-only resources when possible to keep costs down.

Frequently asked questions

What does "Error: Test assertion failed" mean?
A test assertion failure occurs when the condition expression in a Terraform test run block evaluates to false. 6+) validates your infrastructure configuration by creating temporary resources and checking them against defined assertions. When any assertion condition returns false, the test fails and displays the associated error message.
How do I fix "Error: Test assertion failed"?
Terraform test assertions fail when conditions evaluate to false during test execution. This prevents test files from passing and indicates mismatched expected vs actual values in your test configuration.
What causes "Error: Test assertion failed"?
Condition expression evaluates to false because actual value does not match expected value Incorrect variable or output value passed to assertion compared to expected result Type mismatch between expected and actual values (e.g., comparing string to boolean)
Is the "Error: Test assertion failed" error serious?
Yes. This is a high-severity error that usually blocks work until it is fixed, but the steps above resolve it.

Official resources & further reading