Terraform
- Plugin Framework
- v1.16.x (latest)
- No versions of this document exist before v1.15.x. Click below to redirect to the version homepage.
- v1.15.x
- v1.14.x
- v1.13.x
- v1.12.x
- v1.11.x
- v1.10.x
- v1.9.x
- v1.8.x
- v1.7.x
- v1.6.x
- v1.5.x
- v1.4.x
- v1.3.x
- v1.2.x
- v1.1.x
- v1.0.x
- v0.17.x
- v0.16.x
- v0.15.x
- v0.14.x
- v0.13.x
- v0.12.x
- v0.11.x
- v0.10.x
- v0.9.x
- v0.8.x
Acceptance Tests
You can implement testing with the acceptance test framework shipped with SDKv2.
Writing and running tests is similar to SDKv2 providers, with the following exceptions:
TestCase: Specify the provider withProtoV6ProviderFactories.Schema: A root levelidattribute is required for resources and data sources.
Specify Providers
In SDKv2, providers were specified by using the Providers property of the
resource.TestCase to supply a map of
schema.Providers.
For the framework, the same pattern applies, but instead use the
ProtoV6ProviderFactories property of
resource.TestCase
to supply a map of functions that return a
tfprotov6.ProviderServer.
To get a tfprotov6.ProviderServer from a
tfsdk.Provider,
you need to use the
providerserver.NewProtocol6WithError
helper. For example:
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error) {
// newProvider is your function that returns a tfsdk.Provider
"example_provider": providerserver.NewProtocol6WithError(newProvider()),
},
CheckDestroy: testAccCheckExampleResourceDestroy,
Steps: []resource.TestStep{
{
Config: testAccExampleResource,
Check: testAccCheckExampleResourceExists,
},
},
})
See the TestCase
documentation for more
information on using resource.TestCase.
Implement id Attribute
In SDKv2, resources and data sources automatically included an implicit, root level id attribute. In the framework, the id attribute is not implicitly added.
When testing resources and data sources without the id attribute, the acceptance testing framework will return errors such as:
testing_new_config.go:111: no "id" found in attributes
testing_new.go:53: no "id" found in attributes
To avoid this, add a root level id attribute to resource and data source schemas. Ensure the attribute value is appropriately written to state. Conventionally, id is a computed attribute that contains the identifier for the resource.
For example, in the GetSchema method implementation of a DataSourceType or ResourceType:
func (t exampleResourceType) GetSchema(ctx context.Context) (tfsdk.Schema, diag.Diagnostics) {
return tfsdk.Schema{
// ... potentially other schema configuration ...
Attributes: map[string]tfsdk.Attribute{
// ... potentially other schema attributes ...
"id": {
Type: types.StringType,
Computed: true,
},
},
}, nil
}