1 of 26

1

HashiTalks 2025

© HASHICORP

2 of 26

2

Stop breaking things

and start testing!

Terraform Test Framework - Hallway Track Follow-Up!

© HASHICORP

3 of 26

3

HashiCorp Ambassador

LinkedIn: jeremy-j-myers

Jeremy

Myers

© HASHICORP

4 of 26

Table of Contents/�Agenda

Terraform Testing Framework Intro & Basics

Terraform Testing Framework State

Terraform Testing “At-Scale” in Pipelines

Terraform Test Mocking

Review / Wrap-Up

4

©2024 HASHICORP

5 of 26

5

Terraform Testing Framework

© HASHICORP

6 of 26

Terraform Testing Framework Intro

6

HCL based configuration language to test Terraform Modules.

  • Integration Testing
    • Locally
    • CI/CD
  • Terraform CLI v1.6.0 or later
  • Module Validation
    • Variable Verification
    • Provider Verification
    • Capability Verification

©2024 HASHICORP

7 of 26

Terraform Testing Framework Folder Structure

7

NEW folder for tests

  • sub folders for setup or pre and post activities (optional)
  • *.tftest.hcl files for each test
  • *.tftest.json

©2024 HASHICORP

8 of 26

Terraform Testing Framework File Layout

8

One to Many RUN blocks

One to Many PROVIDER blocks - ALIAS are allowed

Plan or Apply COMMAND

VARIABLE block - global / plan specific

  • override module defaults

None to Many ASSERT blocks

©2024 HASHICORP

9 of 26

Terraform testing beyond the basics

9

TEST 1 - PLAN -

TEST 2 - PLAN

TEST 3 - APPLY

©2024 HASHICORP

10 of 26

10

Demo Time

Less Talk - More Code!

storage-account

© HASHICORP

11 of 26

TF Test Apply Strategy - All About the State

11

module. setup_rg

state

create_sa_stnd &

create_sa_change_tag

state

update in place

©2024 HASHICORP

12 of 26

Congratulations

Testing Framework Associate Badge

Note: It’s not a real thing!

©2024 HASHICORP

13 of 26

13

Terraform Testing Beyond Basics

©2024 HASHICORP

14 of 26

Module Lifecycle

Initial Release

Basic deployment with required parameters.

Add Functionality / Capabilities

Enable complex configurations

Provider Upgrade

Breaking Changes

14

Test ALL Versions

©2024 HASHICORP

15 of 26

Testing Multi-Version Modules

Tag Based vs Branch Based

HCP Terraform Cloud

  • HCP Modules - BRANCH based
  • MUST enable testing capability via HCP Portal when module is published
  • Can test lastest version of module.
  • Tests run when new commits are made to MAIN branch.
    • Can be triggered via terraform cli
    • terraform test -cloud-run=app.terraform.io/hashiconf24/storage-account/azurerm

CI/CD via Pipelines

  • HCP Modules - version TAG based
  • Leverage terraform cli test commands
  • Can test 1 to Many versions of HCP registry module by referencing TAG’s
  • Set to Test on CRON schedule

15

© HASHICORP

16 of 26

16

Multi-Version

Module Testing

virtual machines

© HASHICORP

17 of 26

17

Mocking Time

Let’s all pretend for a minute!

virtual machines

© HASHICORP

18 of 26

Mocking

18

Load Balancer Terraform Module

Terraform will NOT build the resource, but return data about the resource AS-IF it had created the resource.

Very useful for resources that require extended deployment time.

  • Virtual Machines
  • Databases
  • Kubernetes
  • Machine Learning

© HASHICORP

19 of 26

19

Quick Re-Cap

https://events.hashicorp.com/hashitalks2025

https://events.hashicorp.com/hashitalks2025

© HASHICORP

20 of 26

Types of Terraform Tests

20

20

  • Produces in Memory State files
  • Will NOT catch ALL provider API errors
  • Variable Validation - Positive / Negative
  • Fast
  • Test EVERYTHING & EVERY SCENARIO
  • Produces in Memory State files
  • Provider API Validation
  • Long Running - Resource Dependent
  • Mocking - Reduces Run time
  • Test High Priority Scenarios

©2024 HASHICORP

21 of 26

How about you?

What big lesson have you learned?

Share your knowledge in the chat!

Pro Tips / Recommendations

Test File Naming & Strategy

  • Executes Alphabetically!
  • Use numbers to order.
  • Use RANDOM everywhere!
  • How many tests per file?
    • Combine the plans
    • Separate the applys

Mocking

  • Returns VALID resource data
  • Fake it… so you don’t have to make it!
  • Cuts APPLY test times down

21

© HASHICORP

22 of 26

Additional Learning

Mattias Fjellstrom - fellow Ambassador

A Comprehensive Guide to Testing in Terraform

YouTube - Highly Recommend!

Terraform Test Mocking - Learn with GVR

CI/CD Pipelines Examples

AzDO - CI/CD Terraform Tests

22

© HASHICORP

23 of 26

hugs@hashicorp.com | learn.hashicorp.com | discuss.hashicorp.com

Thank you

LinkedIn: jeremy-j-myers

Jeremy

Myers

24 of 26

hello@hashicorp.com

Thank you

© HASHICORP

25 of 26

Thank you

© HASHICORP

26 of 26

Thank you

© HASHICORP