-
Notifications
You must be signed in to change notification settings - Fork 9.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve documentation for terraform test
#36846
Comments
Hi @novekm, thanks for filing this. We do have examples of using the One thing to consider is that you can also always write tests directly within the root configuration directory. Terraform will pick up tests both within the |
Thanks Liam, that makes sense. Related to some of the other flags for Terraform test - are there any examples for targeting speficic directories for tests beyond the default I've been trying Additional challenges arose when trying to have a folder named
The goal was to be able to group these tests and with a GitHub action (comment on PR), trigger tests within specific sub-directories for the modules with something like "/test-perforce" (during updates to the module) or "/test-all-modules" (when cutting a new version release for the entire project). Running terraform test and trying to target specific directories when having the name of modules within the test directory threw this error:
This persisted even after renaming the sub directory and re-initializing Terraform. I ended up having to delete the entire .Terraform directory and the lock file, restart my IDE, and re-run Terraform init to resolve. I'm assuming |
One thing to bear in mind is you need to specify the test directory when initialising as well: The There should be no restrictions on naming or structure other than the test directory has to be "beneath" the configuration directory. It's also worth noting that Terraform can't load tests from multiple directories at a time and it doesn't load directories recursively. |
Hi @liamcervante, unfortunately I am still having some issues trying to use the
The
The s3 module and related tests are very basic currently, just to try to test the correct usage of the flags with basic example (main.tf)module "s3_bucket" {
source = "../../"
bucket_prefix = "kevon-test-basic"
}
For the tests, they simply reference the examples in the 01_mandatory_basic.tftest.hclrun "unit_test" {
command = plan
module {
source = "./examples/basic"
}
}
run "e2e_test" {
command = apply
module {
source = "./examples/basic"
}
} 02_mandatory_advanced.tftest.hclrun "unit_test" {
command = plan
module {
source = "./examples/advanced"
}
}
run "e2e_test" {
command = apply
module {
source = "./examples/advanced"
}
}
If I understand what you're saying, to run a test against that specific
terraform init -test-directory=modules/s3/tests
terraform test -test-directory=modules/s3/tests However, when I try to run the first command, I get a response that the Terraform was initialized in an empty directory: ❯ terraform init -test-directory=modules/s3/tests
Terraform initialized in an empty directory! To get this to work, I instead had to use the terraform -chdir=modules/s3 init
Initializing the backend...
Initializing modules...
- test.tests.02_mandatory_advanced.unit_test in examples/advanced
- test.tests.02_mandatory_advanced.unit_test.s3_bucket in .
- test.tests.02_mandatory_advanced.e2e_test in examples/advanced
- test.tests.02_mandatory_advanced.e2e_test.s3_bucket in .
- test.tests.01_mandatory_basic.unit_test in examples/basic
- test.tests.01_mandatory_basic.unit_test.s3_bucket in .
- test.tests.01_mandatory_basic.e2e_test in examples/basic
- test.tests.01_mandatory_basic.e2e_test.s3_bucket in .
Initializing provider plugins...
- Finding latest version of hashicorp/aws...
- Installing hashicorp/aws v5.94.1...
- Installed hashicorp/aws v5.94.1 (signed by HashiCorp)
Terraform has created a lock file .terraform.lock.hcl to record the provider
selections it made above. Include this file in your version control repository
so that Terraform can guarantee to make the same selections by default when
you run "terraform init" in the future. Then I didn't even have to use the -test-directory flag since the s3 module is using the default terraform -chdir=modules/s3 test
tests/01_mandatory_basic.tftest.hcl... in progress
run "unit_test"... pass
run "e2e_test"... pass
tests/01_mandatory_basic.tftest.hcl... tearing down
tests/01_mandatory_basic.tftest.hcl... pass
tests/02_mandatory_advanced.tftest.hcl... in progress
run "unit_test"... pass
run "e2e_test"... pass
tests/02_mandatory_advanced.tftest.hcl... tearing down
tests/02_mandatory_advanced.tftest.hcl... pass
Success! 4 passed, 0 failed. I'm pretty confused on which should be used since I was never able to get the |
Terraform Version
Terraform v1.10.4 on darwin_arm64 + provider registry.terraform.io/hashicorp/aws v5.94.0 Your version of Terraform is out of date! The latest version is 1.11.3. You can update by downloading from https://www.terraform.io/downloads.html
Affected Pages
https://developer.hashicorp.com/terraform/cli/commands/test#terraform-test-command
What is the docs issue?
The current documentation for
terraform test
is not explicit enough with the example usage, especially for the optional parameters such as-filter
. The docs mention:-filter=testfile - Limits the terraform test operation to the specified test files.
However the above doesn't work. With a set of tests within a recommended
/tests
directory with filenames such as01_test.tftest.hcl
,02_test.tftest.hcl
etc, runningterraform test -filter=02_test.tftest.hcl
or eventerraform test -filter="02_test.tftest.hcl"
does not work:Instead, you must write the filter as a string, and include the full path to the file, including the
/tests
directory.I could understand this if using a non-standard directory name for your tests, however this is not expected if using the default recommended naming of
/tests
. One would assume that ifterraform test
automatically uses test files (*.tftest.hcl
) that are contained in a directory with this name, then the simple filter should function the same way.Proposal
Please update the docs with more examples and explicitly mention the way you must use filters to reference specific test files, and call out that it's required to directly reference the directory name, even if it is the default value. In the longterm, perhaps it would be possible to omit having to pass in the directory name in the filter if using the default naming of
/tests
🙂References
terraform test documentation
The text was updated successfully, but these errors were encountered: