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.sh oneTimeSetUp() { : } oneTimeTearDown() { : } setUp() { : } tearDown() { : } # 测试用例 testMyFunction() { assertTrue "[ `myFunction 2` -eq 4 ]" } . "$SHUNIT2_HOME"/src/shunit2
#!/bin/sh # file: src/my_function.sh myFunction() { echo `expr $1 \* $1` }
原文参考: http://d.hatena.ne.jp/oknknic/20110823/1314113501