bats-testing-patterns

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

2|Updated Jun 16, 2026
One-click install
npx skills add https://github.com/monang404/lunawave --skill bats-testing-patterns-monang404
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: bats-testing-patterns
Source: https://github.com/monang404/lunawave/tree/main/.agent/skills/bats-testing-patterns
Command: npx skills add https://github.com/monang404/lunawave --skill bats-testing-patterns-monang404

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) tests so shell utilities can be validated in CI/CD pipelines. ## Core Features & Use Cases - Assertion Patterns: Test exit codes, stdout output, file creation, permissions, and multi-line results using Bats built-ins like run, $status, $output, and $lines. - Fixtures, Mocking & Stubbing: Manage fixture files, mock external commands, stub binaries via PATH injection, and isolate tests with setup/teardown and temp directories. - CI/CD Integration: Run Bats suites in GitHub Actions or Makefiles with TAP output, parallel execution, and shell-dialect compatibility checks (bash, sh, dash). - Use Case: You maintain a deployment shell script and want to verify it fails gracefully on missing arguments and produces correct output files. Use this Skill to scaffold a tests/ directory with Bats tests covering success paths, error conditions, and mocked external tools like curl. ## Quick Start Write Bats unit tests for my deploy.sh script covering its exit codes, output messages, and error handling for missing arguments.

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, wrap commands in the run helper, and assert on $status and $output variables. Use setup() and teardown() functions to create and clean temporary directories, and load a test_helper.sh for shared assertion utilities.

How to mock external commands in Bats tests?▼

Create executable stub scripts in a temporary directory and prepend it to PATH so the stub intercepts calls like curl. Alternatively, define a shell function with the same name and export it so the script under test invokes the mock.

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

Yes, Bats outputs TAP (Test Anything Protocol), which CI systems understand natively. Install it via npm or the bats-core installer, then run bats tests/*.bats in your workflow, optionally with --tap or --parallel flags.

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

Yes, you can write tests that invoke the script explicitly with bash, sh, or dash to verify portability. Use the skip command when a shell like dash is not installed so the suite still passes on minimal environments.

When should I not use Bats for testing?▼

Bats is unsuitable when the project has no shell scripts, when you need integration tests beyond shell behavior, or when the goal is only linting and formatting. For those cases use language-specific test frameworks or tools like ShellCheck.