0
0
Terraformcloud~10 mins

Mock providers in tests in Terraform - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Mock providers in tests
Write Terraform config
Define mock provider
Run terraform test
Terraform uses mock provider
Test resource creation & outputs
Verify expected behavior
Test passes or fails
This flow shows how Terraform tests use mock providers to simulate real providers, allowing tests to run without actual cloud resources.
Execution Sample
Terraform
terraform {
  required_providers {
    mock = {
      source = "hashicorp/mock"
      version = "~> 0.1"
    }
  }
}

provider "mock" {}

resource "mock_resource" "test" {
  name = "example"
}
This Terraform code sets up a mock provider and a mock resource to test resource creation without real cloud calls.
Process Table
StepActionTerraform StateMock Provider BehaviorTest Outcome
1Initialize Terraform with mock providerNo resources createdMock provider ready to simulateNo test result yet
2Apply configurationResource 'mock_resource.test' plannedSimulates resource creationResource creation simulated
3Terraform creates mock_resource.testResource state stored in memoryReturns fake ID and attributesResource appears created
4Run test assertionsResource state accessibleMock provider returns expected outputsAssertions pass if outputs match
5Test completesState unchangedNo real cloud calls madeTest passes if all assertions succeed
6ExitTest environment cleanedMocks resetTesting ends
💡 Test ends after assertions pass or fail; no real infrastructure is created due to mock provider
Status Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
terraform_stateemptyplanned resource mock_resource.testresource created in memoryresource state accessiblecleaned
mock_providerinitializedready to simulatereturns fake resource IDreturns expected outputsreset
Key Moments - 3 Insights
Why does the test not create real cloud resources?
Because the mock provider simulates resource creation and returns fake data, as shown in execution_table steps 2 and 3.
How does Terraform know to use the mock provider during tests?
The Terraform configuration specifies the mock provider source and version, so Terraform uses it instead of a real provider (see execution_table step 1).
What happens if the mock provider returns unexpected outputs?
Test assertions will fail during step 4, causing the test to fail, since outputs do not match expected values.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the Terraform state after step 3?
AResource state stored in memory
BNo resources created
CResource state cleaned
DResource planned but not created
💡 Hint
Check the 'Terraform State' column in row for step 3
At which step does the mock provider simulate resource creation?
AStep 1
BStep 4
CStep 2
DStep 5
💡 Hint
Look at the 'Mock Provider Behavior' column for when resource creation is simulated
If the mock provider returned wrong outputs, what would happen in the test?
ATest assertions would pass
BTest assertions would fail
CTerraform would create real resources
DTerraform would skip resource creation
💡 Hint
Refer to key_moments about unexpected outputs and execution_table step 4
Concept Snapshot
Mock providers in Terraform tests simulate real providers.
They let you test resource creation without real cloud calls.
Define mock provider in Terraform config.
Terraform uses mock provider during tests.
Tests check resource state and outputs.
No real infrastructure is created.
Full Transcript
This visual execution shows how Terraform uses mock providers in tests. First, Terraform config defines the mock provider. When tests run, Terraform initializes with the mock provider, which simulates resource creation by returning fake IDs and attributes. The Terraform state updates in memory without creating real cloud resources. Tests then assert expected outputs from the mock resource. If outputs match, tests pass; otherwise, they fail. After tests, the mock environment resets. This approach allows safe, fast testing of Terraform code without real infrastructure changes.