Cette page est affichée en anglais. Une traduction française est en cours.
Apr 16, 2026·1 min de lecture

Terratest — Automated Testing for Infrastructure Code

Terratest is a Go library that makes it easy to write automated tests for your Terraform, Packer, Kubernetes, and Docker infrastructure.

Anonymous
Anonymous · Community
Prêt pour agents

Installation agent prête

Cet actif peut être installé après choix du runtime, vérification du plan et exécution de la commande adaptée.

Native · 98/100Policy : autoriser
Surface agent
Tout agent MCP/CLI
Type
Skill
Installation
Single
Confiance
Confiance : Community
Point d'entrée
Terratest Overview
Commande d'installation directe
npx -y tokrepo@latest install c79522cf-3997-11f1-9bc6-00163e2b0d79 --target codex

À exécuter après confirmation du plan en dry-run.

TL;DR
Terratest is a Go testing library that validates Terraform, Packer, Kubernetes, and Docker infrastructure with real deployments.
§01

What it is

Terratest is a Go library for writing automated tests for infrastructure code. It deploys real infrastructure (Terraform modules, Kubernetes manifests, Docker images, Packer templates), validates that it works correctly, and then tears it down. Unlike linting or static analysis, Terratest tests actual behavior.

Terratest targets infrastructure engineers and DevOps teams who want confidence that their Terraform modules, Helm charts, and Kubernetes configurations actually work before merging to main.

§02

How it saves time or tokens

Terratest catches infrastructure bugs before they reach production. A broken Terraform module discovered in CI costs minutes. The same bug discovered in production costs hours of debugging and potential downtime. By testing real infrastructure in an isolated environment, you verify networking, IAM permissions, and resource configuration end-to-end.

Terratest also provides helper functions for common assertions (HTTP requests, SSH commands, Kubernetes queries) that would take significant boilerplate to write from scratch.

§03

How to use

  1. Create a Go test file in your Terraform module directory:
go mod init test
go get github.com/gruntwork-io/terratest/modules/terraform
  1. Write a test that deploys and validates infrastructure:
package test

import (
    "testing"
    "github.com/gruntwork-io/terratest/modules/terraform"
    "github.com/stretchr/testify/assert"
)

func TestWebServer(t *testing.T) {
    opts := &terraform.Options{
        TerraformDir: "../modules/web-server",
        Vars: map[string]interface{}{
            "instance_type": "t3.micro",
        },
    }
    defer terraform.Destroy(t, opts)
    terraform.InitAndApply(t, opts)

    url := terraform.Output(t, opts, "url")
    assert.Contains(t, url, "http")
}
  1. Run the test:
go test -v -timeout 30m
§04

Example

Testing a Kubernetes deployment with Terratest:

func TestK8sDeployment(t *testing.T) {
    kubeOpts := k8s.NewKubectlOptions("", "", "test-ns")
    k8s.CreateNamespace(t, kubeOpts, "test-ns")
    defer k8s.DeleteNamespace(t, kubeOpts, "test-ns")

    k8s.KubectlApply(t, kubeOpts, "../manifests/deployment.yaml")
    k8s.WaitUntilDeploymentAvailable(t, kubeOpts, "my-app", 30, 10*time.Second)

    service := k8s.GetService(t, kubeOpts, "my-app")
    assert.Equal(t, int32(80), service.Spec.Ports[0].Port)
}
§05

Related on TokRepo

§06

Common pitfalls

  • Not setting timeouts. Infrastructure provisioning takes minutes, not seconds. Use go test -timeout 30m and set appropriate retry intervals in Terratest helper functions.
  • Testing in shared environments. Always use isolated accounts, namespaces, or resource prefixes to prevent test infrastructure from colliding with production or other test runs.
  • Forgetting the defer terraform.Destroy() call. Without it, failed tests leave orphaned cloud resources that accumulate costs.

Questions fréquentes

Does Terratest deploy real infrastructure?+

Yes. Terratest runs actual Terraform apply, creates real cloud resources, validates them, and then destroys them. This is the key difference from tools like terraform validate or tflint, which only check syntax and configuration without deploying.

How long do Terratest tests take to run?+

Terratest tests are slow compared to unit tests because they provision real infrastructure. A simple EC2 instance test takes 3-5 minutes. Complex tests with databases or Kubernetes clusters can take 15-30 minutes. Run them in CI pipelines, not on every commit.

Can Terratest test Kubernetes configurations?+

Yes. Terratest has a dedicated k8s module for deploying manifests, waiting for pods, querying services, and making HTTP requests to Kubernetes workloads. It works with any Kubernetes cluster (EKS, GKE, kind, minikube).

Is Terratest only for Go developers?+

Terratest is written in Go and tests are written as Go test functions. However, you do not need to be a Go expert. The API is straightforward and most tests follow a simple pattern: deploy, validate, destroy. Basic Go knowledge is sufficient.

How does Terratest compare to Kitchen-Terraform?+

Kitchen-Terraform uses Ruby and the Test Kitchen framework. Terratest uses Go and the standard Go testing library. Terratest is generally preferred in Go-heavy organizations and has a larger community and more helper modules.

Sources citées (3)

Fil de discussion

Connectez-vous pour rejoindre la discussion.
Aucun commentaire pour l'instant. Soyez le premier à partager votre avis.

Actifs similaires