Skip to content
FossShelf
Home / Categories / For Engineers

Bats Core

Automated testing framework for Bash scripts

Replaces —

6.36K stars

Who it's for

Shell script developer

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.

DevOps or infrastructure engineer

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.

Open source project maintainer

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

TAP-compliant output

Bats emits TAP (Test Anything Protocol) output, making it compatible with any TAP consumer including CI systems, test aggregators, and reporting tools.

`run` helper

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 hooks

`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.

Parallel execution

Bats supports running test cases in parallel, which can significantly reduce total test suite time for independent tests.

Library loading

`load` shares common code within a project; `bats_load_library` loads system-wide helper libraries, enabling ecosystem libraries like bats-assert and bats-file.

Test tagging and filtering

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

Languages
—
Frameworks
—
Databases
—
Tooling
—

FAQ

Can I use Bats to test programs not written in Bash?

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.

How do I see the output of a command when a test fails?

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.

What is the difference between `load` and `bats_load_library`?

`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.

How do I run only a subset of tests?

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.

Is bats-core the same as the original bats project?

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.

Similar tools

More in Engineers