0PricingLogin
Linux Command Line & Bash Scripting Mastery · Lesson

Unit Testing Functions with Bats-core

Structure test files, assertions, and setup/teardown to verify individual Bash functions.

What Is Bats-core and Why Use It?

Bats-core (Bash Automated Testing System) is the de-facto unit testing framework for Bash. It lets you write structured, repeatable tests for your shell functions and scripts — the same way you would use JUnit for Java or pytest for Python.

  • Each test is a @test block with a human-readable description.
  • Tests pass when every command inside returns exit code 0.
  • Tests fail on the first non-zero exit code or a failed assertion.
  • Output is TAP-compatible, so CI systems (GitHub Actions, Jenkins, GitLab CI) understand it natively.

Install via your package manager or clone the repository:

# Install via git (recommended — always latest)
git clone https://github.com/bats-core/bats-core.git
cd bats-core && sudo ./install.sh /usr/local

# Or on macOS with Homebrew
brew install bats-core

# Verify installation
bats --version
# bats 1.x.y

Your First Bats Test File

A Bats test file has the extension .bats and starts with a special shebang. The core building block is the @test directive followed by a description string and a block of commands.

  • The shebang #!/usr/bin/env bats tells the shell how to execute the file.
  • Each @test block is an independent test case.
  • You can run a single file with bats my_tests.bats or an entire directory with bats test/.

Below is the minimal structure for a Bats test file:

#!/usr/bin/env bats
# File: test/hello.bats

@test "echo outputs the expected string" {
  result=$(echo "hello world")
  [ "$result" = "hello world" ]
}

@test "false command causes test to fail" {
  # Uncommenting the next line would make this test fail:
  # false
  true
}

All lessons in this course

  1. Unit Testing Functions with Bats-core
  2. Mocking Commands and Stubbing External Tools
  3. Fixtures, Temp Environments, and Coverage
  4. Running Shell Tests in CI Pipelines
← Back to Linux Command Line & Bash Scripting Mastery