bash-testing
Bash Testing (BATS)
Test shell scripts reliably with the Bash Automated Testing System.
Run tests
# Run a test file
bats test/my_script.bats
# Run all tests in directory
bats test/
# Verbose output
bats --verbose-run test/
# TAP format output (for CI)
bats --formatter tap test/
# Pretty output
bats --formatter pretty test/
Write a test
# test/example.bats
#!/usr/bin/env bats
setup() {
# Runs before each test
source ./my_script.sh
}
teardown() {
# Runs after each test
rm -f /tmp/test_output
}
@test "function returns expected output" {
result=$(my_function "input")
[ "$result" = "expected output" ]
}
@test "script exits with code 0 on success" {
run ./my_script.sh --valid-arg
[ "$status" -eq 0 ]
}
@test "script exits with code 1 on bad input" {
run ./my_script.sh --invalid
[ "$status" -eq 1 ]
[[ "$output" =~ "Error" ]]
}
@test "output contains expected string" {
run ./my_script.sh
[ "$status" -eq 0 ]
[[ "$output" =~ "Success" ]]
}
@test "file is created" {
run ./my_script.sh --create /tmp/test_output
[ "$status" -eq 0 ]
[ -f /tmp/test_output ]
}
Key assertions
# Exit status
[ "$status" -eq 0 ]
# Exact output match
[ "$output" = "expected" ]
# Regex match
[[ "$output" =~ "pattern" ]]
# Line-specific output
[ "${lines[0]}" = "first line" ]
[ "${lines[1]}" = "second line" ]
# File exists
[ -f "$filepath" ]
# String not empty
[ -n "$result" ]
# Numeric comparison
[ "$count" -gt 5 ]
Using helper libraries
# With bats-support and bats-assert (install separately)
setup() {
load 'test_helper/bats-support/load'
load 'test_helper/bats-assert/load'
}
@test "readable assertions" {
run ./my_script.sh
assert_success
assert_output --partial "expected text"
refute_output --partial "error"
assert_line --index 0 "first line"
}
Notes
- Install helpers:
git clone https://github.com/bats-core/bats-support test/test_helper/bats-supportandgit clone https://github.com/bats-core/bats-assert test/test_helper/bats-assert. - Use
setup_file()/teardown_file()for expensive one-time setup. - The
runkeyword captures both stdout and exit status into$outputand$status. - Tests run in subshells — variable changes don't leak between tests.
More from thinkfleetai/thinkfleet-engine
local-whisper
Local speech-to-text using OpenAI Whisper. Runs fully offline after model download. High quality transcription with multiple model sizes.
148flyio-cli-public
Use the Fly.io flyctl CLI for deploying and operating apps on Fly.io: deploys (local or remote builder), viewing status/logs, SSH/console, secrets/config, scaling, machines, volumes, and Fly Postgres (create/attach/manage databases). Use when asked to deploy to Fly.io, debug fly deploy/build/runtime failures, set up GitHub Actions deploys/previews, or safely manage Fly apps and Postgres.
24kagi-search
Web search using Kagi Search API. Use when you need to search the web for current information, facts, or references. Requires KAGI_API_KEY in the environment.
22feishu-bridge
Connect a Feishu (Lark) bot to ThinkFleet via WebSocket long-connection. No public server, domain, or ngrok required. Use when setting up Feishu/Lark as a messaging channel, troubleshooting the Feishu bridge, or managing the bridge service (start/stop/logs). Covers bot creation on Feishu Open Platform, credential setup, bridge startup, macOS launchd auto-restart, and group chat behavior tuning.
13bambu-local
Control Bambu Lab 3D printers locally via MQTT (no cloud). Supports A1, A1 Mini, P1P, P1S, X1C.
10voice-transcribe
Transcribe audio files using OpenAI's gpt-4o-mini-transcribe model with vocabulary hints and text replacements. Requires uv (https://docs.astral.sh/uv/).
10