Terraform test applies and tears down resources for every test file in the tests directory

I have multiple (2 as of now) tftest.hcl files in our project repository (functionally, one for each list of services broken down in .tf files). Running terraform test in the source directory gets *.tftest.hcl files in the tests/ directory and runs them one after another. But, terraform applies and tears down the resources after running through each file in the tests directory.

Is there a way to control the test framework to create resources at the start and tear down after going through all the test files at the end?

I am using Terraform v1.7.3.
Below is what I see in my shell -

terraform test -var aws-profile=dev                                               
tests\test_resource_support.tftest.hcl... in progress
  run "test_bucket_names"... pass
  run "test_bucket_notification"... pass
  run "test_backup_vault"... pass
  run "test_backup_vault_lock"... pass
  run "test_backup_plan"... pass
  run "test_backup_selection"... pass
  run "test_cloudwatch_event_rule"... pass
  run "test_cloudwatch_event_rule_target"... pass
  run "test_sns_topic"... pass
  run "test_sns_topic_subscription"... pass
  run "test_sns_topic_policy"... pass
tests\test_resource_support.tftest.hcl... **tearing down**
tests\test_resource_support.tftest.hcl... pass
**tests\test_resource_symmetric-pipeline.tftest.hcl... in progress**
  run "test_bucket_objects"... fail
....
....
....

Hi @think_tank_77, unfortunately it currently isn’t possible to run multiple test files without tearing down created infrastructure between them. Each test file runs as a dedicated test suite in isolation. Note, that there is no limit on the number of run blocks allowed in a test file so you could merge the test files together to avoid the teardown step.

We have an open feature request for this kind of behaviour here: Terraform Test: add ability to skip teardown · Issue #34073 · hashicorp/terraform · GitHub.

This is in active discovery, and following along with that ticket is the best way for any future updates on this functionality.

Thanks!

1 Like

Thank you. I believe, having the ability to control teardown is good feature request.

This topic was automatically closed 62 days after the last reply. New replies are no longer allowed.