Quantcast
Channel: Bashタグが付けられた新着記事 - Qiita
Viewing all articles
Browse latest Browse all 2912

Batsを使ってbashのUnitTestを行う

$
0
0
はじめに 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

Viewing all articles
Browse latest Browse all 2912

Trending Articles