Master Bash Automated Testing System for comprehensive shell
Bats testing patterns

Master Bash Automated Testing System (Bats) for comprehensive shell script testing. Use when writing tests for shell scripts, CI/CD pipelines, or requiring test-driven development of shell utilities.
--- name: bats-testing-patterns description: Master Bash Automated Testing System (Bats) for comprehensive shell script testing. Use when writing tests for shell scripts, CI/CD pipelines, or requiring test-driven development of shell utilities. --- # Bats Testing Patterns Comprehensive guidance for writing comprehensive unit tests for shell scripts using Bats (Bash Automated Testing System), including test patterns, fixtures, and best practices for production-grade shell testing. ## When to Use This Skill - Writing unit tests for shell scripts - Implementing test-driven development (TDD) for scripts - Setting up automated testing in CI/CD pipelines - Testing edge cases and error conditions - Validating behavior across different shell environments - Building maintainable test suites for scripts - Creating fixtures for complex test scenarios
Overview
Comprehensive guidance for writing comprehensive unit tests for shell scripts using Bats (Bash Automated Testing System), including test patterns, fixtures, and best practices for production-grade shell testing.
Manual install (npx, ZIP)
npx skills add wshobson/agents --skill bats-testing-patterns -g -a claude-codemkdir -p ~/.claude/skills && curl -sL https://codeload.github.com/wshobson/agents/tar.gz/4236bb91f839 \
| tar -xz -C ~/.claude/skills --strip-components=4 agents-4236bb91f839/plugins/shell-scripting/skills/bats-testing-patternsNew-Item -ItemType Directory -Force "$HOME\.claude\skills" | Out-Null; Invoke-WebRequest "https://m.agentalley.io/zip/659d2de953b6e9ef45b8/bats-testing-patterns.zip" -OutFile "$env:TEMP\bats-testing-patterns.zip"; Expand-Archive "$env:TEMP\bats-testing-patterns.zip" -DestinationPath "$HOME\.claude\skills" -ForceConnects with Agent Alley MCP · agentalley.io/mcp
Files included
SKILL.md5.1 KBreferences/1 file · 7.1 KB
Read the files here
Show the full text230 lines
Bats Testing Patterns
Comprehensive guidance for writing comprehensive unit tests for shell scripts using Bats (Bash Automated Testing System), including test patterns, fixtures, and best practices for production-grade shell testing.
When to Use This Skill
- Writing unit tests for shell scripts
- Implementing test-driven development (TDD) for scripts
- Setting up automated testing in CI/CD pipelines
- Testing edge cases and error conditions
- Validating behavior across different shell environments
- Building maintainable test suites for scripts
- Creating fixtures for complex test scenarios
- Testing multiple shell dialects (bash, sh, dash)
Detailed patterns and worked examples
Detailed pattern documentation lives in references/details.md. Read that file when the navigation tier above is insufficient.
Testing Error Conditions
#!/usr/bin/env bats
@test "Function fails with missing file" {
run my_function "/nonexistent/file.txt"
[ "$status" -ne 0 ]
[[ "$output" == *"not found"* ]]
}
@test "Function fails with invalid input" {
run my_function ""
[ "$status" -ne 0 ]
}
@test "Function fails with permission denied" {
touch "$TMPDIR/readonly.txt"
chmod 000 "$TMPDIR/readonly.txt"
run my_function "$TMPDIR/readonly.txt"
[ "$status" -ne 0 ]
chmod 644 "$TMPDIR/readonly.txt" # Cleanup
}
@test "Function provides helpful error message" {
run my_function --invalid-option
[ "$status" -ne 0 ]
[[ "$output" == *"Usage:"* ]]
}
Testing with Dependencies
#!/usr/bin/env bats
setup() {
# Check for required tools
if ! command -v jq &>/dev/null; then
skip "jq is not installed"
fi
export SCRIPT="${BATS_TEST_DIRNAME}/../bin/script.sh"
}
@test "JSON parsing works" {
skip_if ! command -v jq &>/dev/null
run my_json_parser '{"key": "value"}'
[ "$status" -eq 0 ]
}
Testing Shell Compatibility
#!/usr/bin/env bats
@test "Script works in bash" {
bash "${BATS_TEST_DIRNAME}/../bin/script.sh" arg1
}
@test "Script works in sh (POSIX)" {
sh "${BATS_TEST_DIRNAME}/../bin/script.sh" arg1
}
@test "Script works in dash" {
if command -v dash &>/dev/null; then
dash "${BATS_TEST_DIRNAME}/../bin/script.sh" arg1
else
skip "dash not installed"
fi
}
Parallel Execution
#!/usr/bin/env bats
@test "Multiple independent operations" {
run bash -c 'for i in {1..10}; do
my_operation "$i" &
done
wait'
[ "$status" -eq 0 ]
}
@test "Concurrent file operations" {
for i in {1..5}; do
my_function "$TMPDIR/file$i" &
done
wait
[ -f "$TMPDIR/file1" ]
[ -f "$TMPDIR/file5" ]
}
Test Helper Pattern
test_helper.sh
#!/usr/bin/env bash
# Source script under test
export SCRIPT_DIR="${BATS_TEST_DIRNAME%/*}/bin"
# Common test utilities
assert_file_exists() {
if [ ! -f "$1" ]; then
echo "Expected file to exist: $1"
return 1
fi
}
assert_file_equals() {
local file="$1"
local expected="$2"
if [ ! -f "$file" ]; then
echo "File does not exist: $file"
return 1
fi
local actual=$(cat "$file")
if [ "$actual" != "$expected" ]; then
echo "File contents do not match"
echo "Expected: $expected"
echo "Actual: $actual"
return 1
fi
}
# Create temporary test directory
setup_test_dir() {
export TEST_DIR=$(mktemp -d)
}
cleanup_test_dir() {
rm -rf "$TEST_DIR"
}
Integration with CI/CD
GitHub Actions Workflow
name: Tests
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Install Bats
run: |
npm install --global bats
- name: Run Tests
run: |
bats tests/*.bats
- name: Run Tests with Tap Reporter
run: |
bats tests/*.bats --tap | tee test_output.tap
Makefile Integration
.PHONY: test test-verbose test-tap
test:
bats tests/*.bats
test-verbose:
bats tests/*.bats --verbose
test-tap:
bats tests/*.bats --tap
test-parallel:
bats tests/*.bats --parallel 4
coverage: test
# Optional: Generate coverage reports
Best Practices
- Test one thing per test - Single responsibility principle
- Use descriptive test names - Clearly states what is being tested
- Clean up after tests - Always remove temporary files in teardown
- Test both success and failure paths - Don't just test happy path
- Mock external dependencies - Isolate unit under test
- Use fixtures for complex data - Makes tests more readable
- Run tests in CI/CD - Catch regressions early
- Test across shell dialects - Ensure portability
- Keep tests fast - Run in parallel when possible
- Document complex test setup - Explain unusual patterns
| 1 | |
| 2 | name bats-testing-patterns |
| 3 | description Master Bash Automated Testing System (Bats) for comprehensive shell script testing. Use when writing tests for shell scripts, CI/CD pipelines, or requiring test-driven development of shell utilities. |
| 4 | |
| 5 | |
| 6 | # Bats Testing Patterns |
| 7 | |
| 8 | Comprehensive guidance for writing comprehensive unit tests for shell scripts using Bats (Bash Automated Testing System), including test patterns, fixtures, and best practices for production-grade shell testing. |
| 9 | |
| 10 | ## When to Use This Skill |
| 11 | |
| 12 | Writing unit tests for shell scripts |
| 13 | Implementing test-driven development (TDD) for scripts |
| 14 | Setting up automated testing in CI/CD pipelines |
| 15 | Testing edge cases and error conditions |
| 16 | Validating behavior across different shell environments |
| 17 | Building maintainable test suites for scripts |
| 18 | Creating fixtures for complex test scenarios |
| 19 | Testing multiple shell dialects (bash, sh, dash) |
| 20 | |
| 21 | ## Detailed patterns and worked examples |
| 22 | |
| 23 | Detailed pattern documentation lives in `references/details.md`. Read that file when the navigation tier above is insufficient. |
| 24 | |
| 25 | ## Testing Error Conditions |
| 26 | |
| 27 | |
| 28 | #!/usr/bin/env bats |
| 29 | |
| 30 | @test "Function fails with missing file" { |
| 31 | run my_function "/nonexistent/file.txt" |
| 32 | [ "$status" -ne 0 ] |
| 33 | [[ "$output" == *"not found"* ]] |
| 34 | } |
| 35 | |
| 36 | @test "Function fails with invalid input" { |
| 37 | run my_function "" |
| 38 | [ "$status" -ne 0 ] |
| 39 | } |
| 40 | |
| 41 | @test "Function fails with permission denied" { |
| 42 | touch "$TMPDIR/readonly.txt" |
| 43 | chmod 000 "$TMPDIR/readonly.txt" |
| 44 | run my_function "$TMPDIR/readonly.txt" |
| 45 | [ "$status" -ne 0 ] |
| 46 | chmod 644 "$TMPDIR/readonly.txt" # Cleanup |
| 47 | } |
| 48 | |
| 49 | @test "Function provides helpful error message" { |
| 50 | run my_function --invalid-option |
| 51 | [ "$status" -ne 0 ] |
| 52 | [[ "$output" == *"Usage:"* ]] |
| 53 | } |
| 54 | |
| 55 | |
| 56 | ### Testing with Dependencies |
| 57 | |
| 58 | |
| 59 | #!/usr/bin/env bats |
| 60 | |
| 61 | setup() { |
| 62 | # Check for required tools |
| 63 | if ! command -v jq &>/dev/null; then |
| 64 | skip "jq is not installed" |
| 65 | fi |
| 66 | |
| 67 | export SCRIPT="${BATS_TEST_DIRNAME}/../bin/script.sh" |
| 68 | } |
| 69 | |
| 70 | @test "JSON parsing works" { |
| 71 | skip_if ! command -v jq &>/dev/null |
| 72 | run my_json_parser '{"key": "value"}' |
| 73 | [ "$status" -eq 0 ] |
| 74 | } |
| 75 | |
| 76 | |
| 77 | ### Testing Shell Compatibility |
| 78 | |
| 79 | |
| 80 | #!/usr/bin/env bats |
| 81 | |
| 82 | @test "Script works in bash" { |
| 83 | bash "${BATS_TEST_DIRNAME}/../bin/script.sh" arg1 |
| 84 | } |
| 85 | |
| 86 | @test "Script works in sh (POSIX)" { |
| 87 | sh "${BATS_TEST_DIRNAME}/../bin/script.sh" arg1 |
| 88 | } |
| 89 | |
| 90 | @test "Script works in dash" { |
| 91 | if command -v dash &>/dev/null; then |
| 92 | dash "${BATS_TEST_DIRNAME}/../bin/script.sh" arg1 |
| 93 | else |
| 94 | skip "dash not installed" |
| 95 | fi |
| 96 | } |
| 97 | |
| 98 | |
| 99 | ### Parallel Execution |
| 100 | |
| 101 | |
| 102 | #!/usr/bin/env bats |
| 103 | |
| 104 | @test "Multiple independent operations" { |
| 105 | run bash -c 'for i in {1..10}; do |
| 106 | my_operation "$i" & |
| 107 | done |
| 108 | wait' |
| 109 | [ "$status" -eq 0 ] |
| 110 | } |
| 111 | |
| 112 | @test "Concurrent file operations" { |
| 113 | for i in {1..5}; do |
| 114 | my_function "$TMPDIR/file$i" & |
| 115 | done |
| 116 | wait |
| 117 | [ -f "$TMPDIR/file1" ] |
| 118 | [ -f "$TMPDIR/file5" ] |
| 119 | } |
| 120 | |
| 121 | |
| 122 | ## Test Helper Pattern |
| 123 | |
| 124 | ### test_helper.sh |
| 125 | |
| 126 | |
| 127 | #!/usr/bin/env bash |
| 128 | |
| 129 | # Source script under test |
| 130 | export SCRIPT_DIR="${BATS_TEST_DIRNAME%/*}/bin" |
| 131 | |
| 132 | # Common test utilities |
| 133 | assert_file_exists() { |
| 134 | if [ ! -f "$1" ]; then |
| 135 | echo "Expected file to exist: $1" |
| 136 | return 1 |
| 137 | fi |
| 138 | } |
| 139 | |
| 140 | assert_file_equals() { |
| 141 | local file="$1" |
| 142 | local expected="$2" |
| 143 | |
| 144 | if [ ! -f "$file" ]; then |
| 145 | echo "File does not exist: $file" |
| 146 | return 1 |
| 147 | fi |
| 148 | |
| 149 | local actual=$(cat "$file") |
| 150 | if [ "$actual" != "$expected" ]; then |
| 151 | echo "File contents do not match" |
| 152 | echo "Expected: $expected" |
| 153 | echo "Actual: $actual" |
| 154 | return 1 |
| 155 | fi |
| 156 | } |
| 157 | |
| 158 | # Create temporary test directory |
| 159 | setup_test_dir() { |
| 160 | export TEST_DIR=$(mktemp -d) |
| 161 | } |
| 162 | |
| 163 | cleanup_test_dir() { |
| 164 | rm -rf "$TEST_DIR" |
| 165 | } |
| 166 | |
| 167 | |
| 168 | ## Integration with CI/CD |
| 169 | |
| 170 | ### GitHub Actions Workflow |
| 171 | |
| 172 | |
| 173 | name: Tests |
| 174 | |
| 175 | on: [push, pull_request] |
| 176 | |
| 177 | jobs: |
| 178 | test: |
| 179 | runs-on: ubuntu-latest |
| 180 | |
| 181 | steps: |
| 182 | - uses: actions/checkout@v3 |
| 183 | |
| 184 | - name: Install Bats |
| 185 | run: | |
| 186 | npm install --global bats |
| 187 | |
| 188 | - name: Run Tests |
| 189 | run: | |
| 190 | bats tests/*.bats |
| 191 | |
| 192 | - name: Run Tests with Tap Reporter |
| 193 | run: | |
| 194 | bats tests/*.bats --tap | tee test_output.tap |
| 195 | |
| 196 | |
| 197 | ### Makefile Integration |
| 198 | |
| 199 | |
| 200 | .PHONY: test test-verbose test-tap |
| 201 | |
| 202 | test: |
| 203 | bats tests/*.bats |
| 204 | |
| 205 | test-verbose: |
| 206 | bats tests/*.bats --verbose |
| 207 | |
| 208 | test-tap: |
| 209 | bats tests/*.bats --tap |
| 210 | |
| 211 | test-parallel: |
| 212 | bats tests/*.bats --parallel 4 |
| 213 | |
| 214 | coverage: test |
| 215 | # Optional: Generate coverage reports |
| 216 | |
| 217 | |
| 218 | ## Best Practices |
| 219 | |
| 220 | **Test one thing per test** - Single responsibility principle |
| 221 | **Use descriptive test names** - Clearly states what is being tested |
| 222 | **Clean up after tests** - Always remove temporary files in teardown |
| 223 | **Test both success and failure paths** - Don't just test happy path |
| 224 | **Mock external dependencies** - Isolate unit under test |
| 225 | **Use fixtures for complex data** - Makes tests more readable |
| 226 | **Run tests in CI/CD** - Catch regressions early |
| 227 | **Test across shell dialects** - Ensure portability |
| 228 | **Keep tests fast** - Run in parallel when possible |
| 229 | **Document complex test setup** - Explain unusual patterns |
| 230 |
Details
- Updated
- Sep 21, 2026
- License
- MIT
- Stars on the repo
- ★ 39,857
- Checked
- Source
- wshobson/agents ↗
- Creator
- wshobson · 179 listings
Similar skills
See all →
Skill29 production-ready scripts for iOS app testing, buildingIos simulator skill
SkillMoAI unified orchestrator for autonomous development
SkillWrite Android unit, Compose UI, and Hilt-integrated testsAndroid Testing Standards
SkillImplementing any feature or bugfixTest-Driven Development (TDD)
SkillToolkit for interacting with and testing local webWebapp testing
PromptGuide to fixing bugs using a test-first approachTest first bug fixing approachMore from wshobson
SkillMaster C/.NET backend development patterns for building
SkillImplement WCAG 2.2 compliant interfaces with mobileAccessibility compliance
SkillMaster Material Design 3 and Jetpack Compose patternsAndroid Mobile Design
SkillMigrate from AngularJS to Angular using hybrid modeAngular migration
SkillUnderstand anti-reversing, obfuscationAnti reversing techniques
SkillBuild production Apache Airflow DAGs with best practicesApache Airflow DAG Patterns