bats-testing-patterns

Write Bats unit tests for shell scripts with assertions and mocking.

38.6k|4.1k|Updated Jul 24, 2025
One-click install
npx skills add https://github.com/wshobson/agents --skill bats-testing-patterns-wshobson
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: bats-testing-patterns
Source: https://github.com/wshobson/agents/tree/main/plugins/shell-scripting/skills/bats-testing-patterns
Command: npx skills add https://github.com/wshobson/agents --skill bats-testing-patterns-wshobson

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

This Skill provides comprehensive guidance and patterns for writing robust unit tests for shell scripts using the Bash Automated Testing System (Bats), ensuring script reliability and maintainability.

Core Features & Use Cases

  • Test Structure: Learn basic and advanced test file structures.
  • Assertion Patterns: Master exit code, output, and file assertions.
  • Setup/Teardown: Implement effective setup and teardown routines for test isolation.
  • Mocking/Stubbing: Practice mocking external commands and stubbing functions.
  • CI/CD Integration: Integrate Bats tests into GitHub Actions and Makefiles.
  • Use Case: You've written a critical shell script for your CI/CD pipeline. Use this Skill to learn how to write comprehensive Bats tests to ensure it functions correctly under various conditions and doesn't break future deployments.

Quick Start

Use the bats-testing-patterns skill to generate a basic Bats test file for a shell script named 'my_script.sh'.

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 using Bats?

Bats testing organizes shell script validation into structured test cases using the `@test` syntax. You assert exit codes, output, and file states to verify Bash script behavior under various conditions.

How do I mock external commands in Bats tests?

Mocking external commands in Bats tests involves creating stub functions that shadow the real commands in your execution path. This skill outlines mocking and stubbing patterns to isolate shell scripts and test specific behaviors without external dependencies.

How do I integrate Bats tests into a CI/CD pipeline?

Integrating Bats tests into a CI/CD pipeline involves running your test suites within GitHub Actions or Makefiles during the build process. This skill provides integration patterns to automate shell script validation and prevent future deployment failures.

What is the best way to isolate test environments in Bats?

The best way to isolate test environments in Bats is by using setup and teardown routines. This skill covers implementing these routines to prepare and clean up the testing environment before and after each test case runs.

Does Bats work with existing Makefiles and GitHub Actions?

Yes, Bats works with existing Makefiles and GitHub Actions. This skill provides specific integration patterns to execute Bats shell script tests within these CI/CD platforms to ensure robust deployment validation.