How to print terraform plan result in a acceptance testing

My acceptance testing code is as below, questions:

  1. how can I print the terraform plan result ?
  2. if I set ExpectNonEmptyPlan as false, why I got error Step 2/2 error: After applying this test step, the plan was not empty
resource.Test(t, resource.TestCase{
    ProtoV6ProviderFactories: testAccProtoV6ProviderFactories(testClient),
    Steps: []resource.TestStep{
      // create
      {
        Config: toTfConfig(providerConfig(), resourceConfig()),
        ConfigPlanChecks: resource.ConfigPlanChecks{
          PreApply: []plancheck.PlanCheck{
            plancheck.ExpectResourceAction(testResourceName, plancheck.ResourceActionCreate),
          },
        },
        Check: resource.ComposeTestCheckFunc(
          resource.TestCheckResourceAttr(testResourceName, "state", "test"),
        ),
      },
      // update one property as true, trigger destroy and create
      {
        Config: toTfConfig(providerConfig(), config),
        ConfigPlanChecks: resource.ConfigPlanChecks{
          PreApply: []plancheck.PlanCheck{
            plancheck.ExpectResourceAction(testResourceName, plancheck.ResourceActionDestroyBeforeCreate),
          },
        },
        ExpectNonEmptyPlan: true,
        Check: resource.ComposeTestCheckFunc(
          resource.TestCheckResourceAttr(testResourceName, "state", "test"),
        ),
      },
    },
  })

thanks.

Hi @shufanhao :wave:t6:

We don’t have a built-in way to print out the results of a Terraform plan to the console in terraform-plugin-testing. If you just want to see the results of a plan for debugging purposes, I would suggest setting a debug point inside the CheckPlan() method on a plan check that’s being used within the test. The CheckPlanRequest has access to the Terraform plan, so you can inspect the fields for debugging.

You can also write your own plan check that implements the PlanCheck interface that prints out or logs certain fields in the Terraform Plan.

ExpectNonEmptyPlan: false is meant to be used when the test step is a no-op (generates no resource actions) and produces an empty plan. Since your second test step is expecting ResourceActionDestroyBeforeCreate, you are expecting a non-empty plan, so ExpectNonEmptyPlan: should be set to true like in the code snippet you provided above.

thanks, but why my first testStep, didn’t set ExpectNonEmptyPlan as true, but it is working ?