Scripts2026年7月14日·1 分钟阅读

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.

Agent 就绪

Agent 可直接安装

这个资产可安装;Agent 先选择当前运行时、检查安装计划,再运行匹配命令。

Native · 98/100策略:允许
Agent 入口
任意 MCP/CLI Agent
类型
Skill
安装
Single
信任
信任等级:Established
入口
Bats-Core Overview
直接安装命令
npx -y tokrepo@latest install d7e8d9e0-7f5c-11f1-9bc6-00163e2b0d79 --target codex

先 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

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产