#author("2020-02-14T20:32:11+09:00","default:haikikyou","haikikyou")
[[moritetuのIT関連技術メモ]]

#contents


* shUnit2 [#o3f32c01]

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

* インストール [#wecdc957]

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


* テスト [#z34b7c96]

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

&label(sample){例};'' sample.sh''

#geshi(bash){{{
#! /bin/sh

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


** テストプログラム [#effb8d19]

- テストファイルは、suffixが&code(){_test.sh};である。
- テスト対象の関数は、&code(){test};を接頭辞に持つものである。~
#geshi(bash){{{
# テスト対象
testVarShouldNotbeEmpty() {
    assertNotNull '$var is not empty' "$msg"
}
# テスト対象でない
hoge() {
    assertNotNull '$var is not empty' "$msg"
}
}}}
- テストは、ファイル内の上から定義されている順に実行される。~



&label(info){補足};
- テスト関数は&code(){eval};で評価される。
- &code(){egrep};で以下の条件にマッチする関数名がテスト対象である。
#geshi{{{
^\s*((function test[A-Za-z0-9_-]*)|(test[A-Za-z0-9_-]* *\(\)))
}}}
** テストの実行 [#v1a0f600]

*** shunit2から実行 [#cb7b2e5b]

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

#geshi(bash){{{
$ shunit2 <a test file> [<func> [<func>...]]
}}}

''sample_test.sh''

#geshi(bash){{{
#! /bin/sh

testEquality() {
    assertEquals 1 1
}

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

''実行''

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

#geshi(bash){{{
$ shunit2 sample_test.sh
testEquality
testTrue

Ran 2 tests.

OK
}}}

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

#geshi(bash){{{
$ shunit2 sample_test.sh testTrue
testTrue

Ran 1 test.

OK
}}}


*** shunit2をインクルードして実行 [#d8c8ee6e]

''hoge_test.sh''
#geshi(bash){{{
# hoge_test.sh
testFunc() {
  :
}

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

''実行''

#geshi(bash){{{
$ bash hoge_test.sh
}}}


** テストスイートの実行 [#q74c6d92]
*** test_runner [#i0625066]

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

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

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

#geshi{{{
/bin/sh ash /bin/bash /bin/dash /bin/ksh /bin/pdksh /bin/zsh
}}}

&label(sample){サンプル}; ''test_runnerの実行例''

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

#geshi(bash){{{
$ 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
}}}
~

&label(study){実験};''テストプログラムがshunit2と別のディレクトリにある場合''

&code(){test_runner};のラッパーを作成して実行してみる(&code(){run.sh};)。~


''ディレクトリ構成''

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

#geshi{{{
/home/guest/shunit2
 |- shunit2
 |- lib
 |- test_runner
 ...

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

''/home/guest/mytests/run.sh''

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

#geshi(bash){{{
#!/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を環境変数で変えられるように作成しておく。

#geshi(bash){{{
#! /bin/sh

. "$SHUNIT2_ROOT"/shunit2_test_helpers

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

. "${TH_SHUNIT}"
}}}


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

#geshi(bash){{{
$ 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
}}}

&label(sample){サンプル}; &ref(./test_runner_sample.zip);
* 参考リンク [#q5e03c7e]

- https://github.com/kward/shunit2
- https://sites.google.com/site/paclearner/shunit2-documentation

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