LinuxCommandLibrary

bats

Automated testing framework for bash scripts

TLDR

Run test file

$ bats [test.bats]
copy
Run all tests in directory
$ bats [tests/]
copy
Tap output
$ bats --tap [test.bats]
copy
Pretty formatting
$ bats --pretty [test.bats]
copy
Filter by test name
$ bats --filter ["pattern"] [test.bats]
copy

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

$ #!/usr/bin/env bats

@test "addition works" {
  result="$(echo 2+2 | bc)"
  [ "$result" -eq 4 ]
}

@test "files exist" {
  [ -f "/etc/hosts" ]
}
copy

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/status
load file
Load helper library
skip [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)

> TERMINAL_GEAR

Curated for the Linux community

Copied to clipboard

> TERMINAL_GEAR

Curated for the Linux community