explaingit

sstephenson/bats

7,109ShellAudience · ops devopsComplexity · 2/5Setup · easy

TLDR

Bash Automated Testing System, a testing framework for shell scripts that lets you write test cases in plain Bash and verify that your command-line tools produce the right output and exit codes.

Mindmap

mindmap
  root((bats))
    What it does
      Tests shell scripts
      Checks exit codes
      Checks output text
    Key helpers
      run command
      load shared code
      skip a test
    Setup and teardown
      Per-test setup
      Per-test teardown
    Output formats
      Human readable
      TAP for CI
Click or tap to explore — scroll the page freely

Code map

Detail Auto

An interactive map of this repo's files and how they connect — its source is parsed live in your browser. Click Visualize to build it.

filefunction / class

Things people build with this

USE CASE 1

Write automated tests for your Bash scripts to catch regressions before deploying to production.

USE CASE 2

Verify that command-line tools return the correct exit codes and expected output strings.

USE CASE 3

Integrate shell script tests into a CI pipeline using TAP-format output that standard test reporters understand.

Tech stack

ShellBash

Getting it running

Difficulty · easy Time to first run · 5min

This repo is no longer maintained, use the community bats-core fork for continued development.

In plain English

Bats stands for Bash Automated Testing System. It is a testing framework for shell scripts, specifically for the Bash shell that is standard on Linux and macOS. Its purpose is to help developers verify that their command-line programs and Bash scripts behave correctly, in the same way that testing libraries exist for languages like Python or JavaScript. A Bats test file is a plain Bash script with a special @test block syntax. Each block has a description and contains shell commands. If every command in a test block exits successfully, the test passes. If any command fails, the test is marked as failing. This design means that writing tests requires no new language to learn beyond what you already know about shell scripting. Bats provides a few built-in helpers to make common testing patterns easier. The run helper lets you execute a command and then check its exit code and output separately. The load helper lets you share setup code across multiple test files. The skip command lets you mark a test as temporarily bypassed, optionally with a reason. You can also define setup and teardown functions that run before and after each test case to prepare and clean up the environment. Running tests is straightforward: pass a file or folder to the bats command, and it prints a summary showing which tests passed and which failed. When run in automated systems like CI pipelines, it outputs results in TAP format, a plain-text standard that many test reporting tools understand. Installation is a clone-and-run process with no package dependencies. The project is no longer actively maintained in this original repository, but a community-maintained fork named bats-core has taken over continued development.

Copy-paste prompts

Prompt 1
I'm writing Bats tests for a Bash script that parses CSV files. Show me how to use the `run` helper to check both the exit code and stdout output.
Prompt 2
How do I share common setup code between multiple Bats test files using the `load` helper?
Prompt 3
Write me a Bats test file that checks my CLI tool prints 'Hello World' and exits with code 0.
Prompt 4
Show me how to use the `setup` and `teardown` functions in Bats to create and clean up temp files around each test.
Prompt 5
My Bats tests are passing locally but failing in CI. What environment differences should I check first?
Open on GitHub → Explain another repo

← sstephenson on gitmyhub — every repo by this author, as a profile.

Verify against the repo before relying on details.