Skip to content

lovelessless99/Bash_Test_Tool

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Bash 自動化測試的工具

靈感來自於這篇 stackoverflow 討論

1. 自己動手寫 shell script

不依賴任何框架,並且搭配 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
}

2. bats

Github link Tutorial link 參考文章

  • 一個 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 == ... ]]

3. shunit2

Github Link 參考教學

  • 可以無痛接軌 bash script
  • 寫起來簡單
  • 無法傳參數到 test function 內
sudo apt install shunit2

專案執行

make ; make -i test

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published