bats-testing-patterns
Master Shell Testing with Bats Framework
Writing reliable shell scripts without tests leads to production failures. This skill teaches comprehensive Bats testing patterns to validate shell scripts with unit tests, fixtures, and CI/CD integration.
Download the skill ZIP
Upload in Claude
Go to Settings → Capabilities → Skills → Upload skill
Toggle on and start using
Test it
Using "bats-testing-patterns". Create a Bats test to verify my backup script creates the correct files
Expected outcome:
Generated Bats test file with @test blocks for file creation validation, setup function creating temporary test directory, teardown cleaning up test files, and assertions checking file existence and permissions.
Using "bats-testing-patterns". Add error handling tests for my validation function
Expected outcome:
Created test cases for missing arguments, invalid input formats, permission denied scenarios, and helpful error messages. Each test uses run command and checks exit status and error output.
Using "bats-testing-patterns". Set up test fixtures for complex input scenarios
Expected outcome:
Implemented fixtures directory structure with sample input files, helper function to load fixtures in setup, and comparison assertions using diff against expected output files.
Security Audit
SafeStatic analysis flagged 103 patterns including external commands, filesystem operations, and network references. All findings are false positives from educational code examples teaching Bats testing patterns. The skill contains no executable code, only documentation with test pattern examples. URLs reference legitimate documentation sites (GitHub, readthedocs.io). No security risks identified.
Quality Score
What You Can Build
DevOps Engineer Testing Deployment Scripts
Write comprehensive unit tests for deployment automation scripts to validate behavior before production rollout. Test setup, teardown, error handling, and edge cases.
Developer Implementing TDD for Shell Utilities
Apply test-driven development methodology to shell script development. Write tests first, then implement functionality to meet test specifications.
Site Reliability Engineer Validating System Scripts
Create automated test suites for system administration scripts in CI/CD pipelines. Validate scripts work across different environments and shell dialects.
Try These Prompts
Create a Bats test file for my script that validates command line argument parsing and exit codes
Add setup and teardown functions to my Bats test that create temporary directories and clean up test fixtures
Show me how to mock the curl command in my Bats tests so I can test API functions without real network calls
Create a GitHub Actions workflow that runs my Bats test suite on every pull request with TAP output format
Best Practices
- Use setup and teardown functions to create isolated test environments with temporary directories that are cleaned up after each test
- Mock external dependencies and commands to make tests fast, deterministic, and independent of external services or system state
- Write descriptive test names that clearly explain what is being validated, making test failures easier to diagnose and understand
Avoid
- Avoid tests that depend on specific system state or files outside the test directory, which makes tests fragile and non-portable
- Do not skip teardown cleanup, as leftover temporary files accumulate and can cause test failures or fill disk space
- Never test multiple unrelated behaviors in a single test case, which makes it difficult to identify which specific functionality failed
Frequently Asked Questions
What is Bats and when should I use it?
How do I install Bats on my system?
Can I mock external commands in my tests?
How do I test error conditions and failures?
Can Bats tests run in parallel?
How do I integrate Bats with CI/CD pipelines?
Developer Details
Author
wshobsonLicense
MIT
Repository
https://github.com/wshobson/agents/tree/main/plugins/shell-scripting/skills/bats-testing-patternsRef
main
File structure
📄 SKILL.md