Testing Bash Scripts with Bats
Write automated tests for your shell scripts using the Bats testing framework to catch regressions before they reach production.
Testing Bash Scripts with Bats is a free DevOps Bootcamp lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the DevOps Bootcamp learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Why Test Shell Scripts?
Scripts that run unattended (cron, CI) fail silently. Automated tests verify behavior so changes do not break working code.
Introducing Bats
Bats (Bash Automated Testing System) is a TAP-compliant framework for testing shell code. Tests are themselves Bash files.
# Install on Debian/Ubuntu
sudo apt-get install batsAnatomy of a Test File
A Bats file ends in .bats and defines tests with the @test keyword and a description.
@test "addition works" {
result=$(( 2 + 2 ))
[ "$result" -eq 4 ]
}Running Tests
Run a file or a whole directory with the bats command. A test passes when every command inside exits 0.
bats test/
bats math.batsThe run Helper
The run command executes a command and captures its exit code in $status and output in $output without aborting the test.
@test "ls succeeds" {
run ls /tmp
[ "$status" -eq 0 ]
}Asserting on Output
Compare $output against expected text. You can also index specific lines with ${lines[0]}.
@test "echo prints hello" {
run echo hello
[ "$output" = "hello" ]
}Testing Failure Cases
Good tests check error paths too. Assert a non-zero $status when a command should fail.
@test "missing file fails" {
run cat /no/such/file
[ "$status" -ne 0 ]
}Setup and Teardown
Define setup() and teardown() functions that run before and after EACH test to prepare and clean up fixtures.
setup() {
TMP=$(mktemp -d)
}
teardown() {
rm -rf "$TMP"
}Sourcing the Code Under Test
To test functions, source your script so its functions are available, then call them directly inside tests.
setup() {
source ./lib/utils.sh
}
@test "slugify lowercases" {
run slugify "Hello World"
[ "$output" = "hello-world" ]
}Skipping and Pending Tests
Use skip to mark a test as not-yet-implemented or unsupported on the current platform, with an optional reason.
@test "windows-only feature" {
skip "not supported on Linux"
}Integrating with CI
Because bats returns a non-zero exit code on failure, it plugs straight into CI pipelines as a gate before deployment.
# CI step
bats --tap test/ || exit 1Quick Check
Test your Bats knowledge.
Recap
You can now test Bash with Bats:
- Define tests with
@test; pass means all commands exit 0 - Use
runto capture$statusand$output - Prepare fixtures with
setup/teardown - Source scripts to test functions and gate CI on results
Frequently asked questions
Is the “Testing Bash Scripts with Bats” lesson free?
Yes — the full text of “Testing Bash Scripts with Bats” is free to read here on the web, and the DevOps Bootcamp course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the DevOps Bootcamp course, upgrade to CoddyKit PRO.
What will I learn in “Testing Bash Scripts with Bats”?
Write automated tests for your shell scripts using the Bats testing framework to catch regressions before they reach production. You practise DevOps Bootcamp with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start DevOps Bootcamp?
No prior experience is required. DevOps Bootcamp on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Testing Bash Scripts with Bats” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this DevOps Bootcamp lesson?
Yes. Every DevOps Bootcamp lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Debugging Bash Scripts (set -x, trap)
- Error Handling & Exit Status
- Scripting Best Practices & Linting
- Testing Bash Scripts with Bats