user1283776 user1283776 - 1 year ago 64
Bash Question

TravisCI build succeeds even when tests fail

This is where I am running my tests in


# Run tests
# Test application in Docker container
- ./tools/

The shell script
looks like this:

#!/usr/bin/env bash

githash="$(git rev-parse --short HEAD)"

echo "-------------------------------------------------"
echo "| Running unit tests |"
echo "-------------------------------------------------"

docker create -it --name test$githash
docker start test
docker exec test /bin/sh -c "go test ./..."
docker stop test
docker rm -fv test

The TravisCI build is a success even if the tests fail.

How can I get TravisCI to know if test have failed or not? I don't know if this is a problem with errors not being propagated from Docker, errors not being propagated from the shell script or TravisCI not knowing when go tests succeed or fail.

Answer Source

Your script is exiting with the status code of the last command docker rm -fv test.

You need to capture the status code of the test's, then clean up docker, then exit.

This code example is from a slightly different question over here but it's the same solution.

#!/usr/bin/env bash
set -e

# Set a default return code

# Cleanup
function cleanup {
  echo "Removing container"
  (docker stop test)
  (docker rm -f test)
  exit $RC
trap cleanup EXIT

# Test steps
docker create -it --name test path
docker start test
docker exec test /bin/sh -c "go test ./..."
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download