Testing shell scripts

Shell scripts serve an import role in a lot of cases, sadly mostly they are also the mother of all legacy pre. Don’t get me wrong, those one-off scripts are important and great, but after they serve there purpose there are really 2 options,ensure that they are well maintained, or get rid of them. Just leaving them around will just make them fight you later on.

This to me means that shell scripts need to be tested like any other software, and even though it does not seem obvious, testing shell scripts is quite easy. My personal tool of choice is assert.sh, which is simple enough and integrates nicely into a testing workflow.

Just a quick example on how this works, the script to test is example.sh

the tests go in example.test.sh

and finally running everything is as easy as

And that’s it, a simple test harness around shell scripts.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.