2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Batsを使ってbashのUnitTestを行う

Last updated at Posted at 2022-01-21

はじめに

shellScriptコードで最低限の自動テストをやりたい人向けのメモ、カバレッジ分析等は対象外

ちなみにBatsとbats-coreが存在するが、現在は bats-core/bats-coreがメンテされている

検証環境

Ubuntu 21.04
bats-core 1.5.0
bats-support, bats-assert 0.3.0

インストール方法

bats-coreのインストール

$ git clone https://github.com/bats-core/bats-core.git
$ cd bats-core
$ ./install.sh /usr/local

bats-support, bats-assert のインストール
(ソースを置くだけだからpathは/usr/local/libじゃなくてもいい)

$ sudo git clone https://github.com/ztombol/bats-assert /usr/local/lib/bats-assert
$ sudo git clone https://github.com/ztombol/bats-support /usr/local/lib/bats-support

テストコード書き方

ファイル名は{name}.bats

test.bats
#!/usr/bin/env bats
setup() {
    load /usr/local/lib/bats-assert/load.bash
    load /usr/local/lib/bats-support/load.bash
    
    # ここに各テストケースの事前の処理を書く
}

teardown() {
    echo "finish test case"
    # ここに各テストケースの事後の処理を書く
}

#テストケース1
@test "test case1" {
    source remove.sh
    #resultフォルダのファイル数 == 0
    assert_equal "$(ls /home/user/result | wc -l)" 0
}
@test "test case2" {
    source remove.sh
    assert_equal "$(ls /home/user/result | wc -l)" 0
}

実行結果

テストファイルを実行

user@pcname:~$ bats test.bats 
  test case1
  test case2

2 tests, 0 failures

参考

https://bats-core.readthedocs.io/en/stable/tutorial.html
https://github.com/bats-core/bats-core#version-history

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?