Tbpgr Blog

Employee Experience Engineer tbpgr(てぃーびー) のブログ

shunit2でシェルもテストファースト

概要

シェル用テストフレームワークのshunit2の導入について。

インストール手順

・以下のURLのダウンロードリンクからshunit2を取得する
※2012/02/24時点での最新版2.1.3を選びました
http://sourceforge.jp/projects/sfnet_shunit2/

・該当ファイルを解凍する

・クラスパスの設定を行う
$SHUNIT2_HOME=shunit2-2.1.3を解凍したディレクトリのパス

例:cドライブ直下に解凍した場合
$SHUNIT2_HOME=/cygdrive/c/shunit2-2.1.3

・PATHの設定を行う
%SHUNIT2_HOME%/src/shell

サンプルを作成し動作確認

#!/bin/sh

setUp() {
  echo "setup"
}
tearDown() {
  echo "teardown"
}

# テストケース成功
test_success()
{
  assertEquals 1 1
}

# テストケース失敗
test_fail()
{
  assertEquals 1 0
}

# shunit2のロード
. shunit2

出力

$./shunit_test.sh
#
# Performing tests
#
▼setup
test_success
▲teardown
▼setup
test_fail
ASSERT:expected:<1> but was:<0>
▲teardown

#
# Test report
#
tests passed:     1  50%
tests failed:     1  50%
tests skipped:    0   0%
tests total:      2 100%