靈感來自於這篇 stackoverflow 討論
不依賴任何框架,並且搭配 diff 及 bash 顏色編碼 就可以做出不錯的
function test_all_file()
{
for i in `seq ${test_num}`;
do
echo -e "\e[104m============ Test $i ============\e[49m"
# "-" read from standard input
# diff -q (short report) -w (ignore the last newline)
result=$(${exe} $(cat $1/test_$i.txt) | diff -q -w $2/ans_$i.txt -)
if [[ ${result} == "" ]];
then
echo -e "\e[92mTest $i Success!\e[39m"
else
echo -e "\e[,91mTest $i Failed!\e[39m"
fi
done
}
- 一個 bash 測試的框架
- 使用框架定義的關鍵字 (有點像 bash 但又不像 bash)
- 有幫你用 syntex highlight
- 只能使用 runtime variable 來從外面傳參數進去 相關討論
此篇討論 作者說有很多個test case,很多人想用 for-loop 去重複化程式碼,但是
@test
關鍵字相當於函數,包在 for-loop 內只是重複定義數次,且 bats 不是直譯器,頂多只能算關鍵字串處理的前處理器,所以仍需要把 case 分開寫
sudo apt install bats
run
指令相當於 Unix Commands,用 pipe時要注意,使用時
run bash -c ["有 pipe 的指令"]
[[ $output == ... ]]
- 可以無痛接軌 bash script
- 寫起來簡單
- 無法傳參數到 test function 內
sudo apt install shunit2
make ; make -i test