Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Testing Bash Scripts With Bats in Linux
Bash Automated Testing System (BATS) is a testing framework designed specifically for bash scripts. It allows developers to write automated tests for their bash code, ensuring scripts function correctly before deployment and improving code reliability in production environments.
BATS provides a structured approach to testing bash scripts similar to testing frameworks available for other programming languages like Python or Java. It uses a simple syntax that makes writing and maintaining tests straightforward for shell script developers.
What is BATS?
BATS stands for Bash Automated Testing System. It is a TAP-compliant testing framework that runs tests written in bash syntax. The framework executes test cases and provides clear feedback about which tests pass or fail, making it easier to identify issues in bash scripts before they reach production.
Key features of BATS include
Simple syntax Tests are written using familiar bash constructs
Assertion helpers Built-in functions for common test scenarios
Detailed output Clear reporting of test results and failures
Integration support Works well with CI/CD pipelines
Setting Up BATS
First, ensure git is installed on your system
sudo apt update sudo apt install git git version
Initialize your project directory and install BATS with its helper libraries as git submodules
git init git submodule add https://github.com/bats-core/bats-core.git test/bats git submodule add https://github.com/bats-core/bats-support.git test/test_helper/bats-support git submodule add https://github.com/bats-core/bats-assert.git test/test_helper/bats-assert git submodule add https://github.com/bats-core/bats-file.git test/test_helper/bats-file
Creating a Sample Bash Script
Let's create a simple bash script to demonstrate testing. Create browser_choice.sh
#!/usr/bin/env bash
if [ $# -ne 3 ]; then
echo "Usage: $0 <browser> <search_engine> <output_file>"
exit 1
fi
choice="Web Browser $1 over $2"
echo "$choice"
echo "$choice" > "$3"
exit 0
Make the script executable
chmod +x browser_choice.sh
Writing BATS Tests
Create a test file test/browser_choice.bats
#!/usr/bin/env bats
load 'test_helper/bats-support/load'
load 'test_helper/bats-assert/load'
@test "script exists and is executable" {
[ -x "./browser_choice.sh" ]
}
@test "script runs with correct arguments" {
run ./browser_choice.sh firefox google output.txt
assert_success
assert_output "Web Browser firefox over google"
}
@test "script creates output file" {
run ./browser_choice.sh chrome bing test_output.txt
assert_success
[ -f "test_output.txt" ]
run cat test_output.txt
assert_output "Web Browser chrome over bing"
rm -f test_output.txt
}
@test "script fails with insufficient arguments" {
run ./browser_choice.sh firefox
assert_failure
assert_output --partial "Usage:"
}
Running Tests
Execute the BATS tests from your project root directory
./test/bats/bin/bats test/browser_choice.bats
Expected output
browser_choice.bats ? script exists and is executable ? script runs with correct arguments ? script creates output file ? script fails with insufficient arguments 4 tests, 0 failures
Common BATS Assertions
| Assertion | Purpose |
|---|---|
assert_success |
Verify command succeeded (exit code 0) |
assert_failure |
Verify command failed (non-zero exit) |
assert_output |
Check exact output match |
assert_line |
Verify specific output line |
Conclusion
BATS provides a robust testing framework for bash scripts, bringing automated testing practices to shell scripting. It offers simple syntax for writing tests, comprehensive assertion helpers, and clear output reporting. By implementing BATS testing, developers can significantly improve the reliability and maintainability of their bash scripts.
