Run Unit Tests from Shell

For a certain reason I have a bunch of binaries which are in fact unit tests. Each of them returns 0 as exit code in case of success and some other value if it fails. I wanted a lightweight solution to run all these tests and printing out the result. What I came up with is a ~50 line shell script which reads the list of tests from a simple text file. In this text file all you have to put in is one command per line, which gets executed by the script then. First see a sample list of tests:

# line will be ignored
uname -a
/bin/false

The actual script running the tests, I named it run-tests.sh for obvious reasons, looks like this:

#!/bin/bash                                                                
# ----------------------------------------------------------------------
# Run tests from a simple text file with a command on each line.           
#                                                                          
# Copyright 2017 Alexander Dahl
# ----------------------------------------------------------------------
                                                                           
. /usr/local/share/colsh/colsh_definitions.inc                             
. /usr/local/share/colsh/colsh_cecho.inc                                   
                                                                           
TESTS_GOOD=0                                                               
TESTS_BAD=0                                                                
                                                                           
if [ -z "${1}" ]                                                           
then                                                                       
    echo "Usage: ${0} LIST"                                                
    exit 1                                                                 
fi                                                                         
                                                                           
if [ ! -f "${1}" ]                                                         
then                                                                       
    cecho --err "Test list file '${1}' does not exist!"                    
    exit 1                                                                 
fi                                                                         
                                                                           
while read line                                                            
do                                                                         
    # skip lines starting with '#'                                         
    if [ -z "${line%%#*}" ]                                                
    then                                                                   
        continue                                                           
    fi                                                                     
                                                                           
    eval ${line} >/dev/null 2>&1                                           
    if [ $? -eq 0 ]                                                        
    then                                                                   
        TESTS_GOOD=$(expr ${TESTS_GOOD} + 1)                               
        echo -e "[${COL_INFO}✔${COL_RST}] '${line}'"                       
    else                                                                   
        TESTS_BAD=$(expr ${TESTS_BAD} + 1)                                 
        echo -e "[${COL_ERR}✘${COL_RST}] '${line}'"                        
    fi                                                                     
done < "${1}"                                                              
echo                                                                       
                                                                           
TESTS_SUM=$(expr ${TESTS_GOOD} + ${TESTS_BAD})                             
                                                                           
if [ ${TESTS_BAD} -gt 0 ]                                                  
then                                                                       
    cecho --warn "${TESTS_BAD} of ${TESTS_SUM} tests failed!"              
    exit 1                                                                 
else                                                                       
    cecho --info "${TESTS_GOOD} of ${TESTS_SUM} tests successful."         
fi                                                                         

Note the inclusion of two files from my own project colsh at the top for nice colors. The sample output below is not colored, so you have to try the script by yourself to see it. The script runs fine with bash and also with busybox, which was my intended target. Now see some sample output:

[✔] 'uname -a'
[✔] '/bin/true'

2 of 2 tests successful.

See the script showing you if the test was successful and the original command. In case of failing tests you can just copy the command from the failed line, paste it in another shell and execute it to see its output or otherwise debug it. Note my script suppresses the output of the called commands to not clutter its own output. Another output, this time with a failed test:

[✔] 'uname -a'
[✘] '/bin/false'

1 of 2 tests failed!

You probably noticed the marks in the brackets are unicode, your environment should be set to some UTF-8 locale. ;-)

Leave a Reply

Your email address will not be published. Required fields are marked *