35

Background:

I have been using Travis CI for my PHP projects and I really like how they give you a link to a picture that shows the status of the current build of your project.

I am currently making a lot of tools using UNIX shell scripting and would like to use Travis CI to test my UNIX scripts.

I have searched the internet trying to find out how to achieve this. I went to the main website, searched Stackoverflow as well as did a bit of Google searching.

It seems like this isn't possible.

I currently use shunit2 to test my shell scripts and functions.

My question(s) is/are:

  1. Is it possible to use Travis CI to test shell scripts?
  2. If not are there any alternatives that I could use that plug into GitHub?
  3. What is the best way to perform integration testing on shell scripts?
Alex Harvey
  • 14,494
  • 5
  • 61
  • 97
Dodzi Dzakuma
  • 1,406
  • 2
  • 21
  • 39

3 Answers3

44

Absolutely.

I made a simple test here: https://travis-ci.org/soulseekah/test-shunit2-travis

My .travis.yml file is:

language: bash

before_script:
    - curl -L "https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/shunit2/shunit2-2.1.6.tgz" | tar zx

script:
    - bash equality_test.sh

Repository: https://github.com/soulseekah/test-shunit2-travis

Bat
  • 771
  • 11
  • 29
soulseekah
  • 8,770
  • 3
  • 53
  • 58
  • Thank you very much. This is just what I was looking for. The Travis CI website is really hard to navigate. I had an extra question. I mainly make include files and I want the files to run the tests when the include is run as a script, but to avoid running tests when they are sourced. What is your method for doing this? I current use something like `scriptName=$(basename $_); if [[ "$scriptName" = "$0" ]]`. I apologize for not having the actual code on me at the moment. Will update a little later. – Dodzi Dzakuma Dec 08 '13 at 07:52
  • If I understood your question correctly, I believe you can simply check for one of the constants that are defined when shunit is running: http://shunit2.googlecode.com/svn/trunk/source/2.1/doc/shunit2.html#some-constants-you-can-use – soulseekah Dec 08 '13 at 07:56
  • I guess I was looking for an answer more like this http://stackoverflow.com/questions/2683279/how-to-detect-if-a-script-is-being-sourced , but just as the comments said `$_ and $0` are very brittle. I wanted to know if you had a special way of dealing with this. The reason being is, I want to test and validate all my scripts, but since they are includes, I don't want the tests to be run when the scripts are sourced. – Dodzi Dzakuma Dec 08 '13 at 16:00
  • But, does this script always exit 0 ? If so, Travis-CI will always think your tests have passed even when some fail. Having looked through the shunit2 docs, I can't see a way to detect how many (or if any) of the tests failed. – Lqueryvg May 05 '16 at 19:58
  • FYI the way I get a global exit status is to add "[[ $? == 1 ]] && exitStatus=1" in every function after every assert, where exitStatus is a global variable I set to 0 at the top of the script, and then I exit the script with "exit $exitStatus" at the end. – Lqueryvg May 05 '16 at 20:43
12

I rolled everything into a Makefile and then call make test...

language: bash
script: make test

I'm using assert.sh, so there was no need for the before_script. You can check it out at https://github.com/wmluke/dokku-domains-plugin.

wmluke
  • 309
  • 1
  • 6
2

If you need the latest version of shUnit2, you may need to get it from the master branch.

I got it working like this:

---
language: bash
before_script: "sudo curl -o /usr/local/bin/shunit2 https://raw.githubusercontent.com/kward/shunit2/master/shunit2"
script: "bash shunit2/test_example.sh"

See also my example shunit2 repo here.

Alex Harvey
  • 14,494
  • 5
  • 61
  • 97