bats
Automated testing framework for bash scripts
TLDR
Run test file
SYNOPSIS
bats [options] test-file...
DESCRIPTION
bats (Bash Automated Testing System) is a TAP-compliant testing framework for Bash scripts. It provides a simple syntax for writing tests and executing them with readable output.
The tool enables test-driven development and continuous integration for shell scripts.
PARAMETERS
-c, --count
Count tests without running-f, --filter pattern
Filter tests by name pattern-t, --tap
Output in TAP format-p, --pretty
Pretty print output-r, --recursive
Run tests recursively-j, --jobs num
Number of parallel jobs--formatter name
Output formatter (tap, junit, pretty)
TEST SYNTAX
@test "addition works" {
result="$(echo 2+2 | bc)"
[ "$result" -eq 4 ]
}
@test "files exist" {
[ -f "/etc/hosts" ]
}
ASSERTIONS
Uses standard bash test operators:
- [ -f file ] - File exists
- [ "$a" = "$b" ] - String equality
- [ $n -eq 4 ] - Numeric equality
HELPER FUNCTIONS
run command
Run command and capture output/statusload file
Load helper libraryskip [reason]
Skip current test
CAVEATS
Tests run in subshells (limited side effects). No built-in assertion library (use test-helper). Parallel execution may have ordering issues. Debugging can be tricky.
HISTORY
bats was created by Sam Stephenson in 2011 to provide a simple testing framework for bash scripts, inspired by similar tools in other languages.
SEE ALSO
bash(1), shellcheck(1), shunit2(1)
