- バックアップ一覧
- 差分 を表示
- 現在との差分 を表示
- ソース を表示
- baut へ行く。
- 1 (2020-02-12 (水) 01:24:51)
- 2 (2020-02-12 (水) 02:55:02)
- 3 (2020-02-14 (金) 21:06:11)
- 4 (2020-02-16 (日) 02:39:33)
baut †
Bashで書かれたテストツール。
batsのようにコマンドラインベースでプログラムの振る舞いをテストすることを目的としている。
テストプログラム自体もすべてBashであり、ツール特有のシンタックスはない。
XUnitのような感覚で使える。
インストール †
任意の場所にソース一式をダウンロードして、bin
ディレクトリにパスを通すだけである。
install.sh
を実行すれば同じことをしてくれる。
$ git clone https://github.com/moritetu/baut.git $ cd baut $ source install.sh $ baut run test
パスが通っていれば、-h
でUsageを確認できる。
$ baut -h Usage: baut [-v] [-h] [--d[0-4]] [run|<command>] [<args>] OPTIONS -v, --version Show version. -h, --help Show usage. --d[0-4] Set log level to TRACE(0), DEBUG(1), INFO(2), WARN(3), ERROR(4) COMMANDS compile Compile a test script file. init Generates test files from a template. report Format the result of 'baut test' execution. run Run tests. test Run tests in a file and print its result. Ordinally this command is called in 'run' command. Show more available information about a specific command. 'baut <command> [-h|--help]'
テスト †
テストプログラム †
- テストファイルは、拡張子が
*.sh
である。 - テストファイル名は、
test_
で始まる必要がある。 - テスト単位は、シェルの関数であり、
test_
で始まる関数はテスト対象である。
test_
で始まる関数名でなくともアノテーション@Test
が付与されている関数はテスト対象となる。 #:
は特別な意味を持ち、#:
の後には@アノテーション
などを記述する。- ファイル内のテストは上から順に実行される。
#!/usr/bin/env bash #: @BeforeAll function setup_all() { : # 本ファイルのすべてのテスト実行前に一度呼ばれる } #: @BeforeEach function setup() { : # 各テスト前に一度呼ばれる } #: @Test(テストの内容について記述できる) test_ng_sample() { fail "Not implemented" } #: @Test test_ng_sample2() { run echo "bar" [ $status -ne 0 ] || fail "exit status should not be 0, but '$status'" "result: $result" } #: @Test test_ok_sample() { run echo "hello baut" [ "$result" = "hello baut" ] [ $status -eq 0 ] } #: @Test test_skip_sample() { run echo "hello baut" skip "Good bye!" echo "Not reach here" } #: @Test test_wait_until() { local pidfile="$(__DIR__)/sample.pid" eval "sleep 2 && echo $BASHPID > $pidfile" & wait_until --retry-max 3 "[ -e '$pidfile' ]" rm $pidfile ||: } #: @AfterEach function teardown() { : # 各テストの実行後に呼ばれる } #: @AfterAll function after_all() { : # 本ファイルのすべてのテスト実行後に呼ばれる }
実行すると以下のようになる。
$ baut r test_sample.sh 1 file, 5 tests #1 /tmp/test_sample.sh x テストの内容について記述できる Not implemented # Error(1) detected at the following: # 13 #: @Test(テストの内容について記述できる) # 14 test_ng_sample() { #=> 15 fail "Not implemented" # 16 } # 17 x test_ng_sample2 exit status should not be 0, but '0' result: bar # Error(1) detected at the following: # 19 test_ng_sample2() { # 20 run echo "bar" #=> 21 [ $status -ne 0 ] || fail "exit status should not be 0, but '$status'" "result: $result" # 22 } # 23 o test_ok_sample ~ test_skip_sample # SKIP Good bye! o test_wait_until #$ 5 tests, 2 ok, 2 failed, 1 skipped 💥 1 file, 5 tests, 2 ok, 2 failed, 1 skipped Time: 0 hour, 0 minute, 3 seconds
テストスイートの実行 †
ディレクトリを指定して実行すれば、ディレクトリ下のテストを順に実行する。
テストファイルの実行順序は決まっていない。
$ baut run testdir
2階層以上にもテストファイルが存在する場合は、-r
をつける。
$ baut run -r testdir
コマンド †
run †
後に続くコマンドを実行し、結果を変数に格納する。
変数 | 説明 |
---|---|
result | stdoutとstderrの結果 |
status | コマンドの終了ステータス |
lines | stdoutとstderrの結果の行単位の配列 |
run2 †
後に続くコマンドを実行し、結果を変数に格納する。
runと違うのは、stdoutとstderrを別々に扱うこと。
変数 | 説明 |
---|---|
result | stdoutの結果 |
status | コマンドの終了ステータス |
lines | stdoutの結果の行単位の配列、stdout_linesと同じ |
stdout | stdoutの結果 |
stderr | stderrの結果 |
stdout_lines | stdoutの結果の行単位の配列 |
stderr_lines | stderrの結果の行単位の配列 |
#: @BeforeAll function setup_all() { : } #: @BeforeEach function setup() { : } #: @Test test_ng_sample() { fail "Not implemented" } #: @Test test_ng_sample2() { run echo "bar" [ $status -ne 0 ] || fail "exit status should not be 0, but '$status'" "result: $result" } #: @Test test_ok_sample() { run echo "hello baut" [ "$result" = "hello baut" ] [ $status -eq 0 ] } #: @Test test_skip_sample() { run echo "hello baut" skip "Good bye!" echo "Not reach here" } #: @Test test_wait_until() { local pidfile="$(__DIR__)/sample.pid" eval "sleep 2 && echo $BASHPID > $pidfile" & wait_until --retry-max 3 "[ -e '$pidfile' ]" rm $pidfile ||: } #: @AfterEach function teardown() { : } #: @AfterAll function after_all() { : }
eval2 †
run2と同じ、リダイレクトやパイプなどを含むコマンドを実行する。
wait_until †
実行に時間のかかるコマンドを待ち合わせる。
baut r -d test_sample.sh [1] /test/test_sample.sh ├─ (1) before_all_functions => setup_all ├─ (1) before_each_functions => setup ├─ (5) test_functions => test_ng_sample test_ng_sample2 test_ok_sample test_skip_sample test_wait_until ├─ (1) after_each_functions => teardown └─ (1) after_all_functions => after_all
stop †
テストプロセスを停止する。
skip †
テストをスキップする。
fail †
テストを失敗させる。
$ cat test_match.sh #: @Test param_test1() { test 1 -eq 1 } #: @Test param_test2() { test 1 -eq 1 } #: @Test command_test1() { run echo "hoge" [ "$stdout" = "hoge" ] } #: @Test command_test2() { run echo "foo" [ "$stdout" = "foo" ] }