home / skills / zenobi-us / dotfiles / bats-testing

This skill helps you write robust shell-native end-to-end tests for CLI tools, Bash libraries, and REST APIs using Bats.

npx playbooks add skill zenobi-us/dotfiles --skill bats-testing

Review the files below or copy the command above to add this skill to your agents.

Files (2)
SKILL.md
4.9 KB
---
name: bats-testing
description: Use when writing shell-native tests for CLI tools, sourced Bash libraries, or REST APIs, especially when process boundaries, shell state, and exit/output behavior must be verified end-to-end.
---

# Testing with Bats

## Overview

Bats is best when correctness depends on real shell behavior: exit codes, stdout/stderr, sourced functions, and external commands.

Core principle: **test behavior at the shell boundary, not implementation details.**

## When to Use

Use this skill when you need to:

- Write e2e tests for CLI tools
- Test Bash libraries that are `source`d (not executed)
- Use Bats as a shell-native REST API test runner with `curl` + `jq`

Typical symptoms:

- "My script works manually but fails in CI"
- "I can test command output, but not sourced function behavior"
- "I need lightweight API tests from shell pipelines"

## Project Setup

Recommended layout:

```text
test/
  helpers/
    test_helper.bash
  cli_*.bats
  lib_*.bats
  api_*.bats
```

`test/helpers/test_helper.bash`:

```bash
#!/usr/bin/env bash

load 'test_helper/bats-support/load'
load 'test_helper/bats-assert/load'

setup_test_tmp() {
  export TEST_TMPDIR="$(mktemp -d)"
}

teardown_test_tmp() {
  rm -rf "$TEST_TMPDIR"
}
```

## Pattern 1: CLI e2e Tests

Test real invocation and output contracts.

```bash
#!/usr/bin/env bats

load './helpers/test_helper.bash'

setup() {
  setup_test_tmp
  export HOME="$TEST_TMPDIR/home"
  mkdir -p "$HOME"
}

teardown() {
  teardown_test_tmp
}

@test "todoctl add persists item" {
  run todoctl add "buy milk"
  assert_success
  assert_output --partial "added"

  run todoctl list --json
  assert_success
  echo "$output" | jq -e 'any(.[]; .text == "buy milk")'
}

@test "todoctl add rejects empty text" {
  run todoctl add ""
  assert_failure
  assert_output --partial "text is required"
}
```

### Notes
- Always isolate runtime directories (`HOME`, config/data dirs).
- Assert both exit status and output.
- Include at least one negative-path test per command surface.

## Pattern 2: Sourced Bash Libraries

For libs, distinguish:

1. **Process-level assertions** (`run bash -c 'source ...'`)
2. **In-shell state assertions** (direct call, no `run`)

`run` executes in a subshell. Side effects on variables do **not** persist to the test shell.

```bash
#!/usr/bin/env bats

load './helpers/test_helper.bash'

setup() {
  # shellcheck disable=SC1091
  source "${BATS_TEST_DIRNAME}/../lib/string_utils.sh"
}

@test "library can be sourced cleanly" {
  run bash -c 'source "./lib/string_utils.sh"'
  assert_success
  assert_output ""
}

@test "trim returns normalized value" {
  run trim "  hello  "
  assert_success
  assert_output "hello"
}

@test "function can mutate caller state (non-run path)" {
  value="  hello world  "
  trim_in_place value   # this function edits variable by name
  [ "$value" = "hello world" ]
}
```

### Notes
- Use `run` for output/status checks.
- Use direct invocation for in-shell state mutation tests.
- Source once in `setup` unless isolation requires per-test sourcing.

## Pattern 3: REST API Testing with Bats

Use helpers so each test focuses on intent.

