Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is the purpose of a test file in Terraform?
A test file in Terraform is used to check if your infrastructure code works as expected before applying changes. It helps catch errors early.
Click to reveal answer
beginner
Where should Terraform test files be placed in your project?
Terraform test files are usually placed in a separate folder named tests or alongside your modules with a _test.tf suffix to keep tests organized and easy to find.
Click to reveal answer
beginner
What naming convention is commonly used for Terraform test files?
Test files often use the suffix _test.tf to clearly indicate they contain tests, making it easy to separate them from main configuration files.
Click to reveal answer
intermediate
Why is it important to keep test files separate from production Terraform files?
Keeping test files separate prevents accidental deployment of test resources and helps maintain a clean, understandable project structure.
Click to reveal answer
intermediate
What tools can be used to run tests on Terraform configurations?
Tools like terraform validate, terraform plan, and third-party tools like terratest help automate testing of Terraform code.
Click to reveal answer
Where is the best place to put Terraform test files?
AIn the root directory only
BMixed with production files without distinction
CInside the <code>.terraform</code> hidden folder
DIn a separate <code>tests</code> folder or with a <code>_test.tf</code> suffix
✗ Incorrect
Test files should be organized separately or clearly named to avoid confusion and accidental deployment.
What suffix is commonly used for Terraform test files?
A_test.tf
B_prod.tf
C_config.tf
D_deploy.tf
✗ Incorrect
The _test.tf suffix clearly marks files as tests.
Which command helps check Terraform code syntax before deployment?
Aterraform apply
Bterraform destroy
Cterraform validate
Dterraform output
✗ Incorrect
terraform validate checks the syntax and configuration correctness.
Why keep test files separate from production files?
ATo prevent accidental deployment of test resources
BTo hide test files from version control
CTo reduce file size
DTo speed up deployment
✗ Incorrect
Separating test files avoids deploying test resources by mistake.
Which tool is NOT typically used for testing Terraform code?
Aterraform plan
Bdocker-compose
Cterraform validate
Dterratest
✗ Incorrect
docker-compose is unrelated to Terraform testing.
Explain how you would organize test files in a Terraform project and why.
Think about how to keep test code apart from production code.
You got /4 concepts.
Describe the role of terraform validate in testing Terraform configurations.
It is a command to check your code before applying.
You got /4 concepts.
Practice
(1/5)
1. What is the main reason to keep Terraform test files separate from production code files?
easy
A. Because Terraform does not support test files inside the main folder
B. To avoid mixing test code with production code and keep the project organized
C. To reduce the size of the Terraform state file
D. Because test files must be written in a different language
Solution
Step 1: Understand file organization purpose
Separating test files helps keep the project clean and easier to manage by not mixing test and production code.
Step 2: Recognize Terraform best practices
Terraform encourages clear separation to avoid confusion and accidental deployment of test code.
Final Answer:
To avoid mixing test code with production code and keep the project organized -> Option B
Quick Check:
Separate test files = organized project [OK]
Hint: Keep test files separate to avoid confusion [OK]
Common Mistakes:
Thinking test files must be in main folder
Believing test files affect state size
Assuming test files require different language
2. Which of the following is the correct folder structure for organizing Terraform files including tests?
easy
A. /main.tf, /variables.tf, /outputs.tf, /test/main_test.tf
B. /main.tf, /variables.tf, /outputs.tf, /modules/tests/test_main.tf
C. /main.tf, /variables.tf, /outputs.tf, /tests/test_main.tf
D. /main.tf, /variables.tf, /outputs.tf, /tests/main_test.tf
Solution
Step 1: Identify standard test folder naming
Tests are usually placed in a separate folder named 'tests' at the root level for clarity.
Step 2: Check file naming conventions
Test files often have '_test' suffix to indicate their purpose, e.g., 'main_test.tf'.
Final Answer:
/main.tf, /variables.tf, /outputs.tf, /tests/main_test.tf -> Option D
Quick Check:
Tests folder with *_test.tf files = correct structure [OK]
Hint: Use 'tests' folder with *_test.tf files [OK]
Running terraform apply gives an error about duplicate resource definitions. What is the likely cause?
medium
A. Outputs are missing in outputs.tf
B. Variables are defined twice in variables.tf
C. Terraform is loading test_main.tf and main.tf causing duplicate resources
D. Terraform state file is corrupted
Solution
Step 1: Analyze error cause
Duplicate resource errors usually mean Terraform sees the same resource defined more than once.
Step 2: Check file loading
If test files are accidentally loaded (e.g., by running terraform in /tests), resources duplicate with main.tf definitions.
Final Answer:
Terraform is loading test_main.tf and main.tf causing duplicate resources -> Option C
Quick Check:
Duplicate resources = multiple files loaded [OK]
Hint: Run terraform only in root, not in tests folder [OK]
Common Mistakes:
Blaming variables or outputs for duplicate resource error
Ignoring that test files can cause duplicates if loaded
Assuming state file corruption without checking files
5. You want to reuse infrastructure code in multiple projects and keep tests separate. Which folder structure best supports this using Terraform modules and tests?
hard
A. /modules/network/main.tf, /modules/network/variables.tf, /tests/network_test.tf, /main.tf
B. /network/main.tf, /network/variables.tf, /tests/network_test.tf, /main.tf
C. /modules/network/main.tf, /tests/network/main_test.tf, /main.tf
D. /modules/network/main.tf, /modules/network/tests/network_test.tf, /main.tf
Solution
Step 1: Identify module folder best practice
Modules should be inside a 'modules' folder with their own files for reuse.
Step 2: Separate tests outside modules
Tests should be in a top-level 'tests' folder to avoid mixing with reusable module code.
Final Answer:
/modules/network/main.tf, /modules/network/variables.tf, /tests/network_test.tf, /main.tf -> Option A
Quick Check:
Modules in 'modules', tests in 'tests' folder [OK]
Hint: Keep modules and tests in separate top-level folders [OK]