bats-testing-patterns

Write unit tests for shell scripts using the Bats testing framework.

Updated Apr 23, 2026
One-click install
npx skills add https://github.com/SanketAdlak/PDMProjectDesign --skill bats-testing-patterns-sanketadlak
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: bats-testing-patterns
Source: https://github.com/SanketAdlak/PDMProjectDesign/tree/main/.agents/skills/bats-testing-patterns
Command: npx skills add https://github.com/SanketAdlak/PDMProjectDesign --skill bats-testing-patterns-sanketadlak

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Shell scripts often ship without automated tests, making regressions and edge-case failures hard to catch. This Skill provides structured patterns for writing, organizing, and running Bats (Bash Automated Testing System) test suites so shell utilities can be tested like any other code. ## Core Features & Use Cases - Assertion Patterns: Test exit codes, output strings, multi-line output, file existence, contents, and permissions. - Fixtures and Mocking: Manage fixture files, stub external commands like curl, and mock functions to isolate the unit under test. - CI/CD Integration: Run Bats suites in GitHub Actions or Makefiles with TAP output and parallel execution. - Use Case: You maintain a deployment shell script and want to verify it handles missing arguments, permission errors, and works across bash, sh, and dash before every release. ## Quick Start Write Bats unit tests for my shell script that verify its exit codes, output, and error handling.

Frequently Asked Questions about bats-testing-patterns

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I write unit tests for shell scripts with Bats?

Create a .bats file with @test blocks, use the run command to execute your script, then assert on $status for exit codes and $output for stdout. Add setup and teardown functions to manage temporary directories and fixtures.

How to mock external commands in Bats tests?

Create executable stub scripts in a temporary directory and prepend it to PATH so your stubs shadow real commands like curl. Alternatively, define shell functions with the same name and export them to override external tools.

Does Bats work in CI/CD pipelines like GitHub Actions?

Yes, Bats produces TAP-compliant output that CI systems understand. Install it via npm or from source in your workflow, then run bats tests/*.bats, optionally with --tap or --parallel flags for reporting and speed.

Can Bats test scripts across different shells like sh and dash?

Yes, you can write tests that invoke your script explicitly with bash, sh, or dash to verify portability. Use the skip command when a shell like dash is not installed on the test machine.

Why does my Bats test fail when checking multi-line output?

The $output variable joins lines with newlines, which can cause quoting mismatches. Use the $lines array instead to assert on individual lines, such as checking that ${lines[0]} equals the expected first line.