Testing module outputs with mocked provider

Let’s say I have a module that I’m testing that uses a mocked provider. Is there a way to test the module’s outputs?

Obviously, some of the attributes within the fake provider will be fake / generated. But let’s say a simple example:

in main.tf

resource "google_secret_manager_secret" "this" {
  secret_id = var.secret_id
  replication {
    auto {}
  }
}

in outputs.tf

# Granted, in this case, we know what we're going to get, but a test could get
# more interesting if we did some mangling of the value that we wanted to
# test the logic of.
output "secret_id" {
  description = "Secret ID for created secret."
  value       = google_secret_manager_secret.this.secret_id
}

I couldn’t find much in terms of any docs on a way, either with the mocked provider, or with a plan-only test (even though in a simplistic scenario like this where the attribute value is known, the plan will print the output). Is there an assertion that will already handle this scenario?

Hi @wyardley, you should just be able to reference the output directly in an assertion block:

run "correct_output" {
    variables {
      secret_id = "my-secret"
    }
    assert {
        condition = output.secret_id == "my-secret"
        error_message = "bad secret"
    }
}

For reference, it’s mentioned in the docs here: Tests - Configuration Language | Terraform | HashiCorp Developer.

Thanks @liamcervante

I have no idea how I didn’t try this and / or how I didn’t come across that example, but much appreciated. It works as expected.