NashTech Insights

Terratest: How to implement it on Azure.

Mohd Muzakkir Saifi
Mohd Muzakkir Saifi
Table of Contents
codes on tilt shift lens

Hello Learners, I am back with another blog. In today’s blog, we will learn about the terratest. Terratest is an essential tool for testing and provides an approach to infrastructure testing. In this blog, we will explore what Terratest is and how to implement it on the Azure platform. By the end of this blog, you’ll have a solid understanding of Terratest’s capabilities and how we can use the terratest to test the Azure resources using Golang. If you like my blogs, you can read more blogs here.

Note:- I am working on the terratest blog series. So please follow my blog and learn about the terratest. You can check out this link for more blogs on the terratest.


Terratest is an open-source testing framework developed by Gruntwork, designed to automate infrastructure testing. It allows the teams to write automated tests for their infrastructure code, ensuring that everything works as we expected. With Terratest, you can test Terraform configurations, and infrastructure code, and also deploy resources to the cloud.

Why use Terratest with Azure?

There is no doubt that Azure is a leading cloud platform offering lots of services to build and manage applications and infrastructure. However, as infrastructure deployments increase which means complexity increases, testing becomes more important and critical to do. Also, we need to avoid costly mistakes with testing infrastructure. Terratest, when integrated with Azure, a resource of Azure’s terraform module then you can validate your infrastructure code, and configurations. Before deploying into production, we need to test the resource.

Implementation of Terratest:-

Let’s dive into the step-by-step process of setting up and implementing Terratest for testing Azure infrastructure.

  1. Prerequisites:
    • Install Terraform: Basically, Terratest uses Terraform for creating and managing infrastructure on Azure. Make sure you have Terraform installed on your local machine.
    • Azure Credentials: Create an Azure Service Principal and configure it with the required permissions.
  2. Setting up the Project:
    • Create a new project directory and organize your Terraform code within it.
    • Integrate Terratest into your project by importing the necessary Go packages.
  3. Writing Tests:
    • Define test cases using Go’s testing framework. These test cases will define the behavior you expect from your Azure infrastructure and you will compare it with the actual resource.
  4. Running Tests:
    • Run your Terratests using the go test -v command. Terratest will automatically set up and tear down your Azure resources during the test execution.
    • Analyze the test output and give you the result as failed or pass.

Benefits of Terratest with Azure:

  1. Reliability: Terratest allows you to validate the correctness of your infrastructure code before deployment on production.
  2. Reusability: Once you’ve written tests for your Azure infrastructure, you can reuse them for future projects for saving time and effort.
  3. Validation of Desired State: Terratest verifies that the actual state of Azure resources matches the desired state defined in your Terraform code. This validation ensures that infrastructure is provisioned correctly and reduces configuration drift.

Terratest code:-

We have understood the terratest. Now It is the time to do some hands-on on terratest. For today’s demo, I will use the sample example without test cases. We will see the flow and how we can integrate the terraform code with the terratest and deploy the resource with the terratest.

First of all, We should have a running terraform module. I am considering that you must have a running terraform module and please check that terraform module separately then we will use it in the terratest.


  • Golang
  • Azure credential

Export the credential:-

Credential plays a very important role with this you will not able to deply the resource. First of all, you need to export the credential in the system. Please replace the values in below and export:-

export ARM_CLIENT=""
export ARM_TENANT=""


I am considering that you have terraform module which is in the module folder and with the same module folder level. you must create a test folder and then create a main_test.go file.

Note:- Terratest code always store in this file naming convention *_test.go

You can paste the below code in this file:-

package main

// Import key modules for this code ...
import (

var (
	acrname = "testacr"

func setTerraformVariables() (map[string]string, error) {


	fmt.Println("##################################Environment Variables:#######################################")
	fmt.Println("****************************************Environment Variables END:***********************************")

func TestTerraform_azure_acr(t *testing.T) {

	terraformOptions := terraform.WithDefaultRetryableErrors(t, &terraform.Options{

		TerraformDir: "../../terraform/module",
		Vars: map[string]interface{}{
			"acrname": acrname,

	// terraform destroy
	defer terraform.Destroy(t, terraformOptions)

	// terraform init and apply
	terraform.InitAndApply(t, terraformOptions)
	// terraform.InitAndPlan(t, terraformOptions)


In the above code, first, we import a few packages as per the requirements:-

import (

Now we set up the global variable for the code and then we are fetching the env variable which we have exported locally and print them.

We have a very important function for terratest TestTerraform_azure_acr in this function we will execute our terraform code:

In this code, I am passing the terraform module path in the TerraformDir and we can also ingest the new define values in the terratest code this will be replaced in the terraform also as I mention the acrname in it. Now we are applying the terraform code and after that destroy it.

Run terratest:-

You need to run the below command to run the terratest:-

go mod init<name>
go mod tidy
go test -v

This is very powerful to test the resource. I am writing more blogs about the terratest please visit me regularly and learn something new always.


Terratest is a powerful tool that can test the infrastructure and integration with Azure. By adopting Terratest in your Azure projects, you can ensure your infrastructure code is reliable, secure, and scalable, giving you the confidence to deploy with ease. If you like my blog then you can like this or want to read more blogs then follow this link. and also check out the terratest official doc.

Mohd Muzakkir Saifi

Mohd Muzakkir Saifi

Leave a Comment

Your email address will not be published. Required fields are marked *

Suggested Article

%d bloggers like this: