ããã¯ããªã«ãããããŠæžãããã®ïŒ
ãã¡ãã§Bashã¹ã¯ãªããããã¹ãããå Žåã®éžæè¢ã調ã¹ãŠã¿ãŸããã
Bashスクリプトのテスティングフレームワークを調べてみる - CLOVER🍀
ãã®äžã ãšããŸãã¯Batsãå ¥éã«ããã®ããªãšæã£ãã®ã§ãã¥ãŒããªã¢ã«ã詊ããŠã¿ãããšæããŸãã
Bats
Batsã®ããã¥ã¡ã³ãã¯ãã¡ãã
Welcome to bats-core’s documentation! — bats-core 1 documentation
GitHubãªããžããªãŒã¯ãã¡ãã
GitHub - bats-core/bats-core: Bash Automated Testing System
Batshã¯ãBash 3.2以äžã察象ã«ããTAPã«æºæ ãããã¹ãã£ã³ã°ãã¬ãŒã ã¯ãŒã¯ã§ãã
Bats (Bash Automated Testing System) is a TAP-compliant testing framework for Bash 3.2 or above.
Arch LinuxãAlpine LinuxãDebianãFedoraãGentoo LinuxãopenSUSEãUbuntu Linuxã§ã¯ããã±ãŒãžãããŒãžã£ãŒããã®ã€ã³ã¹ããŒã«ã
å¯èœã§ãã
Installation — bats-core 1 documentation
ãšããã§ãä»ã®Batsã¯ãªãªãžãã«ãã®ã§ã¯ãªãã¿ããã§ããã1.0ããåããªãªãžãã«ã®ããã§ãã
Note: Bats versions pre 1.0 are from sstephensonâs original project.
ã€ãŸããä»ã®Batsã¯ãã©ãŒã¯ã§ãã以äžãèŠããš2017幎ã«ãã©ãŒã¯ããããªãªãžãã«ã®Batsã¯2021幎ã«ã¢ãŒã«ã€ããããããšãããããŸãã
Bats-core: Bash Automated Testing System / Background
ããã¥ã¡ã³ãããŸãšãŸã£ãŠããã®ã¯Bats-Coreã§ããã以äžã®ãããªè¿œå ã¢ãžã¥ãŒã«ãããããã§ãã
- bats-assert
- Batså ±éã®ã¢ãµãŒã·ã§ã³ãæäŸãããã«ããŒã©ã€ãã©ãªãŒ
- GitHub - bats-core/bats-assert: Common assertions for Bats
- bats-file
- äžè¬çãªãã¡ã€ã«ã·ã¹ãã é¢é£ã®ã¢ãµãŒã·ã§ã³ãšãã«ããŒãæäŸããã©ã€ãã©ãªãŒ
- GitHub - bats-core/bats-file: Common filesystem assertions for Bats
bats-supportãšãããã®ããããŸãããããã¯éçºè åãã®ãã®ã¿ããã§ãããbats-assertãšbats-fileãäŸåããŠããŸãã
GitHub - bats-core/bats-support: Supporting library for Bats test helpers
éçºè åãã®ããã¥ã¡ã³ãã¯ãã¡ãã
GitHub - bats-core/bats-docs: Shared Bats helper documentation
ä»åã¯ãã¡ãã®ãã¥ãŒããªã¢ã«ã詊ããŠã¿ãŸãã
Tutorial — bats-core 1 documentation
ç°å¢
ä»åã®ç°å¢ã¯ãã¡ãã
$ lsb_release -a No LSB modules are available. Distributor ID: Ubuntu Description: Ubuntu 24.04.3 LTS Release: 24.04 Codename: noble $ uname -srvmpio Linux 6.8.0-79-generic #79-Ubuntu SMP PREEMPT_DYNAMIC Tue Aug 12 14:42:46 UTC 2025 x86_64 x86_64 x86_64 GNU/Linux
Batsãã€ã³ã¹ããŒã«ãã
ãŸãã¯Batsãã€ã³ã¹ããŒã«ããŸãããã
batsã§æ€çŽ¢ãããš
$ apt search bats
ãã®ããããèŠã€ãããŸãã
bats/noble 1.10.0-1 all bash automated testing system bats-assert/noble 2.1.0-3 all Helper library providing common assertions for Bats bats-file/noble 0.4.0-1 all Helper library providing filesystem-related assertions for Bats bats-support/noble 0.3.0-4 all Supporting library to test bats helper libraries
ææ°ã®Batsã¯1.12.0ã®ããã§ãããä»åã¯ããã±ãŒãžãããŒãžã£ãŒã§ã€ã³ã¹ããŒã«ã§ãã1.10.0ã䜿ãããšã«ããŸãã
ã¡ãªã¿ã«ã1.10.0ã®ããã¥ã¡ã³ããèŠããšçŸåšã®ãã®ãšã¯èŠãç®ã倧ããå€ãã£ãŠããŠé©ããŸãâŠã
Installation — bats-core 1 documentation
1.11.0ã§ããã¥ã¡ã³ãã倧ããå€ãã£ãããã§ããä»åã¯1.10ã®ãã®ãèŠãŠããããšã«ããŸãã
ã€ã³ã¹ããŒã«ã
$ sudo apt install bats
ããŒãžã§ã³ã
$ bats --version Bats 1.10.0
ãã«ãã
$ bats --help
Bats 1.10.0
Usage: bats [OPTIONS] <tests>
bats [-h | -v]
<tests> is the path to a Bats test file, or the path to a directory
containing Bats test files (ending with ".bats")
-c, --count Count test cases without running any tests
--code-quote-style <style>
A two character string of code quote delimiters
or 'custom' which requires setting $BATS_BEGIN_CODE_QUOTE and
$BATS_END_CODE_QUOTE. Can also be set via $BATS_CODE_QUOTE_STYLE
--line-reference-format Controls how file/line references e.g. in stack traces are printed:
- comma_line (default): a.bats, line 1
- colon: a.bats:1
- uri: file:///tests/a.bats:1
- custom: provide your own via defining bats_format_file_line_reference_custom
with parameters <filename> <line>, store via `printf -v "$output"`
-f, --filter <regex> Only run tests that match the regular expression
--filter-status <status> Only run tests with the given status in the last completed (no CTRL+C/SIGINT) run.
Valid <status> values are:
failed - runs tests that failed or were not present in the last run
missed - runs tests that were not present in the last run
--filter-tags <comma-separated-tag-list>
Only run tests that match all the tags in the list (&&).
You can negate a tag via prepending '!'.
Specifying this flag multiple times allows for logical or (||):
`--filter-tags A,B --filter-tags A,!C` matches tags (A && B) || (A && !C)
-F, --formatter <type> Switch between formatters: pretty (default),
tap (default w/o term), tap13, junit, /<absolute path to formatter>
--gather-test-outputs-in <directory>
Gather the output of failing *and* passing tests
as files in directory (if existing, must be empty)
-h, --help Display this help message
-j, --jobs <jobs> Number of parallel jobs (requires GNU parallel or shenwei356/rush)
--parallel-binary-name Name of parallel binary
--no-tempdir-cleanup Preserve test output temporary directory
--no-parallelize-across-files
Serialize test file execution instead of running
them in parallel (requires --jobs >1)
--no-parallelize-within-files
Serialize test execution within files instead of
running them in parallel (requires --jobs >1)
--report-formatter <type> Switch between reporters (same options as --formatter)
-o, --output <dir> Directory to write report files (must exist)
-p, --pretty Shorthand for "--formatter pretty"
--print-output-on-failure Automatically print the value of `$output` on failed tests
-r, --recursive Include tests in subdirectories
--show-output-of-passing-tests
Print output of passing tests
-t, --tap Shorthand for "--formatter tap"
-T, --timing Add timing information to tests
-x, --trace Print test commands as they are executed (like `set -x`)
--verbose-run Make `run` print `$output` by default
-v, --version Display the version number
For more information, see https://github.com/bats-core/bats-core
Batsã§ãã¹ããæžã
ããã§ã¯ããã¥ãŒããªã¢ã«ã«æ²¿ã£ãŠãã¹ããæžããŠãã£ãŠã¿ãŸãããã
Tutorial — bats-core 1 documentation
ãã¹ã察象ã®ã¹ã¯ãªããã¯srcã«ããã¹ãçšã®ã¹ã¯ãªããã¯testã«é 眮ãããããªã®ã§ãã£ã¬ã¯ããªãŒãäœæã
$ mkdir {src,test}
ããšã¯ãã¥ãŒããªã¢ã«ã«æ²¿ã£ãŠé²ããŠãããŸãã
æåã®ãã¹ããæžã
æåã®ãã¹ããæžããŠã¿ãŸãã
ãœãŒã¹ã³ãŒããäœããªããŸãŸããã¹ãã ãäœæã
test/test.bats
@test "can run out script" {
./project.sh
}
å®è¡ããã¡ãã倱æããŸãã
$ bats test/test.bats
test.bats
â can run out script
(in test file test/test.bats, line 2)
`./project.sh' failed with status 127
/path/to/test/test.bats: è¡ 2: ./project.sh: ãã®ãããªãã¡ã€ã«ããã£ã¬ã¯ããªã¯ãããŸãã
1 test, 1 failure
ããã§ãsrcãã£ã¬ã¯ããªãŒå ã«ã¹ã¯ãªãããäœæã
src/project.sh
#!/bin/bash echo 'Hello World'
å®è¡æš©éãä»äžããŸãã
$ chmod +x src/project.sh
ãã¹ãã¹ã¯ãªãããããã¹ã察象ã®ã¹ã¯ãªãããèŠãæã¯batsã³ãã³ããå®è¡ãããã£ã¬ã¯ããªãŒãèµ·ç¹ã«ãªããããªã®ã§ã以äžã®ããã«ä¿®æ£ã
test/test.bats
@test "can run out script" {
src/project.sh
}
ä»åºŠã¯ãã¹ãããã¹ããŸãã
$ bats test/test.bats test.bats â can run out script 1 test, 0 failures
ã»ããã¢ãããã
次ã¯ã»ããã¢ãããè¡ã£ãŠã¿ãŸãã
Tutorial / Letâs do some setup
ãã¹ãã¹ã¯ãªããããã®ããã«å€æŽã
test/test.bats
setup() {
DIR=$(cd $(dirname $BATS_TEST_FILENAME) >/dev/null 2>&1 && pwd)
PATH="$DIR/../src:$PATH"
}
@test "can run our script" {
project.sh
}
setup颿°å ã§ãsrcãã£ã¬ã¯ããªãŒã«ãã¹ãéãããã«ããŸããã
setup颿°ãšããã®ã¯åãã¹ãã®åã«å®è¡ããã颿°ã®ããã§ããåãã¹ãã®å®è¡åŸã«åŒã°ãã颿°ãå®çŸ©ããå Žåã¯teardownã«ãªãããã§ãã
Writing tests / setup and teardown: Pre- and post-test hooks
ãã®ãã¹ãã¹ã¯ãªããã§å®çŸ©ãããŠãããã¹ãŠã®ãã¹ãã®åããã³åŸã§å®è¡ãããå Žåã¯ãsetup_fileãšteardown_fileã䜿ãããã§ãã
$BATS_TEST_FILENAMEãšããã®ã¯ããã¹ãã¹ã¯ãªããã®çµ¶å¯Ÿãã¡ã€ã«ãã¹ã§ãã
Writing tests / Special variables
å®è¡ã
$ bats test/test.bats test.bats â can run our script 1 test, 0 failures
OKã§ããã
åºåãæ±ã
Tutorial / Dealing with output
ã²ãšãŸãsrc/project.shã以äžã®ããã«å€æŽãã
src/project.sh
#!/bin/bash echo 'Welcome to our project!' echo 'NOT IMPLEMENTED!' >&2 exit 1
exit 1ããŠããã®ã§ãã¹ãã倱æããããšã確èªããŸãã
$ bats test/test.bats
test.bats
â can run our script
(in test file test/test.bats, line 7)
`project.sh' failed
Welcome to our project!
NOT IMPLEMENTED!
1 test, 1 failure
ããã§ããã¹ãã¹ã¯ãªãããä¿®æ£ããã®ã«bats-assertãå¿ èŠã«ãªãã®ã§ã€ã³ã¹ããŒã«ããŸãã
$ sudo apt install bats-assert
ã€ã³ã¹ããŒã«ããããŒãžã§ã³ã¯ãã¡ãã
$ apt show bats-assert Package: bats-assert Version: 2.1.0-3 Priority: optional Section: universe/devel Origin: Ubuntu Maintainer: Ubuntu Developers <ubuntu-devel-discuss@lists.ubuntu.com> Original-Maintainer: Debian Bats team <team+bats@tracker.debian.org> Bugs: https://bugs.launchpad.net/ubuntu/+filebug Installed-Size: 58.4 kB Depends: bats-support Enhances: bats Homepage: https://github.com/bats-core/bats-assert Download-Size: 9,042 B APT-Manual-Installed: yes APT-Sources: http://archive.ubuntu.com/ubuntu noble/universe amd64 Packages Description: Helper library providing common assertions for Bats bats-assert provides various ready-made assertions that can be used to make Bats tests simpler to understand and to debug. . For example: . * assert_success: exit status is 0. * assert_output: output contains given content. * assert_line: a specific line of output contains given content.
ã¡ãªã¿ã«äŸåããŠããbats-supportãã€ããŠããŸãã
$ apt show bats-support Package: bats-support Version: 0.3.0-4 Priority: optional Section: universe/libdevel Origin: Ubuntu Maintainer: Ubuntu Developers <ubuntu-devel-discuss@lists.ubuntu.com> Original-Maintainer: Debian Bats team <team+bats@tracker.debian.org> Bugs: https://bugs.launchpad.net/ubuntu/+filebug Installed-Size: 25.6 kB Homepage: https://github.com/bats-core/bats-support Download-Size: 5,326 B APT-Manual-Installed: no APT-Sources: http://archive.ubuntu.com/ubuntu noble/universe amd64 Packages Description: Supporting library to test bats helper libraries bats-support is a supporting library providing common functions to test helper libraries written for Bats. . ç¹åŸŽ: . * error reporting * output formatting * language tools
ãã¹ãã¹ã¯ãªããã以äžã®ããã«ä¿®æ£ã
test/test.bats
setup() {
load '/usr/lib/bats/bats-support/load'
load '/usr/lib/bats/bats-assert/load'
DIR=$(cd $(dirname $BATS_TEST_FILENAME) >/dev/null 2>&1 && pwd)
PATH="$DIR/../src:$PATH"
}
@test "can run our script" {
run project.sh
assert_output 'Welcome to our project!'
}
bats-fileãããŒãããå¿
èŠããããŸããããã®ãã¹ã¯ä»¥äžã®ã³ãã³ãã§ç¢ºèªããŸãããæåŸã®.bashã¯æç€ºçã«ä»äžããªããŠããload颿°ã
è£ã£ãŠãããŸãã
â»ããã¥ã¡ã³ãã§ã¯bats-supportã®ããŒããå¿
èŠã ãšæžãããŠããŸããããªããŠãåãããã§ãã
$ dpkg -L bats-assert | grep load /usr/lib/bats/bats-assert/load.bash $ dpkg -L bats-support | grep load /usr/lib/bats/bats-support/load.bash
ã¡ãªã¿ã«ãload颿°ã§ã¯ãªãbats_load_library颿°ã䜿ããšãã£ãšç°¡æœã«æå®ã§ããããã§ãã
Batsのbats_load_library関数で、ロードするライブラリーのパスを簡潔に指定する - CLOVER🍀
ãŸãproject.shã®åŒã³åºããrunã³ãã³ãçµç±ã«ãªã£ãŠããŸãããããã¯Batsã®é¢æ°ã§ãã
Writing tests / run: Test other commands
run颿°ã¯å®è¡ããã³ãã³ãã®çµäºã¹ããŒã¿ã¹ãåºåãã°ããŒãã«å€æ°ã«ä¿åããã³ãã³ãã§ãããããªã€ã¡ãŒãžã§ããã
@test "invoking foo with a nonexistent file prints an error" {
run foo nonexistent_filename
[ "$status" -eq 1 ]
[ "$output" = "foo: no such file 'nonexistent_filename'" ]
[ "$BATS_RUN_COMMAND" = "foo nonexistent_filename" ]
}
assert_output颿°ã¯$outputã«å¯ŸããŠã¢ãµãŒã·ã§ã³ãè¡ããŸãã
bats-assert / Full Assertion API / assert_output
ãããŠãããå®è¡ãããšãŸã ãã¹ãã«å€±æããŸãã
$ bats test/test.bats
test.bats
â can run our script
(in test file test/test.bats, line 11)
`assert_output 'Welcome to our project!'' failed
-- output differs --
expected (1 lines):
Welcome to our project!
actual (2 lines):
Welcome to our project!
NOT IMPLEMENTED!
--
1 test, 1 failure
ããã¯assert_output颿°ã$outputå
šäœãšæ¯èŒããããšããŠããããã§ããã
ããã§assert_output颿°ã«--partialãªãã·ã§ã³ã远å ããŠãéšåäžèŽã«ããŸãã
test/test.bats
setup() {
load '/usr/lib/bats/bats-support/load'
load '/usr/lib/bats/bats-assert/load'
DIR=$(cd $(dirname $BATS_TEST_FILENAME) >/dev/null 2>&1 && pwd)
PATH="$DIR/../src:$PATH"
}
@test "can run our script" {
run project.sh
assert_output --partial 'Welcome to our project!'
}
ä»åºŠã¯ãã¹ãã«ãã¹ããŸãã
$ bats test/test.bats test.bats â can run our script 1 test, 0 failures
ãã®ä»
ã ãã¶é·ããªã£ãŠããã®ãšé°å²æ°ãããã£ãŠããã®ã§ãããããã«ããããšæããŸãããããšã¯ãããªå 容ã§ããã
- teardown颿°ã䜿ã
- skip颿°ã䜿ã£ãŠãã¹ããã¹ããããã
- å ±éã®ãã¹ããã«ããŒãäœæããŠload颿°ã§åŒã³åºã
- setup_file颿°ãšteardown_file颿°ã䜿ã
ãããã«
Bashã®ãã¹ãã£ã³ã°ãã¬ãŒã ã¯ãŒã¯ãBatsããã¥ãŒããªã¢ã«ã䜿ã£ãŠè©ŠããŠã¿ãŸããã
ã·ã§ã«ã¹ã¯ãªããã®ãã¹ããæžããããšããªãã£ãã®ã§ã¡ãã£ãšæžæããŸããããåºæ¬çãªããšã¯ããã£ãããªãšæããŸãã
ãã ä»åã®ããã«ãã¹ã察象ã®ã¹ã¯ãªããããšãŠãåçŽãªãããã®ã§ãããé·ãã¹ã¯ãªããã察象ã ãšãã¹ããæžãã®ã倧å€ã ãããªãš
æããŸããã·ã§ã«ã¹ã¯ãªããã¯ããªãã ããã èšã£ãŠå¯äœçšã®å¡ã«ãªããããã®ã§âŠã