
Who it's for
You maintain a library of Bash utilities or a CLI tool written in Bash and want a repeatable test suite that runs in CI. Bats lets you write `@test` blocks that call your scripts and assert on their output and exit codes.
You write Bash-based provisioning or deployment scripts and need to verify their behaviour in a sandboxed environment before they touch production. Bats' `setup` and `teardown` hooks let you create and clean up temporary state around each test.
If your infrastructure tooling is primarily in Python or Go rather than shell scripts, a language-native test framework is a better fit.
Your project ships a shell-based installer or CLI wrapper and you want CI to catch regressions. Bats integrates with GitHub Actions and other CI systems through its TAP output and standard exit codes.
The problem
Shell scripts lack a standard testing discipline: there is no built-in assertion library, no test runner, and no structured output format. Developers either skip tests entirely or write ad hoc scripts that are hard to maintain and give no structured pass/fail signal to CI systems.
How it works
Bats emits TAP (Test Anything Protocol) output, making it compatible with any TAP consumer including CI systems, test aggregators, and reporting tools.
The `run` helper executes a command and captures its stdout, stderr, and exit code into `$output` and `$status` without triggering `errexit`, so tests can assert on failure conditions without aborting.
`setup` and `teardown` functions run automatically before and after each test case. `setup_suite` and `teardown_suite` run once per file, letting you avoid costly repeated initialisation.
Bats supports running test cases in parallel, which can significantly reduce total test suite time for independent tests.
`load` shares common code within a project; `bats_load_library` loads system-wide helper libraries, enabling ecosystem libraries like bats-assert and bats-file.
Tests can be tagged and the `--filter` flag lets you run a subset of tests by name or tag, making it practical to target specific areas during development.
Features
- TAP-compliant output
- `run` helper
- Setup and teardown hooks
- Parallel execution
- Library loading
- Test tagging and filtering
How to install
# macOS via Homebrew brew install bats-core # npm (any OS) npm install -g bats # From source git clone https://github.com/bats-core/bats-core.git cd bats-core ./install.sh /usr/local
Recommended for this setup
Strengths
- Native Bash syntax: Test files are valid Bash scripts. There is no DSL to learn beyond the `@test` block syntax and a handful of helper functions, so anyone who writes shell scripts can read and write tests immediately.
- Tests any UNIX program: Although designed for Bash scripts, Bats can test any UNIX program by running it as a subprocess and asserting on its exit code and output, making it broadly useful in shell-centric environments.
- Active community maintenance: The original bats project was archived in 2021. Bats-core is the community-maintained fork that has continued to receive releases, documentation improvements, and new features since 2017.
- Multiple installation paths: Available through Linux distribution package managers, Homebrew on macOS, npm, and source installation via Git Bash on Windows, covering the main platforms where shell scripting is used.
Trade-offs
- Bash-only runtime: Bats requires Bash 3.2 or above and is not portable to other shells such as zsh, fish, or dash. Code that must run under a POSIX sh shell cannot be tested directly with Bats.
- Limited assertion library built in: Bats itself provides minimal assertion helpers. Richer matchers (like those in bats-assert) require installing and loading separate add-on libraries, adding setup overhead for new projects.
- File descriptor gotchas: Bats uses file descriptor 3 internally for test output. Code under test that closes or redirects FD 3 can cause the test run to hang, requiring awareness of this constraint when testing lower-level utilities.
Tech stack
FAQ
Yes. Bats can test any UNIX program. You invoke the program inside a `@test` block using `run` and assert on its exit code and output, regardless of what language it is written in.
Use the `run` helper to execute the command. Its stdout and stderr are captured in `$output`. When the test fails, Bats prints `$output` automatically, so you can see what the command produced.
`load` loads a file relative to the test file, suitable for sharing project-local helper code. `bats_load_library` loads a named library from a system-wide location, intended for installed add-ons like bats-assert.
Use the `--filter` flag with a regular expression matching test names, or tag your tests and filter by tag. The `skip` helper inside a test body also lets you conditionally skip individual tests at runtime.
Bats-core is a fork of the original `sstephenson/bats` repository, created in September 2017 when the original stalled. The original was archived by its owner in April 2021; bats-core is now the actively maintained community version.