Cette page est affichée en anglais. Une traduction française est en cours.
ScriptsJul 14, 2026·3 min de lecture

Bats-Core — Bash Automated Testing System

Bats-Core is a TAP-compliant testing framework for Bash scripts, letting you write and run unit tests for shell code with familiar test syntax.

Prêt pour agents

Installation agent prête

Cet actif peut être installé après choix du runtime, vérification du plan et exécution de la commande adaptée.

Native · 98/100Policy : autoriser
Surface agent
Tout agent MCP/CLI
Type
Skill
Installation
Single
Confiance
Confiance : Established
Point d'entrée
Bats-Core Overview
Commande d'installation directe
npx -y tokrepo@latest install d7e8d9e0-7f5c-11f1-9bc6-00163e2b0d79 --target codex

À exécuter après confirmation du plan en dry-run.

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.

Sources

Fil de discussion

Connectez-vous pour rejoindre la discussion.
Aucun commentaire pour l'instant. Soyez le premier à partager votre avis.

Actifs similaires