moritetuのIT関連技術メモ

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]'
 

テスト

テストプログラム

#!/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

後に続くコマンドを実行し、結果を変数に格納する。

変数説明
resultstdoutとstderrの結果
statusコマンドの終了ステータス
linesstdoutとstderrの結果の行単位の配列

run2

後に続くコマンドを実行し、結果を変数に格納する。
runと違うのは、stdoutとstderrを別々に扱うこと。

変数説明
resultstdoutの結果
statusコマンドの終了ステータス
linesstdoutの結果の行単位の配列、stdout_linesと同じ
stdoutstdoutの結果
stderrstderrの結果
stdout_linesstdoutの結果の行単位の配列
stderr_linesstderrの結果の行単位の配列
#: @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" ]
}

参考リンク


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