bats-testing
БесплатноБез исполняемых скриптовНе проверенUse whenever the code under test is a shell script, a CLI binary or anything run from Bash: argument parsing, flags, exit codes and output. Sets up Bats-core…
от testland★ 18 звёзд на GitHubОб этом скилле
bats-testing
Overview
Per bats-readthedocs:
"Bats (Bash Automated Testing System) is a TAP-compliant testing framework for Bash 3.2 or above."
Per bats-github:
"Bats is a TAP-compliant testing framework for Bash 3.2 or above. It provides a simple way to verify that the UNIX programs you write behave as expected."
Bats works "with any Unix program," not just Bash - it captures stdout / stderr / exit code of any executable.
When to use
- Testing a shell script (deployment scripts, CI scripts, installers).
- Testing a CLI binary's interface (commands, flags, exit codes, output) at the integration layer.
- Validating that wrapper scripts (e.g.,
kubectlplugins,gitaliases) behave as expected. - Cross-platform smoke tests for a CLI's install scripts.
How to use
- Install
bats(npm / Homebrew / git submodule); pin via submodule for CI determinism (Step 1). - Create
test/<feature>.batsstarting with#!/usr/bin/env batsand one@testblock per behavior (Step 2). - Wrap each command under test with
run, then assert on$status,$output, and$lines[](Step 3). - Add
setup/teardownwithmktemp -dso each test is isolated and parallel-safe (Step 4). loadshared helpers plus bats-support / bats-assert for diff-style assertions (Steps 5-6).- Guard environment-specific tests with
skip; run the suite viabats test/, adding--jobs Nfor parallelism (Steps 7-8). - Wire it into CI with TAP or JUnit output (references/ci-integration.md).
Step 1 - Install
Per bgh:
# npm
npm install -g bats
# Homebrew
brew install bats-core
# Git submodule (vendored, deterministic for CI)
git submodule add https://github.com/bats-core/bats-core.git test/bats
git submodule add https://github.com/bats-core/bats-support.git test/test_helper/bats-support
git submodule add https://github.com/bats-core/bats-assert.git test/test_helper/bats-assert
The git-submodule approach pins the Bats version per repo - recommended for CI determinism.
Step 2 - First test file
Per bgh (verbatim example):
#!/usr/bin/env bats
@test "addition using bc" {
result="$(echo 2+2 | bc)"
[ "$result" -eq 4 ]
}
@test "addition using dc" {
result="$(echo 2 2+p | dc)"
[ "$result" -eq 4 ]
}
Save as test/math.bats; run bats test/math.bats.
Step 3 - run for exit-code + output capture
The run helper invokes a command and captures $status,
$output, and $lines[]:
@test "myscript --help exits 0 and mentions usage" {
run ./myscript --help
[ "$status" -eq 0 ]
"$output" == *"Usage:"*
}
@test "myscript with no args exits 1" {
run ./myscript
[ "$status" -eq 1 ]
[ "$output" = "error: missing required argument" ]
}
@test "myscript output line count" {
run ./myscript list
[ "$status" -eq 0 ]
[ "${#lines[@]}" -eq 3 ]
[ "${lines[0]}" = "alice" ]
}
Without run, a non-zero exit code aborts the test before
assertions execute.
Step 4 - setup / teardown
Per bats: "setup and teardown: Pre- and post-test hooks."
setup() {
TEST_DIR="$(mktemp -d)"
cp fixtures/sample.txt "$TEST_DIR/"
}
teardown() {
rm -rf "$TEST_DIR"
}
@test "myscript processes the sample file" {
run ./myscript "$TEST_DIR/sample.txt"
[ "$status" -eq 0 ]
}
setup_file / teardown_file run once per file (vs once per
test). Use for expensive setup (Docker container start,
database init).
Step 5 - load shared helpers
Per bats: "load: Share common code."
# test/test_helper.bash
make_temp_repo() {
local d
d="$(mktemp -d)"
(cd "$d" && git init -q)
echo "$d"
}
# test/integration.bats
load test_helper
@test "git status in fresh repo is clean" {
local d
d="$(make_temp_repo)"
cd "$d"
run git status --porcelain
[ "$status" -eq 0 ]
[ -z "$output" ]
}
Step 6 - bats-assert / bats-support
Better assertions than raw [ ]:
load test_helper/bats-support/load
load test_helper/bats-assert/load
@test "myscript produces expected JSON" {
run ./myscript --json
assert_success
assert_output --partial '"version":'
refute_output --partial 'ERROR'
}
@test "myscript fails on bad input" {
run ./myscript --bad-flag
assert_failure 2
assert_line --index 0 "Unknown flag: --bad-flag"
}
assert_success / assert_failure / assert_output /
assert_line give clear diff-style failure messages (vs [ ]'s
silent fail).
Step 7 - skip
Per bats: "skip: Easily skip tests."
@test "macOS-only behavior" {
if "$(uname -s)" != "Darwin"; then
skip "macOS-only test"
fi
run ./myscript --use-keychain
[ "$status" -eq 0 ]
}
@test "needs docker daemon" {
if ! command -v docker >/dev/null; then
skip "docker not installed"
fi
if ! docker info >/dev/null
Установить bats-testing в Claude Code и Claude Desktop
Зарегайся, чтобы установить скилл
Создай бесплатный аккаунт, чтобы открыть команду установки и сохранить скилл в библиотеку.
- Открой команду установки в одну строку
- Сохраняй скиллы в синхронизируемую библиотеку
- Уведомления, когда скиллы обновляются
Разрешённые инструменты
Инструменты, которые скиллу разрешено вызывать.
Без ограничений — скилл может использовать любой инструмент.
Вложенные файлы
FAQ
Что делает скилл bats-testing?
Use whenever the code under test is a shell script, a CLI binary or anything run from Bash: argument parsing, flags, exit codes and output. Sets up Bats-core with @test blocks, run, status and output assertions, setup/teardown, shared helpers and TAP output for CI.
Как установить скилл bats-testing?
Скопируй папку скилла в ~/.claude/skills (вкладка Claude Code выше делает это одной командой), либо поставь как плагин.
Скилл bats-testing запускает скрипты?
Нет, скилл состоит только из инструкций (SKILL.md), без исполняемых скриптов.
Похожие скиллы
mcp-builder
Guide for creating high-quality MCP (Model Context Protocol) servers that enable LLMs to interact with external services through well-designed tools. Use when…
от Anthropicclaude-api
Reference for the Claude API / Anthropic SDK — model ids, pricing, params, streaming, tool use, MCP, agents, caching, token counting, model migration. TRIGGER…
от Anthropicalgorithmic-art
Creating algorithmic art using p5.js with seeded randomness and interactive parameter exploration. Use this when users request creating art using code…
от Anthropicmcp-integration
This skill should be used when the user asks to "add MCP server", "integrate MCP", "configure MCP in plugin", "use .mcp.json", "set up Model Context…
от Anthropic