#author("2020-02-14T14:21:26+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が_test.shとする。
- テスト対象の関数は、


** test_runner [#h7105899]

テスト実行のためのヘルパーである。~
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
}}}
* 参考リンク [#q5e03c7e]

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

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