Esta página se muestra en inglés. Una traducción al español está en curso.
SkillsApr 16, 2026·3 min de lectura

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.

Listo para agents

Instalación lista para agent

Este activo puede instalarse después de elegir el runtime, revisar el plan y ejecutar el comando correspondiente.

Native · 98/100Política: permitir
Superficie agent
Cualquier agent MCP/CLI
Tipo
Skill
Instalación
Single
Confianza
Confianza: Established
Entrada
Terratest Overview
Comando de instalación directa
npx -y tokrepo@latest install c79522cf-3997-11f1-9bc6-00163e2b0d79 --target codex

Ejecutar después de confirmar el plan con 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.

Preguntas frecuentes

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.

Referencias (3)

Discusión

Inicia sesión para unirte a la discusión.
Aún no hay comentarios. Sé el primero en compartir tus ideas.

Activos relacionados