# Terraform test conditional command attribute 'plan' or 'apply' for unit tests and integration tests

**URL:** <https://discuss.hashicorp.com/t/terraform-test-conditional-command-attribute-plan-or-apply-for-unit-tests-and-integration-tests/69346>\
**Category:** Terraform\
**Tags:** testing\
**Created:** [August 16, 2024, 9:05pm UTC](https://discuss.hashicorp.com/t/terraform-test-conditional-command-attribute-plan-or-apply-for-unit-tests-and-integration-tests/69346 "2024-08-16T21:05:34Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![think\_tank\_77](https://avatars.discourse-cdn.com/v4/letter/t/58f4c7/32.png) [@think\_tank\_77](https://discuss.hashicorp.com/u/think_tank_77)\
**Post date:** [August 16, 2024, 9:05pm UTC](https://discuss.hashicorp.com/t/terraform-test-conditional-command-attribute-plan-or-apply-for-unit-tests-and-integration-tests/69346/1 "2024-08-16T21:05:34Z")

</div>

Hi,  
I am using Terraform 1.9+ and writing hcl tests for unit testing and integration testing. Want to write ‘run’ blocks with asserts to support tests using command = plan for unit tests and command = apply for integration tests.  
Example:

```auto
variables {
  datalake_bucket_name = "test-bkt-202408"
  plan = true
}
run "test_bucket_names" {
  command = var.plan ? plan : apply
  assert {
    condition = aws_s3_bucket.datalake.bucket == var.datalake_bucket_name
    error_message = "Incorrect bucket name. Expected: ${var.datalake_bucket_name}, Got: ${aws_s3_bucket.datalake.bucket}"
  }
}

```

I want to be able to run this test as below to create the resource and run integration test. Also want to be able to use it for unit tests with plan=true

```auto
$ terraform test -var plan=false --filter=tests\test_s3_resource.tftest.hcl

```

But, I am getting the following error -

```auto
The "command" argument requires one of the following keywords without quotes: apply or plan.

```

Is there a way I can accomplish this, or the command attribute cannot take any variables or conditionals?

---

<div class="post-metadata">

**Author:** ![liamcervante](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/liamcervante/32/31449_2.png) [@liamcervante](https://discuss.hashicorp.com/u/liamcervante)\
**Post date:** [August 19, 2024, 9:52am UTC](https://discuss.hashicorp.com/t/terraform-test-conditional-command-attribute-plan-or-apply-for-unit-tests-and-integration-tests/69346/2 "2024-08-19T09:52:57Z")

</div>

Hi @think_tank_77, unfortunately what you’re attempting here isn’t currently possibly.

I’d recommend filing a feature request for this: [Terraform Issues](https://github.com/hashicorp/terraform/issues/new). We can use that to gauge interest and assign priority to potentially implementing this in the future.

Thanks!

---

<div class="post-metadata">

**Author:** ![liamcervante](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/liamcervante/32/31449_2.png) [@liamcervante](https://discuss.hashicorp.com/u/liamcervante)\
**Post date:** [August 19, 2024, 9:54am UTC](https://discuss.hashicorp.com/t/terraform-test-conditional-command-attribute-plan-or-apply-for-unit-tests-and-integration-tests/69346/3 "2024-08-19T09:54:51Z")

</div>

You could also take a look at this issue: [Terraform Test - Add plan or apply as a command line variable · Issue #34359 · hashicorp/terraform · GitHub](https://github.com/hashicorp/terraform/issues/34359)

That’s another potential approach to solving this kind of problem - but it is also awaiting prioritisation.

---

<div class="post-metadata">

**Author:** ![think\_tank\_77](https://avatars.discourse-cdn.com/v4/letter/t/58f4c7/32.png) [@think\_tank\_77](https://discuss.hashicorp.com/u/think_tank_77)\
**Post date:** [August 19, 2024, 11:11am UTC](https://discuss.hashicorp.com/t/terraform-test-conditional-command-attribute-plan-or-apply-for-unit-tests-and-integration-tests/69346/4 "2024-08-19T11:11:39Z")

</div>

Thank you @liamcervante. This helps. On similar lines, is there a way to skip tests or use an attribute to ignore an assert statement within a run block?

---

<div class="post-metadata">

**Author:** ![liamcervante](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/liamcervante/32/31449_2.png) [@liamcervante](https://discuss.hashicorp.com/u/liamcervante)\
**Post date:** [August 19, 2024, 11:33am UTC](https://discuss.hashicorp.com/t/terraform-test-conditional-command-attribute-plan-or-apply-for-unit-tests-and-integration-tests/69346/5 "2024-08-19T11:33:24Z")

</div>

There isn’t a way to skip individual run blocks currently.

You could ignore an assertion using the ternary operator. Something like `condition = var.skip ? true : <actual assertion>` would mean the assertion always passes if `var.skip` is assigned to true. There isn’t a built-in approach for this.

---

<div class="post-metadata">

**Author:** ![think\_tank\_77](https://avatars.discourse-cdn.com/v4/letter/t/58f4c7/32.png) [@think\_tank\_77](https://discuss.hashicorp.com/u/think_tank_77)\
**Post date:** [August 19, 2024, 11:49am UTC](https://discuss.hashicorp.com/t/terraform-test-conditional-command-attribute-plan-or-apply-for-unit-tests-and-integration-tests/69346/6 "2024-08-19T11:49:16Z")

</div>

Thank you. That’s what I am currently doing. Thought there would be a cleaner approach/way of doing it.

---

<div class="post-metadata">

**Author:** ![think\_tank\_77](https://avatars.discourse-cdn.com/v4/letter/t/58f4c7/32.png) [@think\_tank\_77](https://discuss.hashicorp.com/u/think_tank_77)\
**Post date:** [August 19, 2024, 1:29pm UTC](https://discuss.hashicorp.com/t/terraform-test-conditional-command-attribute-plan-or-apply-for-unit-tests-and-integration-tests/69346/7 "2024-08-19T13:29:21Z")

</div>

unrelated to the issue but similar question – instead of specifying

```auto
command = plan

```

for every run block, is there a way to infer it globally for the entire test file or through command line or specifying at the start of the file or the first run block?

---

<div class="post-metadata">

**Author:** ![liamcervante](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/liamcervante/32/31449_2.png) [@liamcervante](https://discuss.hashicorp.com/u/liamcervante)\
**Post date:** [October 7, 2024, 8:37am UTC](https://discuss.hashicorp.com/t/terraform-test-conditional-command-attribute-plan-or-apply-for-unit-tests-and-integration-tests/69346/8 "2024-10-07T08:37:57Z")

</div>

Sorry for the delayed response! That’s also not currently a way to specify this globally yet. We do have a tracking request for something similar as an enhancement here: [Terraform Test - Add plan or apply as a command line variable · Issue #34359 · hashicorp/terraform · GitHub](https://github.com/hashicorp/terraform/issues/34359).

---

<div class="post-metadata">

**Author:** ![wyardley](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/wyardley/32/21845_2.png) [@wyardley](https://discuss.hashicorp.com/u/wyardley)\
**Post date:** [October 7, 2024, 4:10pm UTC](https://discuss.hashicorp.com/t/terraform-test-conditional-command-attribute-plan-or-apply-for-unit-tests-and-integration-tests/69346/9 "2024-10-07T16:10:03Z")

</div>

side note: you’ll probably want to add

```auto
  plan_options {
    refresh = false
  }

```

to that too.

Using a mock provider may be another option to consider for your unit tests; I’ve found there are some pros and cons.

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/hashicorp/original/1X/79bf76adfd92a75631996933098f9ce29d37abf3.svg) [@system](https://discuss.hashicorp.com/u/system)\
**Post date:** [December 8, 2024, 4:10pm UTC](https://discuss.hashicorp.com/t/terraform-test-conditional-command-attribute-plan-or-apply-for-unit-tests-and-integration-tests/69346/10 "2024-12-08T16:10:07Z")

</div>

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