BATS(7)

πŸ¦‹ BATS(7) πŸ¦‹

okay so this is the manual page for Bats β€” the Bash Automated Testing System β€” and specifically it's the whole tea on the Bats test file format. bestie, grab a snack, we're getting into it πŸ’…


✨ NAME ✨

bats β€” the Bats test file format, and everything it's giving


🌸 DESCRIPTION 🌸

So a Bats test file is literally just a Bash script, but with special syntax for defining test cases. Under the hood, every single test case is just a function with a description. iconic behavior honestly.

#!/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 ]
}

Now here's the part that's lowkey kind of mind-blowing: each Bats test file gets evaluated n+1 times, where n is the number of test cases in the file. The first run just counts how many test cases there are, then it iterates over the test cases and executes each one in its own process. slay, isolation πŸ’–


πŸŽ€ Tagging tests πŸŽ€

Every test has a list of tags attached to it, and without any specification that list is just empty. You can define tags two ways β€” the first one being # bats test_tags=:

# bats test_tags=tag:1, tag:2, tag:3 @test "second test" { # ... }

@test "second test" { # ... }

These tags (tag:1, tag:2, tag:3) will be attached to the test first test. The second test will have no tags attached, no cap. Values defined in the # bats test_tags= directive get assigned to the next @test that's encountered in the file and then forgotten after that. And only the value of the last # bats test_tags= directive before a given test will be used, so don't be messy about it πŸ™ˆ

Sometimes you want to give all the tests in a file the same set of tags β€” that's what # bats file_tags= is for. They get added to all tests in the file after that directive. And an additional # bats file_tags= directive will override the previously defined values:

@test "Zeroth test" {
  # will have no tags
}

# bats file_tags=a:b
# bats test_tags=c:d

@test "First test" {
  # will be tagged a:b, c:d
}

# bats file_tags=

@test "Second test" {
  # will have no tags
}

Tags are case sensitive and must only consist of alphanumeric characters and _, -, or :. They must not contain whitespaces! The colon is intended as a separator for (recursive) namespacing, iykyk.

Tag lists must be separated by commas and are allowed to contain whitespace. They must not contain empty tags like test_tags=,b (first tag is empty), test_tags=a,,c, test_tags=a, ,c (second tag is only whitespace/empty), test_tags=a,b, (third tag is empty). literally just don't do it 🫢

Every tag starting with bats: (case insensitive!) is reserved for Bats' internal use:

  • bats:focus β€” if any test with the tag bats:focus is encountered in a test suite, only those tagged with this tag will be executed.

In focus mode, the exit code of successful runs will be overridden to 1 to prevent CI from silently running on a subset of tests due to an accidentally committed bats:focus tag. Should you require the true exit code, e.g. for a git bisect operation, you can disable this behavior by setting BATS_NO_FAIL_FOCUS_RUN=1 when running bats, but make sure not to commit this to CI! πŸ’€

fr fr, that last warning is not a vibe β€” commit a bats:focus by accident and your CI is quietly testing like 3 tests and calling it a day 😭

πŸ”₯ THE RUN HELPER πŸ”₯

Usage:

run [OPTIONS] [--]

Okay so many Bats tests need to run a command and then make assertions about its exit status and output. Bats includes a run helper that:

  • invokes its arguments as a command
  • saves the exit status and output into special global variables
  • (optionally) checks exit status against a given expected value

If it's successful, run returns with a 0 status code so you can keep making assertions in your test case. main character energy πŸ‘‘

For example, let's say you're testing that the foo command, when passed a nonexistent filename, exits with a 1 status code and prints an error message:

@test "invoking foo with a nonexistent file prints an error" {
  run -1 foo nonexistent_filename
  [ "$output" = "foo: no such file 'nonexistent_filename'" ]
}

The -1 as the first argument tells run to expect 1 as an exit status, and to fail if the command exits with any other value. On failure, both actual and expected values will be displayed, along with the invoked command and its output:

(in test file test.bats, line 2)
 `run -1 foo nonexistent_filename' failed, expected exit code 1, got 127

This error indicates a possible problem with the installation or configuration of foo β€” and note that a simple [ $status != 0 ] test would not have caught this kind of failure. see, attention to detail ✨

The $status variable contains the status code of the command, and the $output variable contains the combined contents of the command's standard output and standard error streams. both. together. in one variable. we love convenience πŸ’–

A third special variable, the $lines array, is available for easily accessing individual lines of output. For example, if you want to test that invoking foo without any arguments prints usage information on the first line:

@test "invoking foo without arguments prints usage" {
  run -1 foo
  [ "${lines[0]}" = "usage: foo <filename>" ]
}

By default run leaves out empty lines in ${lines[@]}. Use run --keep-empty-lines to retain them. the more you know 🌈

Additionally, you can use --separate-stderr to split stdout and stderr into $output/$stderr and ${lines[@]}/${stderr_lines[@]}. separate vibes for separate streams, periodt.

All additional parameters to run should come before the command. And if you want to run a command that starts with -, prefix it with -- to prevent run from parsing it as an option. tiny detail, huge difference πŸŽ€


🐚 THE BATS_PIPE HELPER 🐚

Usage:

bats_pipe [OPTIONS] [--]

The bats_pipe helper command is meant to handle piping between commands. Its main purpose is to aide the run helper command (which cannot handle pipes, due to bash parsing priority). run command0 | command1 will parse | before run, which is commonly not intended by test authors. oops πŸ™ˆ

Running run bats_pipe command0 \| command1 will have the piped commands run within the context of the run command, and thus have the output and status variables properly contained within the normal output and status variables. chef's kiss.

Note that this requires the usage of \|, not |. This is to avoid bash parsing out | first, instead sending \| to the bats_pipe command for it to parse and set up intended piping. Running bats_pipe with no instances of \| will always fail β€” this is intended to catch typos (accidentally using |) by the test author. protective king πŸ‘‘

The bats_pipe command will also properly propagate exit status from the piped commands. The default behavior mimics set -o pipefail, returning the status of the last (rightmost) command that exits with a non-zero status. This ensures that usage of pipes do not mask the exit statuses of earlier commands. no masking, no lies πŸ’…

@test "invoking foo piped to bar" {
  run bats_pipe foo \| bar
  # asserting foo or bar would return 17 (from foo if bar returns 0).
  [ "$status" -eq 17 ]
  [ "$output" = "bar output." ]
}

Alternatively, if the test always cares about the status of a specific command, the -N option can be given (e.g. -0) to always return the status of the command of interest:

@test "invoking foo piped to bar always return foo status" {
  run bats_pipe -0 foo \| bar
  # status of bar is ignored, status is always from foo.
  [ "$status" -eq 2 ]
  [ "$output" = "bar output." ]
}

Similarly, --returned-status N (or --returned-status=N) can be used for similar functionality. This option supports negative values, which always return the status of the command starting from the end and in reverse order:

@test "invoking foo piped to bar always return foo status" {
  run bats_pipe --returned-status -2 foo \| bar
  # status of bar is ignored, status is always from foo.
  [ "$status" -eq 2 ]
  [ "$output" = "bar output." ]
}

Piping of command output is especially helpful when the output needs to be modified in some way (e.g. the command outputs binary data into stdout, which cannot be stored as-is in an environment variable). look at this little icon:

@test "invoking foo that returns binary data" {
  run bats_pipe foo \| hexdump -v -e "1/1 \"0x%02X \""
  [ "$status" -eq 17 ]
  [[ "$output" =~ 0xDE\ 0xAD ]]
}

Any number of pipes can be used in conjunction to chain output between some set of running commands. go off, chain as many as you want πŸ¦‹


πŸ’œ THE LOAD COMMAND πŸ’œ

You may want to share common code across multiple test files, which, valid. Bats includes a convenient load command for sourcing a Bash source file relative to the location of the current test file. For example, if you have a Bats test in test/foo.bats, the command

load test_helper

will source the script test/test_helper.bash in your test file. This can be useful for sharing functions to set up your environment or load fixtures. reusability is self-care πŸŽ€


πŸ‘‘ THE BATS_LOAD_LIBRARY COMMAND πŸ‘‘

Some libraries are installed on the system, e.g. by npm or brew. These should not be loaded, as their path depends on the installation method. Instead, one should use bats_load_library together with setting BATS_LIB_PATH, a PATH-like colon-delimited variable.

bats_load_library has two modes of resolving requests:

  1. by relative path from the BATS_LIB_PATH to a file in the library
  2. by library name, expecting libraries to have a load.bash entrypoint

For example if your BATS_LIB_PATH is set to ~/.bats/libs:/usr/lib/bats, then bats_load_library test_helper would look for existing files with the following paths:

  • ~/.bats/libs/test_helper
  • ~/.bats/libs/test_helper/load.bash
  • /usr/lib/bats/test_helper
  • /usr/lib/bats/test_helper/load.bash

The first existing file in this list will be sourced. first come, first served ✨

If you want to load only part of a library or the entry point is not named load.bash, you have to include it in the argument: bats_load_library library_name/file_to_load will try

  • ~/.bats/libs/library_name/file_to_load
  • ~/.bats/libs/library_name/file_to_load/load.bash
  • /usr/lib/bats/library_name/file_to_load
  • /usr/lib/bats/library_name/file_to_load/load.bash

Apart from the changed lookup rules, bats_load_library behaves like load. same energy, different lookup, iykyk πŸ’…

Note: As seen above load.bash is the entry point for libraries and meant to load more files from its directory or other libraries.
Note: Obviously, the actual BATS_LIB_PATH is highly dependent on the environment. To maintain a uniform location across systems, (distribution) package maintainers are encouraged to use /usr/lib/bats/ as the install path for libraries where possible. However, if the package manager has another preferred location, like npm or brew, you should use this instead. do what your package manager says, not me 🌸

✨ THE SKIP COMMAND ✨

okay so sometimes a test just isn't it today and you need it to not run, right? You bail out with the skip command, right at the exact point in the test where you wanna dip 🌸

@test "A test I don't want to execute for now" {
  skip
  run -0 foo
}

Bestie, you can also drop a reason in there so future-you isn't left like "why did I skip this??" 😭

@test "A test I don't want to execute for now" {
  skip "This command will return zero soon, but not now"
  run -0 foo
}

Or lowkey skip it conditionally, which is honestly the main character move πŸ’…

@test "A test which should run" {
  if [ foo != bar ]; then
    skip "foo isn't bar"
  fi

  run -0 foo
}

πŸ¦‹ THE BATS_REQUIRE_MINIMUM_VERSION COMMAND πŸ¦‹

not to be dramatic but this one is important πŸ”₯ Code written for newer versions of Bats can be totally incompatible with older versions. Best case scenario? You get an error message and a failed test suite β€” annoying but honest. Worst case? The tests just... pass. erroneously. And that's masking a failure, which is genuinely the scariest thing here πŸ’€

So use bats_require_minimum_version <Bats version number> to avoid all that. It's basically you announcing, in the most concise way possible, that you intend the code that follows to be run under that Bats version or higher ✨

On top of that, this function communicates the current Bats version floor to subsequent code, which lets e.g. Bats' internal warning give more informed warnings. cute AND useful πŸŽ€

Note: by default, calling bats_require_minimum_version with versions before Bats 1.7.0 will fail regardless of the required version, because the function just isn't available there. But!! you can use the bats-backports plugin (https://github.com/bats-core/bats-backports) to make your code usable with older versions β€” say, during migration while your CI system is not yet upgraded πŸ™ˆ

🧚 SETUP AND TEARDOWN FUNCTIONS 🧚

You can define special setup and teardown functions that run before and after each test case, respectively. Use them to load fixtures, set up your environment, and clean up when you're done. it's giving responsible queen behavior πŸ‘‘


🌈 CODE OUTSIDE OF TEST CASES 🌈

You can include code in your test file outside of @test functions. Like, super useful if you want to check for dependencies and fail immediately if they're not present β€” no cap, fail fast is the vibe ⭐

BUT β€” and this is the one, bestie β€” any output that you print in code outside of @test, setup or teardown functions must be redirected to stderr (>&2). Otherwise, the output may cause Bats to fail by polluting the TAP stream on stdout 😭 literally just redirect it, it takes two seconds.


πŸ‘‘ SPECIAL VARIABLES πŸ‘‘

There are several global variables you can use to introspect on Bats tests. collect them all, iykyk 🫢

  • $BATS_TEST_FILENAME β€” the fully expanded path to the Bats test file.
  • $BATS_TEST_DIRNAME β€” the directory in which the Bats test file is located.
  • $BATS_TEST_NAMES β€” an array of function names for each test case.
  • $BATS_TEST_NAME β€” the name of the function containing the current test case.
  • BATS_TEST_NAME_PREFIX β€” will be prepended to the description of each test on stdout and in reports.
  • $BATS_TEST_DESCRIPTION β€” the description of the current test case.
  • BATS_TEST_RETRIES β€” the maximum number of additional attempts that will be made on a failed test before it is finally considered failed. The default of 0 means the test must pass on the first attempt. no second chances energy πŸ’€
  • BATS_TEST_TIMEOUT β€” the number of seconds after which a test (including setup) will be aborted and marked as failed. Updates to this value in setup() or @test cannot change the running timeout countdown, so the latest useful update location is setup_file(). put it where it counts, okay? ⏰
  • $BATS_TEST_NUMBER β€” the (1-based) index of the current test case in the test file.
  • $BATS_SUITE_TEST_NUMBER β€” the (1-based) index of the current test case in the test suite (over all files).
  • $BATS_TMPDIR β€” the base temporary directory used by bats to create its temporary files / directories. (default: $TMPDIR. If $TMPDIR is not set, /tmp is used.)
  • $BATS_RUN_TMPDIR β€” the location to the temporary directory used by bats to store all its internal temporary files during the tests. (default: $BATS_TMPDIR/bats-run-$BATS_ROOT_PID-XXXXXX)
  • $BATS_FILE_EXTENSION β€” (default: bats) specifies the extension of test files that should be found when running a suite (via bats [-r] suite_folder/)
  • $BATS_TEST_TAGS β€” the tags of the current test.
  • $BATS_SUITE_TMPDIR β€” a temporary directory common to all tests of a suite. Could be used to create files required by multiple tests.
  • $BATS_FILE_TMPDIR β€” a temporary directory common to all tests of a test file. Could be used to create files required by multiple tests in the same test file.
  • $BATS_TEST_TMPDIR β€” a temporary directory unique for each test. Could be used to create files required only for specific tests.
  • $BATS_VERSION β€” the version of Bats running the test.

πŸ’– SEE ALSO πŸ’–


and that's the whole page bestie!! πŸŽ€ this one's from bats-core, dated July 2026, section BATS(7) β€” go forth and write tests that actually tell you the truth, mwah πŸ’…πŸ¦‹βœ¨

πŸ“¦ where this page comes from