Bash + bats
方針
- Bash のサードパーティ コマンド
batsを使う
前提
bash
brew install bats
プロジェクト作成
bash
mkdir -p ~/workdir/SampleBash
code ~/workdir/SampleBash
最小のコード
sample.sh
#!/usr/bin/env bash
set -euo pipefail
add() {
local a="$1"
local b="$2"
echo "$((a + b))"
}
test/test_sample.bats
#!/usr/bin/env bats
setup() {
source "$BATS_TEST_DIRNAME/../sample.sh"
}
@test "add 2 and 3 => 5" {
run add 2 3
[ "${status}" -eq 0 ]
[ "${output}" -eq 5 ]
}
決まりごと
- ファイル名 :
*.bats - テストケース :
@test "<名前>" { .. }で定義したブロック - テストしたい箇所で test コマンド (
[ <条件> ]) を呼ぶ - bats ファイル自体のディレクトリは
$BATS_TEST_DIRNAMEで参照
実行
bash
bats -r test
ファイル名を指定でも良いが、test ディレクトリ以下を再帰的に実行する例とした
表示例
成功時
txt
test_sample.bats
✓ add 2 and 3 => 5
1 test, 0 failures
失敗時
txt
test_sample.bats
✗ add 2 and 3 => 5
(in test file test_sample.bats, line 10)
`[ "${output}" -eq 4 ]' failed
1 test, 1 failure
以下広告