说明
演示用bats测试框架测试shell脚本
目录结构
目录内容
src/
project.sh
...
test/
bats/ <- submodule
test_helper/
bats-support/ <- submodule
bats-assert/ <- submodule
test.bats
...
搭建目录结构
从github下载bats和bats-support
mkdir myproj
cd myproj
git clone https://github.com/bats-core/bats-core.git test/bats
git clone https://github.com/bats-core/bats-support.git test/test_helper/bats-support
git clone https://github.com/bats-core/bats-assert.git test/test_helper/bats-assert
mkdir src
编辑源代码project.sh
cd src
vi project.sh
[nome@LIYUEFU-T14 src]$ cat project.sh
#!/usr/bin/env bash
#
# echo "Welcome to our project!"
# echo "NOT IMPLEMENTED!" >&2
# exit 1
FIRST_RUN_FILE=/tmp/bats-tutorial-project-ran
if [[ ! -e "$FIRST_RUN_FILE" ]]; then
echo "Welcome to our project!"
touch $FIRST_RUN_FILE
fi
echo "NOT IMPLEMENTED" >&2
exit 1
编辑测试代码
cd test
vi tests.bat
[nome@LIYUEFU-T14 test]$ cat tests.bat
setup() {
load 'test_helper/bats-support/load'
load 'test_helper/bats-assert/load'
# get the containing directory of this file
# use $BATS_TEST_FILENAME instead of ${BASH_SOURCE[0]} or $0,
# as those will point to the bats executable's location or the preprocessed file respectively
DIR="$( cd "$( dirname "$BATS_TEST_FILENAME" )" >/dev/null 2>&1 && pwd )"
# make executables in src/ visible to PATH
PATH="$DIR/../src:$PATH"
}
@test "Show welcome message on first invocation" {
if [[ -e /tmp/bats-tutorial-project-ran ]]; then
skip 'The FIRST_RUN_FILE always exists'
fi
run project.sh
assert_output -p 'Welcome to our project!'
run project.sh
refute_output -p 'Welcome to our project!'
}
teardown() {
# : # Look Ma! No cleanup!
rm -f /tmp/bats-tutorial-project-ran
}
[nome@LIYUEFU-T14 test]$
安装bats到/usr/local
需要以root安装
[nome@LIYUEFU-T14 test]$ ls
bats test_helper tests.bat
[nome@LIYUEFU-T14 test]$ cd bats
[nome@LIYUEFU-T14 bats]$ ls
AUTHORS docker Dockerfile lib man SECURITY.md uninstall.sh
bin docker-compose.override.dist docs libexec package.json shellcheck.sh
contrib docker-compose.yml install.sh LICENSE.md README.md test
[nome@LIYUEFU-T14 bats]$ sudo su
[sudo] password for nome:
[root@LIYUEFU-T14 bats]# pwd
/mnt/d/workdir/dev/shell/myproj/test/bats
[root@LIYUEFU-T14 bats]# ./install.sh /usr/local
Installed Bats to /usr/local/bin/bats
[root@LIYUEFU-T14 bats]#
执行测试
[root@LIYUEFU-T14 test]# pwd
/mnt/d/workdir/dev/shell/myproj/test
[root@LIYUEFU-T14 test]# bats tests.bat
tests.bat
✓ Show welcome message on first invocation
1 test, 0 failures
[root@LIYUEFU-T14 test]#
更多assert命令
https://github.com/bats-core/bats-assert/blob/master/README.md
assert / refute Assert a given expression evaluates to true or false.
assert_equal Assert two parameters are equal.
assert_not_equal Assert two parameters are not equal.
assert_success / assert_failure Assert exit status is 0 or 1.
assert_output / refute_output Assert output does (or does not) contain given content.
assert_line / refute_line Assert a specific line of output does (or does not) contain given content.
assert_regex / refute_regex Assert a parameter does (or does not) match given pattern.