```bash
#!/usr/bin/env bats

load './helpers/test_helper.bash'

request_json() {
  local method="$1"; shift
  local url="$1"; shift
  local body_file="$BATS_TEST_TMPDIR/response.json"

  HTTP_STATUS="$({
    curl -sS \
      -X "$method" \
      -H 'Accept: application/json' \
      -H 'Content-Type: application/json' \
      -o "$body_file" \
      -w '%{http_code}' \
      "$url" "$@"
  })"

  HTTP_BODY="$(cat "$body_file")"
}

@test "GET /health is healthy" {
  [ -n "${API_BASE_URL:-}" ] || skip "API_BASE_URL is required"

  request_json GET "${API_BASE_URL%/}/health"
  [ "$HTTP_STATUS" -eq 200 ]
  echo "$HTTP_BODY" | jq -e '.status | IN("ok", "healthy", "up")'
}

@test "POST /users creates user" {
  [ -n "${API_BASE_URL:-}" ] || skip "API_BASE_URL is required"

  local email="[email protected]"
  request_json POST "${API_BASE_URL%/}/users" \
    --data "$(jq -nc --arg email "$email" '{name:"Bats User", email:$email}')"

  [ "$HTTP_STATUS" -eq 201 ]
  echo "$HTTP_BODY" | jq -e --arg email "$email" '.email == $email and .id != null'
}
```

### Notes
- Prefer `jq` over regex for JSON assertions.
- Generate unique test data to avoid collisions.
- For stateful APIs, add explicit cleanup calls or disposable environments.

## Common Mistakes

- **Parsing JSON with `grep` only** → brittle checks; use `jq -e`.
- **Only happy-path tests** → add negative-path assertions for each command/endpoint.
- **Using `run` for stateful sourced-function tests** → side effects disappear (subshell).
- **Leaking local machine state** (`HOME`, config dirs) → isolate with temp dirs.

## Quick Checklist

Before claiming tests are done:

- [ ] Exit code and output are both asserted
- [ ] At least one failure-path test exists
- [ ] Sourced-library tests include non-`run` state checks when relevant
- [ ] API JSON assertions use `jq`
- [ ] Test state is isolated and reproducible

Overview

This skill teaches how to write shell-native tests using Bats to verify real shell behavior: exit codes, stdout/stderr, sourced functions, and interactions with external commands or REST endpoints. It focuses on testing at the shell boundary so tests validate observable behavior rather than internal implementation. Practical patterns cover CLI end-to-end tests, sourced Bash library checks, and lightweight API tests driven from shell pipelines.

How this skill works

The skill provides patterns and helper conventions to run commands under test, capture exit status and output using Bats' run helper, and isolate runtime state with temporary directories. For sourced libraries, it shows when to invoke functions directly (to test in-shell mutation) versus using run (to test process-level exit/output). For REST APIs, it demonstrates a request_json helper that captures HTTP status and body and uses jq for reliable JSON assertions.

When to use it

  • Writing end-to-end tests for command-line tools where exit codes and output matter
  • Testing Bash libraries that are sourced and may mutate caller state
  • Running shell-native REST API checks using curl + jq
  • When CI failures differ from local runs and you need real shell boundaries
  • When you need lightweight, reproducible tests that exercise the real runtime environment

Best practices

  • Isolate test state: set HOME and data/config dirs to temp locations per test
  • Assert both exit status and output for every command under test
  • Include at least one negative-path test (error handling) for each surface
  • Use jq for JSON assertions instead of regex or grep
  • For sourced libraries, use run for subprocess assertions and direct calls for in-shell state mutations

Example use cases

  • Test a todo CLI end-to-end: add, list, and verify persisted JSON results
  • Verify a sourced string utilities library both for return output and for functions that mutate caller variables
  • Run health and create-user HTTP tests against an API using curl; assert HTTP status and JSON body with jq
  • Ensure scripts behave the same in CI by isolating HOME and other environment paths in tests

FAQ

When should I source a library in setup versus inside each test?

Source once in setup if functions are pure and tests don't conflict. Source per-test when you need isolation or tests change global state that could affect others.

Why prefer jq over grep for JSON?

jq parses JSON structurally and fails on invalid JSON; grep is brittle and may pass or fail incorrectly when formatting changes.