moritetuのIT関連技術メモ

shUnit2

xUnit系テストツールのシェル版である。

インストール

特別な操作は不要。
ソースをダウンロードして、任意の場所に設置するだけである。
非常に簡単である。

テスト

特別な設定は不要であり、テストプログラムの中でshunit2プログラムをインクルードするだけである。

sample.sh

#! /bin/sh

testEquality() {
  assertEquals 1 1
}
# shunit2テストをインクルード、テストが実行される
. /path/to/shunit2

テストプログラム

補足

テストの実行

shunit2から実行

テストは、テストプログラム内からshunit2をロードする、または、shunit2にテスト対象ファイル名を渡すことで実行される。

$ shunit2 <a test file> [<func> [<func>...]]

sample_test.sh

#! /bin/sh

testEquality() {
    assertEquals 1 1
}

testTrue() {
    assertTrue "[ 1 -eq 1 ]"
}

実行

ファイルのテスト関数をすべて実行する。

$ shunit2 sample_test.sh
testEquality
testTrue

Ran 2 tests.

OK

ファイルの特定のテスト関数を実行する。

$ shunit2 sample_test.sh testTrue
testTrue

Ran 1 test.

OK

shunit2をインクルードして実行

hoge_test.sh

# hoge_test.sh
testFunc() {
  :
}

. /path/to/shunit2
# この場合、$0がテスト対象ファイルと見なされる(つまり、hoge_test.sh)

実行

$ bash hoge_test.sh

テストスイートの実行

test_runner

テスト実行のためのヘルパーである。
suffixが、_test.shであるテストファイル見つけてまとめて実行してくれる(テストスイートの実行である)。
特定のシェル環境で実行したり、指定がしなければデフォルトで複数のシェル環境でテストを実行してくれる。

$ ./test_runner -h
usage: test_runner [-e key=val ...] [-s shell(s)] [-t test(s)]

デフォルトのシェル環境は以下のとおり。

/bin/sh ash /bin/bash /bin/dash /bin/ksh /bin/pdksh /bin/zsh

サンプル test_runnerの実行例

shunit2の配下にあるテストでない場合は、shunit2のlibディレクトリの場所を指定すると流れる。
テスト対象は、テスト実行ディレクトリ($PWD)にある_test.shのファイルである。

$ tree ../shunit2
../shunit2
├── CODE_OF_CONDUCT.md
├── LICENSE
├── README.md
├── doc
│   ├── CHANGES-2.1.md
│   ├── RELEASE_NOTES-2.1.0.txt
│   ├── RELEASE_NOTES-2.1.1.txt
│   ├── RELEASE_NOTES-2.1.2.txt
│   ├── RELEASE_NOTES-2.1.3.txt
│   ├── RELEASE_NOTES-2.1.4.txt
│   ├── RELEASE_NOTES-2.1.5.txt
│   ├── RELEASE_NOTES-2.1.6.txt
│   ├── RELEASE_NOTES-2.1.7.md
│   ├── TODO.txt
│   ├── contributors.md
│   └── design_doc.txt
├── examples
│   ├── equality_test.sh
│   ├── lineno_test.sh
│   ├── math.inc
│   ├── math_test.sh
│   ├── mkdir_test.sh
│   ├── mock_file.sh
│   ├── mock_file_test.sh
│   ├── party_test.sh
│   └── suite_test.sh
├── lib
│   ├── shflags
│   └── versions
├── shunit2
├── shunit2_args_test.sh
├── shunit2_asserts_test.sh
├── shunit2_failures_test.sh
├── shunit2_macros_test.sh
├── shunit2_misc_test.sh
├── shunit2_standalone_test.sh
├── shunit2_test_helpers
└── test_runner

3 directories, 35 files
$ tree
.
└── hoge_test.sh

0 directories, 1 file
$ LIB_DIR=../shunit2/lib ../shunit2/test_runner
#------------------------------------------------------------------------------
# System data.
#

$ uname -mprsv
Linux 3.10.0-1062.9.1.el7.x86_64 #1 SMP Fri Dec 6 15:49:49 UTC 2019 x86_64 x86_64

OS Name: Linux
OS Version: CentOS Linux 7 (Core)

### Test run info.
shells: /bin/sh ash /bin/bash /bin/dash /bin/ksh /bin/pdksh /bin/zsh
tests: hoge_test.sh


#------------------------------------------------------------------------------
# Running the test suite with /bin/sh.
#
shell name: sh
shell version: GNU bash, バージョン 4.2.46(2)-release (x86_64-redhat-linux-gnu)

--- Executing the 'hoge' test suite. ---
testEquality

Ran 1 test.

OK


#------------------------------------------------------------------------------
# Running the test suite with ash.
#
runner:WARN unable to run tests with the ash shell


#------------------------------------------------------------------------------
# Running the test suite with /bin/bash.
#
shell name: bash
shell version: GNU bash, バージョン 4.2.46(2)-release (x86_64-redhat-linux-gnu)

--- Executing the 'hoge' test suite. ---
testEquality

Ran 1 test.

OK


#------------------------------------------------------------------------------
# Running the test suite with /bin/dash.
#
runner:WARN unable to run tests with the dash shell


#------------------------------------------------------------------------------
# Running the test suite with /bin/ksh.
#
runner:WARN unable to run tests with the ksh shell


#------------------------------------------------------------------------------
# Running the test suite with /bin/pdksh.
#
runner:WARN unable to run tests with the pdksh shell


#------------------------------------------------------------------------------
# Running the test suite with /bin/zsh.
#
runner:WARN unable to run tests with the zsh shell


実験テストプログラムがshunit2と別のディレクトリにある場合

test_runnerのラッパーを作成して実行してみる(run.sh)。

ディレクトリ構成

ディレクトリ構成は以下のとおり。
テストは、shunit2とは別のディレクトリにあるとする。

/home/guest/shunit2
 |- shunit2
 |- lib
 |- test_runner
 ...

/home/guest/mytests
  |- my_test.sh
  |- run.sh

/home/guest/mytests/run.sh

test_runnerを実行するためだけのラッパー。

#!/usr/bin/env bash

export SHUNIT2_ROOT=${SHUNIT2_ROOT:-/home/guest/shunit2}
export LIB_DIR="$SHUNIT2_ROOT/lib"
export SHUNIT_INC="$SHUNIT2_ROOT/shunit2"

"$SHUNIT2_ROOT"/test_runner "$@"

/home/guest/mytests/my_test.sh

テストプログラムは、shunit2を環境変数で変えられるように作成しておく。

#! /bin/sh

. "$SHUNIT2_ROOT"/shunit2_test_helpers

testEquality() {
  assertEquals 1 1
  assertEquals "$HOGE" "foo"
}

. "${TH_SHUNIT}"

以下では、シェルは/bin/sh、テスト対象はmy_test.sh、環境変数としてHOGE=fooを定義して実行している。

$ cd /home/guest/mytests
$ bash run.sh -s /bin/sh -t my_test.sh -e HOGE=foo

#------------------------------------------------------------------------------
# System data.
#

$ uname -mprsv
Linux 3.10.0-1062.9.1.el7.x86_64 #1 SMP Fri Dec 6 15:49:49 UTC 2019 x86_64 x86_64

OS Name: Linux
OS Version: CentOS Linux 7 (Core)

### Test run info.
shells: /bin/sh
tests: my_test.sh
HOGE=foo


#------------------------------------------------------------------------------
# Running the test suite with /bin/sh.
#
shell name: sh
shell version: GNU bash, バージョン 4.2.46(2)-release (x86_64-redhat-linux-gnu)

--- Executing the 'my' test suite. ---
testEquality

Ran 1 test.

OK

サンプル filetest_runner_sample.zip

参考リンク


トップ   一覧 単語検索 最終更新   ヘルプ   最終更新のRSS
目次
ダブルクリックで閉じるTOP | 閉じる
GO TO TOP