Unit testing in bash
I wrote a really simple unit test framework for bash. Like 10 minutes, 2 functions, totally half-baked. But it seems to work.
testErrors=""
function assertEqual {
expected="$1"
actual="$2"
message="${3:-Not Equal}"
if [ "$expected" == "$actual" ]; then
echo -n "."
else
echo -n "E"
testErrors="$testErrors\n$message: Got $actual, expected $expected"
fi
}
function assertSql {
expected="$1"
sql="$2"
actual=$(echo "$sql" | sqlite3 $db | sed -e 's/ *$//')
assertEqual "$expected" "$actual"
}
function testReport {
if [ -z "$testErrors" ]; then
echo "Tests OK!"
else
echo -e "Tests failed$testErrors"
fi
Comments
My friend Karl recommended cram as a way to test shell scripts. https://bitheap.org/cram/