shunit2でテストコードを書いてみる
Google Code Archive - Long-term storage for Google Code Project Hosting.
インストール
$ mkdir shunit2 $ wget https://shunit2.googlecode.com/files/shunit2-2.1.6.tgz $ tar zxvf shunit2-2.1.6.tgz
./shunit2/shunit2-2.1.6
テストファイル
./shunit2/shunit2-2.1.6 ./shunit2/sample |--first.sh |--firstTest.sh |--test | |--hello | |--hoge
テスト対象のコード
first.sh
#!/bin/bash function helloWorld() { echo 'Hello World!' }
テストコード
firstTest.sh
#!/bin/bash # テストのセットアップ。標準出力をリダイレクトするファイルを用意する。今回のテストでは用意しなくても動く。 setup() { touch ./test/hello } # テスト結果の整理。リダイレクトしたファイルを削除する。 tearDown() { touch ./test/hoge } # テスト対象スクリプトを読み込む . first.sh # テスト関数。testを名称の初めにつける。 testHelloWorld() { # テスト対象を読んでリダイレクトする helloWorld > ./test/hello # 関数の戻りが0であることを確認する assertEquals $? 0 # 出力結果を変数に入れる actual=`cat ./test/hello` # 変数と期待値を比較する assertEquals "${actual}" 'Hello World!' } # shunit2を読み込む . ../shunit2-2.1.6/src/shunit2
テスト実行
$ ./firstTest.sh testHelloWorld Hello World! Ran 1 test. OK