Terraform test keep complaining about providers

Terraform folder structure ready to run terraform test command

.
├── main.tf
├── modules
│   └── resource_group
│       ├── main.tf
│       ├── output.tf
│       └── variables.tf
├── providers.tf
└── tests
└── main.tftest.hcl

providers.tf

terraform {

  required_providers {

azurerm = {

source  = "hashicorp/azurerm"

version = "\~> 4.10.0"

    }

  }

}



provider "azurerm" {

features {}

}

Error below

% terraform test -verbose
tests/main.tftest.hcl... in progress
run "verify_azure_resource_group_creation"... fail
╷
│ Error: Invalid provider configuration
│
│ Provider " registry.terraform.io/hashicorp/azurerm " requires explicit configuration. Add a provider block to the root module and configure
│ the provider's required arguments as described in the provider documentation.
│
╵
╷
│ Error: Missing required argument
│
│   with provider\[" registry.terraform.io/hashicorp/azurerm "\],
│   on  line 0:
│   (source code not available)
│
│ The argument "features" is required, but no definition was found.
╵
tests/main.tftest.hcl... tearing down
tests/main.tftest.hcl... fail

Failure! 0 passed, 1 failed

I’m struggling to get this running, as I don’t understand how the terraform test folder structure is working. My aim to define the folder structure like above where all terraform resources are in each subfolder of modules folder and the final terraform module declarative is in the root folder. What did I do wrong?

With terraform test you need to supply the provider configuration inside the test file (main.tftest.hcl) – the root module’s providers.tf is ignored for the test run.

add these to tests/main.tftest.hcl

terraform {
  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = "~> 4.10.0"
    }
  }
}

Provider configuration used by the test run

providers {
  azurerm = {
    features = {}
  }
}

After adding the provider blocks to the test file, run:

terraform test -verbose

You should see the test proceed without the provider errors.