tech blog, another one …

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:

1

2

3

# 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:

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:

1

2

3

4

[✔] '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:

1

2

3

4

[✔] '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. ;-)