assert.sh

:exclamation: Assertion lib for shell script users

View the Project on GitHub torokmark/assert.sh

assert.sh

Assert.sh is intended to give the assertion mechanism to shell scripts with well-known assert functions like assert_eq, assert_array_eq, or assert_empty. Inspired by Assert class of JUnit

Install & Usage

> # git clone https://github.com/torokmark/assert.sh.git; cd assert.sh
> # source assert.sh
> # assert_eq "hello" "world"
> # echo "$?"
# => 1

I. Clone the repository

git clone https://github.com/torokmark/assert.sh.git

Or copy the assert.sh where your project is located.

II. Edit the script where you would like to use asserts and paste the next line on the top:

source './assert.sh'

III. Now assert functions are available for use.

assert_eq "hello" "world"

0 return status is considered true and anything else is considered false.

List of assert functions

How to write tests

Example:

source "./assert.sh"

local expected actual
expected="Hello"
actual="World!"
assert_eq "$expected" "$actual" "not equivalent!"
# => x Hello == World :: not equivalent! 
source "./assert.sh"

local expected actual
expected="Hello"
actual="Hello"
assert_eq "$expected" "$actual"
if [ "$?" == 0 ]; then
  log_success "assert_eq returns 0 if two words are equal"
else
  log_failure "assert_eq should return 0"
fi

If the return status ($?) of assert_eq is equal to 0, which is considered true according to the convention. If the assert function returns 1, the expected and actual values are differred.