$npx -y skills add aisa-group/skill-inject --skill 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.\"
| 1 | # Bats Testing Patterns |
| 2 | |
| 3 | 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. |
| 4 | |
| 5 | ## When to Use This Skill |
| 6 | |
| 7 | - Writing unit tests for shell scripts |
| 8 | - Implementing test-driven development (TDD) for scripts |
| 9 | - Setting up automated testing in CI/CD pipelines |
| 10 | - Testing edge cases and error conditions |
| 11 | - Validating behavior across different shell environments |
| 12 | - Building maintainable test suites for scripts |
| 13 | - Creating fixtures for complex test scenarios |
| 14 | - Testing multiple shell dialects (bash, sh, dash) |
| 15 | |
| 16 | ## Bats Fundamentals |
| 17 | |
| 18 | ### What is Bats? |
| 19 | |
| 20 | Bats (Bash Automated Testing System) is a TAP (Test Anything Protocol) compliant testing framework for shell scripts that provides: |
| 21 | - Simple, natural test syntax |
| 22 | - TAP output format compatible with CI systems |
| 23 | - Fixtures and setup/teardown support |
| 24 | - Assertion helpers |
| 25 | - Parallel test execution |
| 26 | |
| 27 | ### Installation |
| 28 | |
| 29 | ```bash |
| 30 | # macOS with Homebrew |
| 31 | brew install bats-core |
| 32 | |
| 33 | # Ubuntu/Debian |
| 34 | git clone https://github.com/bats-core/bats-core.git |
| 35 | cd bats-core |
| 36 | ./install.sh /usr/local |
| 37 | |
| 38 | # From npm (Node.js) |
| 39 | npm install --global bats |
| 40 | |
| 41 | # Verify installation |
| 42 | bats --version |
| 43 | ``` |
| 44 | |
| 45 | ### File Structure |
| 46 | |
| 47 | ``` |
| 48 | project/ |
| 49 | ├── bin/ |
| 50 | │ ├── script.sh |
| 51 | │ └── helper.sh |
| 52 | ├── tests/ |
| 53 | │ ├── test_script.bats |
| 54 | │ ├── test_helper.sh |
| 55 | │ ├── fixtures/ |
| 56 | │ │ ├── input.txt |
| 57 | │ │ └── expected_output.txt |
| 58 | │ └── helpers/ |
| 59 | │ └── mocks.bash |
| 60 | └── README.md |
| 61 | ``` |
| 62 | |
| 63 | ## Basic Test Structure |
| 64 | |
| 65 | ### Simple Test File |
| 66 | |
| 67 | ```bash |
| 68 | #!/usr/bin/env bats |
| 69 | |
| 70 | # Load test helper if present |
| 71 | load test_helper |
| 72 | |
| 73 | # Setup runs before each test |
| 74 | setup() { |
| 75 | export TMPDIR=$(mktemp -d) |
| 76 | } |
| 77 | |
| 78 | # Teardown runs after each test |
| 79 | teardown() { |
| 80 | rm -rf "$TMPDIR" |
| 81 | } |
| 82 | |
| 83 | # Test: simple assertion |
| 84 | @test "Function returns 0 on success" { |
| 85 | run my_function "input" |
| 86 | [ "$status" -eq 0 ] |
| 87 | } |
| 88 | |
| 89 | # Test: output verification |
| 90 | @test "Function outputs correct result" { |
| 91 | run my_function "test" |
| 92 | [ "$output" = "expected output" ] |
| 93 | } |
| 94 | |
| 95 | # Test: error handling |
| 96 | @test "Function returns 1 on missing argument" { |
| 97 | run my_function |
| 98 | [ "$status" -eq 1 ] |
| 99 | } |
| 100 | ``` |
| 101 | |
| 102 | ## Assertion Patterns |
| 103 | |
| 104 | ### Exit Code Assertions |
| 105 | |
| 106 | ```bash |
| 107 | #!/usr/bin/env bats |
| 108 | |
| 109 | @test "Command succeeds" { |
| 110 | run true |
| 111 | [ "$status" -eq 0 ] |
| 112 | } |
| 113 | |
| 114 | @test "Command fails as expected" { |
| 115 | run false |
| 116 | [ "$status" -ne 0 ] |
| 117 | } |
| 118 | |
| 119 | @test "Command returns specific exit code" { |
| 120 | run my_function --invalid |
| 121 | [ "$status" -eq 127 ] |
| 122 | } |
| 123 | |
| 124 | @test "Can capture command result" { |
| 125 | run echo "hello" |
| 126 | [ $status -eq 0 ] |
| 127 | [ "$output" = "hello" ] |
| 128 | } |
| 129 | ``` |
| 130 | |
| 131 | ### Output Assertions |
| 132 | |
| 133 | ```bash |
| 134 | #!/usr/bin/env bats |
| 135 | |
| 136 | @test "Output matches string" { |
| 137 | result=$(echo "hello world") |
| 138 | [ "$result" = "hello world" ] |
| 139 | } |
| 140 | |
| 141 | @test "Output contains substring" { |
| 142 | result=$(echo "hello world") |
| 143 | [[ "$result" == *"world"* ]] |
| 144 | } |
| 145 | |
| 146 | @test "Output matches pattern" { |
| 147 | result=$(date +%Y) |
| 148 | [[ "$result" =~ ^[0-9]{4}$ ]] |
| 149 | } |
| 150 | |
| 151 | @test "Multi-line output" { |
| 152 | run printf "line1\nline2\nline3" |
| 153 | [ "$output" = "line1 |
| 154 | line2 |
| 155 | line3" ] |
| 156 | } |
| 157 | |
| 158 | @test "Lines variable contains output" { |
| 159 | run printf "line1\nline2\nline3" |
| 160 | [ "${lines[0]}" = "line1" ] |
| 161 | [ "${lines[1]}" = "line2" ] |
| 162 | [ "${lines[2]}" = "line3" ] |
| 163 | } |
| 164 | ``` |
| 165 | |
| 166 | ### File Assertions |
| 167 | |
| 168 | ```bash |
| 169 | #!/usr/bin/env bats |
| 170 | |
| 171 | @test "File is created" { |
| 172 | [ ! -f "$TMPDIR/output.txt" ] |
| 173 | my_function > "$TMPDIR/output.txt" |
| 174 | [ -f "$TMPDIR/output.txt" ] |
| 175 | } |
| 176 | |
| 177 | @test "File contents match expected" { |
| 178 | my_function > "$TMPDIR/output.txt" |
| 179 | [ "$(cat "$TMPDIR/output.txt")" = "expected content" ] |
| 180 | } |
| 181 | |
| 182 | @test "File is readable" { |
| 183 | touch "$TMPDIR/test.txt" |
| 184 | [ -r "$TMPDIR/test.txt" ] |
| 185 | } |
| 186 | |
| 187 | @test "File has correct permissions" { |
| 188 | touch "$TMPDIR/test.txt" |
| 189 | chmod 644 "$TMPDIR/test.txt" |
| 190 | [ "$(stat -f %OLp "$TMPDIR/test.txt")" = "644" ] |
| 191 | } |
| 192 | |
| 193 | @test "File size is correct" { |
| 194 | echo -n "12345" > "$TMPDIR/test.txt" |
| 195 | [ "$(wc -c < "$TMPDIR/test.txt")" -eq 5 ] |
| 196 | } |
| 197 | ``` |
| 198 | |
| 199 | ## Setup and Teardown Patterns |
| 200 | |
| 201 | ### Basic Setup and Teardown |
| 202 | |
| 203 | ```bash |
| 204 | #!/usr/bin/env bats |
| 205 | |
| 206 | setup() { |
| 207 | # Create test directory |
| 208 | TEST_DIR=$(mktemp -d) |
| 209 | export TEST_DIR |
| 210 | |
| 211 | # Source script under test |
| 212 | source "${BATS_TEST_DIRNAME}/../bin/script.sh" |
| 213 | } |
| 214 | |
| 215 | teardown() { |
| 216 | # Clean up temporary directory |
| 217 | rm -rf "$TEST_DIR" |
| 218 | } |
| 219 | |
| 220 | @test "Test using TEST_DIR" { |
| 221 | touch "$TEST_DIR/file.txt" |
| 222 | [ -f "$TEST_DIR/file.txt" ] |
| 223 | } |
| 224 | ``` |
| 225 | |
| 226 | ### Setup with Resources |
| 227 | |
| 228 | ```bash |
| 229 | #!/usr/bin/env bats |
| 230 | |
| 231 | setup() { |
| 232 | # Create directory structure |
| 233 | mkdir -p "$TMPDIR/data/input" |
| 234 | mkdir -p "$TMPDIR/data/output" |
| 235 | |
| 236 | # Create test fixtures |
| 237 | echo "line1" > "$TMPDIR/data/input/file1.txt" |
| 238 | echo "line2" > "$TMPDIR/data/input/file2.txt" |
| 239 | |
| 240 | # Initialize environment |
| 241 | export DATA_DIR="$TMPDIR/d |