LogoLogo
Bonsai (0.13) DocsGitHubDiscord CommunityGarden Enterprise
Acorn (0.12)
Acorn (0.12)
  • Welcome!
  • 🌳Basics
    • How Garden Works
    • Quickstart Guide
    • The Stack Graph (Terminology)
  • 🌻Tutorials
    • Your First Project
      • 1. Initialize a Project
      • 2. Connect to a Cluster
      • 3. Deploy and Test
      • 4. Configure Your Project
  • 💐Using Garden
    • Configuration Overview
    • Projects
    • Modules
    • Services
    • Tests
    • Tasks
    • Workflows
    • Variables and templating
    • Module Templates
    • Using the CLI
  • 🌿Kubernetes Plugins
    • About
    • Remote K8s Plugin Configuration
      • 1. Create a Cluster
        • AWS
        • GCP
        • Azure
      • 2. Configure Container Registry (Optional)
        • AWS
        • GCP
        • Azure
      • 3. Set Up Ingress, TLS and DNS
      • 4. Configure the Provider
    • Local K8s Plugin Configuration
      • 1. Install Local Kubernetes
      • 2. Configure the Provider
    • Module Configuration
      • Container
      • Kubernetes
      • Helm
      • PersistentVolumeClaim
      • ConfigMap
    • Advanced
      • In-Cluster Building
      • Minimal RBAC Configuration for Development Clusters
      • Deploying to Production
  • 🌺Terraform Plugin
    • About
    • Provider Configuration
    • Module Configuration
  • ☘️Pulumi Plugin
    • About
    • Provider Configuration
    • Module Configuration
  • 🌹Other Plugins
    • Container
    • Exec (local scripts)
  • 🌼Guides
    • Installing Garden
    • Adopting Garden
    • Code Synchronization (Dev Mode)
    • Connecting a local service to a K8s cluster (Local Mode)
    • Environments and namespaces
    • Hot Reload
    • Migrating from Docker Compose to Garden
    • Using Garden in CI
  • 🌷Advanced
    • cert-manager Integration
    • Using Remote Sources
    • Custom Commands
  • 🪷Reference
    • Providers
      • conftest-container
      • conftest-kubernetes
      • conftest
      • container
      • exec
      • hadolint
      • jib
      • kubernetes
      • local-kubernetes
      • maven-container
      • octant
      • openfaas
      • pulumi
      • terraform
    • Module Types
      • configmap
      • conftest
      • container
      • exec
      • hadolint
      • helm
      • jib-container
      • kubernetes
      • maven-container
      • openfaas
      • persistentvolumeclaim
      • pulumi
      • templated
      • terraform
    • Template Strings
      • Project configuration context
      • Environment configuration context
      • Provider configuration context
      • Module configuration context
      • Remote Source configuration context
      • Project Output configuration context
      • Custom Command configuration context
      • Workflow configuration context
      • Template Helper Functions
    • Glossary
    • Commands
    • Project Configuration
    • Module Template Configuration
    • Workflow Configuration
  • 🎋Misc
    • FAQ
    • Troubleshooting
    • Telemetry
Powered by GitBook
On this page
  • How it Works
  • Tests in the Stack Graph
  • Examples
  • Integration Testing
  • Advanced
  • Test Artifacts
  • Disabling Tests
  • Kubernetes Provider
  • Exec Modules
  • Kubernetes and Helm Modules
  • Next Steps

Was this helpful?

  1. Using Garden

Tests

PreviousServicesNextTasks

Last updated 1 year ago

Was this helpful?

You add tests when you want Garden to run your test suites for you. A simple configuration looks like this:

# garden.yml
kind: Module
tests:
  - name: unit
    args: [npm, run, test:unit]
  - name: integ
    args: [npm, run, test:integ]
    dependencies:
      - backend

Note that not all support tests.

How it Works

Tests belong to modules and each module can have several tests. Because tests are a part of the Stack Graph and dependency aware, you can easily run integration tests that require other parts of your stack to be running.

