Testing Functions
When a function is implemented, ensure the function behaves as expected. Follow recommendations to cover how practitioner configurations may call the function.
There are two methodologies for testing provider-defined functions:
- Acceptance Testing: Verify implementation using real Terraform configurations and commands.
- Unit Testing: Verify implementation using with Terraform and framework implementation details.
Similar to other provider concepts, many provider developers prefer acceptance testing over unit testing. Acceptance testing guarantees the function implementation works exactly as expected in real world use cases without trying to determine Terraform or framework implementation details. Unit testing details are provided, however, for function implementations which warrant a broad amount of input value testing, such as generic data handling functions or to perform fuzzing.
Testing examples on this page are dependent on the example echo function implementation.
Recommendations
Testing a provider-defined function should ensure at least the following behaviors are covered:
- Known values return the expected results.
- For any list, map, object, and set parameters, null values for collection elements or object attributes. The
AllowNullValue
parameter setting does not affect Terraform sending these types of null values. - If any parameters enable
AllowNullValue
, null values for those arguments. - If any parameters enable
AllowUnknownValues
, unknown values for those arguments. - Any errors, such as argument validation errors.
Acceptance Testing
Use the plugin testing Go module to implement real world testing with Terraform configurations and commands. The documentation for that Go module covers many more available testing features, however this section example gives a high level overview of how to start writing these tests.
In this example, a echo_function_test.go
file is created:
Unit Testing
Use the function.NewArgumentsData()
function and function.NewResultData()
function as part of implementing a Go test.
In this example, a echo_function_test.go
file is created: