bats-testing-patterns

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

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

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 for shell scripts. ## Core Features & Use Cases - Assertion Patterns: Covers exit code, output, multi-line, and file-based assertions using Bats built-ins like run, $status, $output, and $lines. - Fixtures, Mocking, and Stubbing: Shows how to build fixture directories, stub external commands via PATH injection, and mock functions for isolated unit tests. - CI/CD Integration: Includes GitHub Actions and Makefile configurations for running Bats suites with TAP output and parallel execution. - Use Case: You maintain a set of deployment shell scripts and want to add them to a CI pipeline. Use this Skill to generate a Bats test suite with setup/teardown, stubbed network calls, and TAP reporting. ## Quick Start Write a Bats test suite for my shell script that verifies its exit codes and output, including setup, teardown, and a stubbed curl dependency.

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 test state.

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 mock functions in the test file and export them with export -f.

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

Yes, Bats outputs TAP (Test Anything Protocol), which CI systems understand natively. Install it via npm or from source in your workflow, then run bats tests/*.bats with the --tap flag for structured reporting.

Can Bats test scripts across different shell dialects?▼

Yes, you can invoke your script explicitly with bash, sh, or dash inside separate test cases to verify portability. Use the skip command when a dialect like dash is not installed on the test machine.

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

Multi-line output comparisons fail when trailing newlines or quoting differ between expected and actual values. Use the $lines array to assert on individual lines, or compare against an exact quoted string with embedded newlines.