Garden caches test results and only re-runs the test if the module the test belongs to, or upstream dependents, have changed. For remote environments, the test results are stored at the cluster level so that the entire team can share the cached results.

You use the command and args directives to specify how the test is run. If the execution exits with 0, the test is considered to have passed, otherwise failed.

If you have expensive tests that you don't want to run on every watch event when in watch mode, you can use the --skip-tests flag or, alternatively, specify what tests to run with the --test-names flag.

You can run a test manually with the garden run test <test-name> command. This will run the test regardless of whether or not the result is cached.

You can view test results from the dashboard or by running garden get test-result <module-name> <test-name>.

Tests in the Stack Graph

Tests correspond to a test action in the Stack Graph.

  • Tests implicitly depend on the build step of their parent module.

  • Tests can depend on services and tasks.

  • Currently, nothing else can depend on tests.

Examples

Integration Testing

Here's the configuration for frontend module:

# garden.yml
kind: Module
type: container
name: frontend
tests:
  - name: unit
    args: [npm, run, test:unit]
  - name: integ
    args: [npm, run, test:integ]
    timeout: 60
    dependencies:
      - backend

The integ test looks like this:

// tests/integ/test.js
describe('POST /vote', () => {
  it('respond with message from hello-function', async () => {
    const result = await axios.post('<http://backend/vote/>', `vote=a`);
    expect(result.status).to.eql(200);
  });
});

Now when you're in watch mode and make a change to the frontend, Garden will re-run both the unit and integ tests for you.

If you make a change to the backend backend module, Garden will first re-build and re-deploy the backend, and then run the integ test defined for the frontend.

Advanced

Test Artifacts

Many module types, including container, exec and helm, allow you to extract artifacts after tests have been run. This can be handy when you'd like to view reports or logs, or if you'd like a script (via a local exec module, for instance) to validate the output from a test.

By convention, artifacts you'd like to copy can be specified using the artifacts field on test configurations. For example, for the container module, you can do something like this:

kind: Module
type: container
name: my-container
...
tests:
  - name: my-test
    command: [some, command]
    artifacts:
      - source: /report/*
        target: my-test-report

After running my-test, you can find the contents of the report directory in the test's container, locally under .garden/artifacts/my-test-report.

Disabling Tests

Module types that allow you to configure tests generally also allow you to disable tests by setting disabled: true in the test configuration. You can also disable them conditionally using template strings. For example, to disable a container module test for a specific environment, you could do something like this:

kind: Module
type: container
...
tests:
  - name: e2e
    disabled: ${environment.name == "prod"}
    ...

Tests are also implicitly disabled when the parent module is disabled.

Kubernetes Provider

Tests are executed in their own Pod inside the project namespace. The Pod is removed once the test has finished running.

To clear cached test results, you currently have to delete the ConfigMaps manually with kubectl. Here's an example:

kubectl delete -n garden-system--metadata $(kubectl get configmap -n garden-system--metadata -o name | grep test-result)

Exec Modules

The exec module type runs tests locally in your shell. By default, the exec module type executes tests in the Garden build directory (under .garden/build/<module-name>). By setting local: true, the tests are executed in the module source directory instead.

Kubernetes and Helm Modules

Next Steps

For full test configuration by module type, please take a look at our .

Below is an example of a frontend module that has a unit test and an integ test that depends on a backend module. The integ test checks whether the frontend gets the correct response from the backend. The example is based on our .

Please look at individual to see how to configure each module type's tests to extract artifacts after running them.

Tests results are stored as in the garden-system--metadata namespace with the format test-result--<hash> and shared across the team.

Because a Kubernetes or Helm module can contain any number of Kubernetes resources, a serviceResource needs to be specified to determine the pod spec for the test pod. You can see the whole pod spec used in the reference docs for and . Please note that the startupProbe, livenessProbe and readinessProbe are stripped from your pod spec. Health checks for your application might fail when the container is used for testing because the main process usually running in that container is replaced by the test command.

In the , we'll see how Garden can execute tasks for your. For example populating a database after it has been deployed.

💐
modules types
reference docs
vote example project
module type references
ConfigMaps
kubernetes
helm modules
next section