Unit Testing
Tip
Terraform Provider testing has migrated to a new terraform-plugin-testing Go module. New features will only be implemented in the new module. Review the migration documentation to learn how to switch existing testing code.
Testing plugin code in small, isolated units is distinct from Acceptance Tests, and does not require network connections. Unit tests are commonly used for testing helper methods that expand or flatten API responses into data structures for storage into state by Terraform. This section covers the specifics of writing Unit Tests for Terraform Plugin code.
The procedure for writing unit tests for Terraform follows the same setup and
conventions of writing any Go unit tests. We recommend naming tests to follow
the same convention as our acceptance tests, Test<Provider>_<Test Name>
. For more
information on Go tests, see the official Golang docs on testing.
Below is an example unit test used in flattening AWS security group rules,
demonstrating a typical flattener
type method that's commonly used to convert
structures returned from APIs into data structures used by Terraform in saving
to state. This example is truncated for brevity, but you can see the full test in the
aws/structure_test.go in the Terraform AWS Provider
repository on GitHub