Mock providers and negative tests
Test rejection as a useful behavior
A good input contract rejects unsupported values. In the first lesson, replicas accepts whole numbers from one through three. The following two runs deliberately submit values outside that contract. A successful negative test means the named custom condition rejects its input; it does not mean Terraform should fail for any reason. A typo, incompatible type, missing provider or unexpected failure must not be hidden. A plan run is suitable because the validation can be evaluated before resource creation.
run "reject_zero" { command = plan variables { replicas = 0 } expect_failures = [var.replicas]}run "reject_fraction" { command = plan variables { replicas = 1.5 } expect_failures = [var.replicas]}Add a boundary case for three and a separate invalid case for four. If validation is removed, an expected-failure run should no longer pass. For conditions that depend on values available only at apply, choose the actual failing phase and account for earlier errors.
Replace external behavior with a controlled mock
Native provider mocking arrived in Terraform 1.7. A mock substitutes schema-shaped results for actual provider operations. This is useful for checking the module logic without creating an S3 bucket. It does not remove the need for a reviewed provider schema and initialization. The following AWS configuration and test are static illustrations using the earlier provisional provider release. Never initialize or run them as supplied; provider schema and package provenance have not been verified.
# Static example only: exact provider schema not verified.terraform { required_providers { aws = { source = "hashicorp/aws" version = "6.55.0" } }}resource "aws_s3_bucket" "example" { bucket = "bytemind-fictional-test-only"}output "bucket_id" { value = aws_s3_bucket.example.id}mock_provider "aws" { mock_resource "aws_s3_bucket" { defaults = { id = "synthetic-bucket-id" } }}run "mocked_bucket" { command = apply assert { condition = output.bucket_id == "synthetic-bucket-id" error_message = "Expected the configured mock ID." }}The assertion demonstrates how a deterministic computed ID can be supplied rather than relying on generated random strings. It does not prove the bucket exists, is private, or can be accessed by an actual identity.
Override a single object or module interface
A provider-wide mock may be too broad when two instances need different returned values. An override can replace the result of a particular addressed resource or data source; a module override can replace its outputs without exercising the underlying module. Here is an alternative test targeting one resource. Keep it separate from the previous test illustration.
# Alternative test fragment, not a standalone configuration.mock_provider "aws" {}run "one_override" { command = apply override_resource { target = aws_s3_bucket.example values = { id = "specific-fixture-id" } } assert { condition = output.bucket_id == "specific-fixture-id" error_message = "Expected the instance-specific fixture." }}Mocked values may be unknown at plan time by default. Where supported, override_during = plan changes the timing of generated or supplied values; verify the exact version and do not assume a computed value is known in a real plan. For a test with aliases, explicitly map the intended mocked configurations in the run providers mapping; unmocked providers may perform actual operations.
Choose the right level of evidence
A plan test can catch a wrong resource count, an apply test can examine local computed output, a mock can test how module logic reacts to supplied API-like values, and an authorized integration test can observe the real service. None establishes everything. For example, an assertion about an encryption configuration value is not proof of actual at-rest encryption. Test denied and permitted access in an authorized environment only, with bounded accounts and approved cleanup. Do not use actual secrets or generate real resources to practice writing tests.
Review the result and plan the next gate
Use the worksheet to classify failures, choose meaningful assertions and identify missing evidence. The supplied offline Python model independently checks the accepted numeric inputs and generated slot names. It is not a Terraform interpreter, does not verify HCL or mock semantics, and does not prove cleanup. For a future native run, record the exact CLI/provider versions, input, test file, exit code, diagnostics and post-test inventory. Retain the run log only after checking it for sensitive information. A failed cleanup is a separate blocker even when assertions passed.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Mock providers and negative tests — knowledge check
Review native tests and mocked behavior.
What should expect_failures target?
If an expected validation does not fail, what happens?
What does a mock bucket ID establish?
Which block targets one resource result?
Can mocking remove the need to review provider plugins?