shellscript自動化測試架構-shUnit2
shUnit2的開發背景
shUnit2是基於log4sh SHELL自動化測試對應而開發的, 使用方式和JUnit類似.
準備
1. 下載shUnit2
wget -P ~/download http://shunit2.googlecode.com/files/shunit2-2.1.6.tgz
2. 解壓
tar -xzvf ~/download/shunit2-2.1.6.tgz
3. 執行測試案例
~/download/shunit2-2.1.6/examples/equality_test.sh
測試執行的內部處理
shUnit2的內部處理流程如下所示.
shunit2執行時會列舉出所以有test開頭的測試函數oneTimeSetUp函數被定義的情況下, 就執行該函數被列舉的各個測試函數按如下方式執行:setUp函數被定義的情況下, 就執行該函數執行測試函數tearDown函數被定義的情況下, 就執行該函數oneTimeTearDown函數被定義的情況下, 就執行該函數
. "$SHUNIT2_HOME"/src/shell/shunit2
產生測試代碼
例:
#!/bin/sh# file: test/my_function_test.sh# 讀取測試對象. ../src/my_function.shoneTimeSetUp() {:}oneTimeTearDown() {:}setUp() {:}tearDown() {:}# 測試案例testMyFunction() { assertTrue "[ `myFunction 2` -eq 4 ]"}. "$SHUNIT2_HOME"/src/shunit2
#!/bin/sh# file: src/my_function.shmyFunction() { echo `expr $1 \* $1`}
原文參考: http://d.hatena.ne.jp/oknknic/20110823/1314113501