Skip to content
Tom edited this page Aug 12, 2019 · 2 revisions

Welcome to the shUnit wiki!

shUnit is a simple script for testing you shell code. To get started clone this repository and add both shUnit and assert.sh to your path.

To verify if you did it correctly execute the following shUnit -v If the above doesn't work than you haven't installed shUnit in your path.

To get started make a simple directory called test or something other that you like. Next cd into the directory and make these files

#!/bin/bash
# Filename: app.sh
if [[ "$1" == "--echo" ]]; then
   echo "$1"
fi

The file above is a simple script to echo something to the screen. Lets test if it works

# app test echo
# Filename: echo.shUnit

function Test_echo_hello {
   AssertEquals "hello" "$(./app.sh --echo hello)" 
}

function Test_echo_hi {
   AssertEquals "hi" "$(./app.sh --echo hi)" 
}

function Test_echo_empty {
   AssertEquals "" "$(./app.sh --echo)" 
}

function Test_echo_multiargument {
   AssertEquals "hello hi" "$(./app.sh --echo hello hi)" 
}

When running the above unit test you should see something like this

Running unit: app.shUnit
 app test echo
 Running test: Test_echo
PASS: hello and hello are equal
 Running test: Test_echo_hi
PASS: hi and hi are equal
 Running test: Test_echo_empty
PASS:  and  are equal
 Running test: Test_echo_multiargument
ERROR: expected hello hi, but got hello instead
--------------------------------------------------------
Pass: 3, Errors: 1

As you can see the --echo option only listens to the next argument. Lets quickly fix our app

#!/bin/bash
# Filename: app.sh
if [[ "$1" == "--echo" ]]; then
   echo "${@:2}"
fi

And run our tests again. Now you should see the following

 Running unit: app.shUnit
 app test echo
 Running test: Test_echo
PASS: hello and hello are equal
 Running test: Test_echo_hi
PASS: hi and hi are equal
 Running test: Test_echo_empty
PASS:  and  are equal
 Running test: Test_echo_multiargument
PASS: hello hi and hello hi are equal
--------------------------------------------------------
Pass: 4, Errors: 0
Clone this wiki locally