Introduction on how to use shunit2 with Travis-CI

Introduction

TDD or Test-Driven Development is the latest and greatest in recent software development history. And that's good for a reason.

But what has been missing for a long time are unit tests for our good old bash scripts. It's time for us to jump onto the bandwagon of tested software!

This post is one of a row of posts about testing bash scripts. In all posts i will use shunit2 as the testing framework and Travis-CI as the continius integration tool. In a later post i may show how to use shunit2 in conjunction with Jenkins as well.

For this small guide i assume that you already have a github account and some bash scripts you want to test as well as a free Travis-CI account for your github repositories. If not, follow this guide for intial setup.

TL;DR

For my restless readers just look at my example repository on github on how to set up Travis-CI. For all others, continue with the next steps.

Adding a random test

For the start let's say we just have an almost empty repository with just one bash script we want to test. At first we will create a subdirectory for our tests which is called tests.

In this directory we create a tests file which we call myscript_tests.sh whereas i assume that our bash script is called myscript.sh.

The basic skeleton you need in your myscript_tests.sh look as follows:

#!/usr/bin/env bash

# This is a test with one or more asserts
testSampleScriptParameters() {
  # Load sample_script.sh for testing
  . myscript.sh

  echo "Executing 3 Asserts..."

  # Add asserts here ...
}

# Execute shunit2 to run the tests
. shunit2-2.1.6/src/shunit2

Self testing your tests

If you have shunit2 installed on your machine (for example with homebrew on OSX) you can and should run your tests locally first before pushing it to your repository.

This way you can ensure, that only clean tested code is in your repo!

You can easily run the tests locally with the following command:

cd /path/to/your/repo
shunit2 tests/myscript_tests.sh

If you're using the unpacked version of shunit2 the second command should point to the unpacked "binary"

Adding the Travis-CI part

At first we need to configure Travis-CI for our builds. On the settings menu for our repository enable Build only if .travis.yml is present since we need the .travis.yml for our builds.

If you have done this create the .travis.yml file in the root of your repository with the following content:

language: bash

before_script:
    - curl -L "https://shunit2.googlecode.com/files/shunit2-2.1.6.tgz" | tar zx

script:
    - bash tests/myscript_tests.sh

In this file we define the language being tested and we add the shunit2 framework before we can test our script since Travis-CI does not support bash out of the box yet.

You can and should use newer shunit2 versions if available here but don't forget to change the version in the last line of each test file because they're hard-coded as well

At the last step we execute the actual test file in Travis-CI.

Already done?

Yes! You're done already setting up basic unit testing for your bash scripts using shunit2 and Travis-CI. Your build status should look like this now:

Build Status

Congratulations. In later posts i'll explain a bit more about asserts and will show how to mock things for more complex testing scenarios.

Happy testing!