A framework for testing Terraform modules using Terratest with a focus on functional testing and parallel execution of examples.
This framework provides a structured way to test Terraform modules by:
- Automatically discovering and running all examples in parallel
- Testing idempotency of Terraform code
- Providing common assertions for Terraform outputs and resources
- Supporting custom test functions for advanced verification
- Simplifying test setup and execution
- Parallel Example Testing: Run all examples in the
examples/directory in parallel - Flexible Parallelism Control: Control parallelism at both test fixture and individual test levels
- Idempotency Testing: Verify that Terraform code is idempotent by running a plan after apply
- Common Assertions: Pre-built assertions for common testing scenarios
- Custom Tests: Support for custom test functions to verify specific resource behaviors
- Automatic Discovery: Automatically finds and runs tests on all examples without manual configuration
- Configurable: Easily customize test configurations for each example
- Environment Control: Disable idempotency testing with the
TERRATEST_IDEMPOTENCY=falseenvironment variable
The easiest way to set up your development environment is using the Caylent Devcontainer:
-
Install the Caylent Devcontainer CLI:
pip install caylent-devcontainer-cli
-
Set up the devcontainer in your project:
cdevcontainer setup-devcontainer .Follow the interactive prompts to configure your environment.
-
Launch VS Code with the devcontainer:
cdevcontainer code
Accept the prompt to reopen in container when VS Code launches.
For more detailed instructions on using the Caylent Devcontainer, run cdevcontainer --help or refer to the Caylent Devcontainer documentation.
For details on the required directory structure, see the Directory Structure Documentation.
For details on installing and using the tftest CLI, see the CLI Usage Documentation.
-
AWS Authentication: Tests require AWS credentials to be available
export AWS_ACCESS_KEY_ID=your_access_key export AWS_SECRET_ACCESS_KEY=your_secret_key export AWS_REGION=us-west-2
Or using AWS SSO:
aws sso login --profile your-profile export AWS_PROFILE=your-profile -
Test Control (optional):
# To disable idempotency testing export TERRATEST_IDEMPOTENCY=false # To control parallelism of tests within fixtures export TERRATEST_DISABLE_PARALLEL_TESTS=true # Disable parallel tests within fixtures
For details on writing tests, see the Writing Tests Documentation.
The testctx package is the core of the framework, providing essential functionality for running and managing Terraform tests:
// Run a single example ctx := testctx.RunSingleExample(t, "../../examples", "example1", testctx.TestConfig{ Name: "example1-test", ExtraVars: map[string]interface{}{ "region": "us-west-2", }, }) // Run all examples results := testctx.RunAllExamples(t, "../../examples", nil) // Run custom tests on all examples testctx.RunCustomTests(t, results, verifyS3Bucket)
For detailed documentation on the testctx package, see the TestCtx Package Documentation.
By default, the framework automatically runs idempotency tests for all Terraform examples. This ensures your Terraform code is idempotent (running it multiple times produces the same result).
The idempotency test:
- Runs automatically when using
RunExampleorRunAllExamplesfunctions - Verifies that running
terraform planafterterraform applyshows no changes - Is enabled by default
To disable idempotency testing (useful when there are known issues with providers):
TERRATEST_IDEMPOTENCY=false tftest run
The test will run if:
TERRATEST_IDEMPOTENCYenvironment variable doesn't existTERRATEST_IDEMPOTENCYis set to any value other than "false"
The test will be skipped if:
TERRATEST_IDEMPOTENCYis set to "false"
You can control parallelism at two levels:
- Test Fixtures: Control whether different test fixtures run in parallel
- Tests Within Fixtures: Control whether tests within a single fixture run in parallel
By default, both levels of parallelism are disabled for maximum stability. You can enable them as needed:
# Run test fixtures in parallel tftest run --parallel-fixtures=true # Run tests within fixtures in parallel tftest run --parallel-tests=true # Enable both levels of parallelism tftest run --parallel-fixtures=true --parallel-tests=true
Controlling parallelism is particularly useful when:
- Tests interact with shared state files
- Tests modify the same AWS resources
- You're experiencing intermittent failures due to race conditions
You can also control test-level parallelism using an environment variable:
# Disable parallel tests within fixtures export TERRATEST_DISABLE_PARALLEL_TESTS=true
AssertIdempotent: Verifies that a Terraform plan shows no changes after applyAssertOutputEquals: Checks if a specified Terraform output matches an expected valueAssertOutputContains: Checks if a specified Terraform output contains an expected substringAssertOutputMatches: Checks if a specified Terraform output matches a regular expressionAssertOutputNotEmpty: Checks if a specified Terraform output is not emptyAssertOutputEmpty: Checks if a specified Terraform output is empty
AssertFileExists: Checks if a file exists at the path specified by theoutput_file_pathTerraform outputAssertFileContent: Checks if theoutput_contentTerraform output matches the expected value
AssertOutputMapContainsKey: Checks if a Terraform map output contains a specific keyAssertOutputMapKeyEquals: Checks if a key in a Terraform map output equals an expected valueAssertOutputListContains: Checks if a Terraform list output contains an expected valueAssertOutputListLength: Checks if a Terraform list output has the expected length
AssertOutputJSONContains: Checks if a JSON string output contains an expected key-value pair
AssertResourceExists: Checks if a specific resource exists in the Terraform stateAssertResourceCount: Checks if the number of resources of a specific type matches the expected countAssertNoResourcesOfType: Checks that no resources of a specific type exist in the Terraform state
AssertTerraformVersion: Checks if the Terraform version meets the minimum required version
For detailed documentation on all assertions, including usage examples and requirements, see the Assertions Documentation.
- Directory Structure - Required directory structure for using the framework
- CLI Usage - How to use the TFTest CLI
- Writing Tests - Guide to writing tests with the framework
- TestCtx Package - Documentation for the core testctx package
- Standard Tests - Standard tests to run on every Terraform module
- Assertions Documentation - Detailed guide to all built-in assertions
- Testing Guide - Comprehensive guide to writing and organizing tests
- Error Handling - Error handling strategy for the framework
- Logging - Logging framework for the framework
- Benchmarking - Benchmarking and performance optimization
- CI/CD Pipeline - Overview of the CI/CD pipeline and release process
- Contributing Guide - Guidelines for contributing to the project
For a complete working example of how to use this framework in a real Terraform module, see the Example Directory. This example demonstrates best practices for structuring and testing Terraform modules using this framework.
For simple test fixtures used by the framework's own tests, see the tests/terraform-module-test-fixtures directory.
To install the framework as a dependency in your Terraform module project:
go get github.com/caylent-solutions/terraform-terratest-framework
This adds the framework to your module's go.mod file, allowing you to use its testing capabilities in your Go test files.
To install the CLI tool:
go install github.com/caylent-solutions/terraform-terratest-framework/cmd/tftest
- Go 1.23 or later - Required to run the Go tests
- Terraform CLI - Required to execute the
terraformcommands used by the framework - AWS credentials (if testing AWS resources) - Required for deploying and testing AWS resources
make install-tools make install
# Run all tests (unit tests and functional tests) make test # Run all tests including example tests make test-all # List all functional tests with descriptions make list-functional-tests # Run a specific functional test FUNCTIONAL_TEST=TestName make run-specific-functional-test
The list-functional-tests command will show all available functional tests in the project. This is useful when you need to run a specific test or understand what tests are available.
The run-specific-functional-test command allows you to run a single functional test by name. You must set the FUNCTIONAL_TEST environment variable to the name of the test you want to run. For example:
FUNCTIONAL_TEST=TestCliVersion make run-specific-functional-test
Releases are typically managed through the GitHub Actions pipeline. However, for manual releases outside of the pipeline, you can use the make release-manual command:
# Create a manual release based on commit messages make release-manual # Explicitly specify the version bump type make release-manual TYPE=major make release-manual TYPE=minor make release-manual TYPE=patch
Note: This manual release process should only be used in exceptional circumstances when the automated pipeline cannot be used.
The release process automatically determines the version bump type based on conventional commit messages:
- Major version bump is triggered by:
BREAKING CHANGE:prefix- Any prefix with an exclamation mark (
!):feat!:,fix!:,refactor!:, etc.
- Minor version bump is triggered by:
feat:orfeature:prefix
- Patch version bump is triggered by all other prefixes:
fix:- Bug fixesdocs:- Documentation changesstyle:- Code style changesrefactor:- Code refactoringtest:- Test changeschore:- Routine tasksci:- CI/CD changesbuild:- Build system changesperf:- Performance improvements
We welcome contributions from both external contributors and Caylent team members. Please see our Contributing Guide for details on how to contribute to this project.
This project is licensed under the Apache License, Version 2.0. See the LICENSE file for details.