shUnit2 - Introduction
Introduction to shUnit2
Unit tests for Bash with shUnit2
shUnit2 is a xUnit unit test framework for Bourne based shell scripts, and it is designed to work in a similar manner to JUnit, PyUnit, etc..
Setup
Install
1
2
3
4# Manjaro
yum -y install shunit2
# You may also just download shunit2 and make sure it's on your path.Quickstart
1
2
3
4
5
6
7
8
9
10
# file: examples/party_test.sh
testEquality()
{
assertEquals 1 1
}
# load shunit2
. /usr/share/shunit2/shunit2
Tests structure
- 3 sections:
- set up section
- setUp and tearDown functions
- define mocks
- test cases section
- call to the shunit2 test runner (
. shunit2.
)
- set up section
Example
File to test
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
cidr="$1"
usage() {
[ ! -z "$1" ] && echo $1
cat <<EOF
Print all IPs in a CIDR range, similar to the Ubuntu prips utility.
This script assumes that the Red Hat version of ipcalc is available.
Usage: $0 <cidr> [-h]
Example: $0 192.168.0.3/28
EOF
exit 1
}
[ -h == "$1" ] && usage
[ ! -z "$2" ] && usage 'You may only pass one CIDR'
[ -z "$cidr" ] && usage 'You must pass a CIDR'
echo $cidr | egrep -q "^(?:[0-9]+\.){3}[0-9]+/[0-9]+$" || \
usage "$cidr is not a valid CIDR"
# range is bounded by network (-n) & broadcast (-b) addresses.
lo=$(ipcalc -n $cidr | cut -f2 -d=)
hi=$(ipcalc -b $cidr | cut -f2 -d=)
IFS=. read a b c d <<< "$lo"
IFS=. read e f g h <<< "$hi"
eval "echo {$a..$e}.{$b..$f}.{$c..$g}.{$d..$h}"The test cases (White-box testing)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# Mock the output of the ipcalc.
ipcalc() {
case "$*" in
"-n 192.168.0.2/28")
echo NETWORK=192.168.0.0
;;
"-b 192.168.0.2/28")
echo BROADCAST=192.168.0.15
;;
"-n 10.45.0.0/16")
echo NETWORK=10.45.0.0
;;
"-b 10.45.0.0/16")
echo BROADCAST=10.45.255.255
esac
}
# test cases
test_minus_h() {
first_line=$(. ./prips.sh -h | head -1)
assertEquals "Print all IPs in a CIDR range, similar to the Ubuntu \
prips utility." "$first_line"
}
test_missing_args() {
first_line=$(. ./prips.sh | head -1)
assertEquals 'You must pass a CIDR' "$first_line"
}
test_too_many_args() {
first_line=$(. ./prips.sh 192.168.0.2/28 192.168.0.2/30 | head -1)
assertEquals 'You may only pass one CIDR' "$first_line"
}
test_bad_input() {
first_line=$(. ./prips.sh bad_input | head -1)
assertEquals 'bad_input is not a valid CIDR' "$first_line"
}
test_a_little_cidr() {
response=$(. ./prips.sh 192.168.0.2/28)
expected="192.168.0.0 192.168.0.1 192.168.0.2 192.168.0.3 192.168.0.4 \
192.168.0.5 192.168.0.6 192.168.0.7 192.168.0.8 192.168.0.9 192.168.0.10 \
192.168.0.11 192.168.0.12 192.168.0.13 192.168.0.14 192.168.0.15"
assertEquals "$expected" "$response"
}
test_a_big_cidr() {
number_of_ips=$(. ./prips.sh 10.45.0.0/16 | wc -w)
assertEquals 65536 "$number_of_ips"
}
. shunit2Run tests
1
bash examples/test_prips.sh