Introduction
Bats-Core is the community-maintained successor to the original Bats project. It provides a simple way to verify that shell scripts and command-line programs behave as expected, using a clean syntax that extends Bash with test annotations.
What Bats-Core Does
- Runs test files written in a Bash-compatible syntax with @test annotations
- Outputs results in TAP (Test Anything Protocol) format for CI integration
- Supports setup and teardown functions at both file and test level
- Provides helper libraries for assertions, file operations, and output matching
- Executes tests in parallel for faster feedback on large test suites
Architecture Overview
Each .bats file is preprocessed into a valid Bash script where @test blocks become functions. The Bats runner discovers test functions, executes each in a subshell for isolation, captures exit codes and output, then formats results as TAP. Helper libraries like bats-assert and bats-support are sourced into tests as needed.
Self-Hosting & Configuration
- Install via npm, Homebrew, or by cloning the repository
- Place test files with .bats extension in a test directory
- Load helper libraries with bats_load_library in setup functions
- Configure parallel execution with the --jobs flag
- Integrate with CI by parsing TAP output or using JUnit formatters
Key Features
- Minimal syntax that feels natural to shell script authors
- Test isolation through subshell execution prevents side effects
- TAP and JUnit output formats for CI pipeline compatibility
- Extensible helper ecosystem for assertions and file checks
- Parallel test execution for large test suites
Comparison with Similar Tools
- ShellCheck — static analysis for shell scripts; Bats tests runtime behavior
- shunit2 — xUnit-style shell testing; Bats uses a simpler annotation-based syntax
- pytest — Python testing framework; Bats is purpose-built for Bash and shell scripts
- Bash itself — manual test scripts with set -e; Bats adds structure and reporting
- Make — task runner that can run tests; Bats provides proper test isolation and output
FAQ
Q: Can Bats test non-Bash programs? A: Yes, any command-line program can be tested since Bats runs commands and checks exit codes and output.
Q: How do I skip a test conditionally? A: Use the skip command inside a test block with an optional reason string.
Q: Does it support test fixtures? A: Yes, setup and teardown functions run before and after each test. File-level setup_file and teardown_file run once per file.
Q: Can I use it in CI/CD pipelines? A: Yes, Bats outputs TAP format by default and supports JUnit XML via the --formatter flag.