Terminating test case after timeout

Last edited on

The Unix shell script below demonstrates a way to terminate the test case execution after a specified period of time. This is to cope with cases of e.g. a test script being stuck inside an endless loop.

#! /bin/sh

TIMEOUT=3
SQUISHRUNNER="<path_to_squish>/bin/squishrunner --testsuite <path_to_suite> --reportgen xml2.1,/tmp/results.xml"

# run subshell to watch and kill with TIMEOUT ### 
( sleep $TIMEOUT; echo "TIMEOUT AFTER $TIMEOUT seconds"; kill -KILL $$ 2>/dev/null ) &

$SQUISHRUNNER

# kill subshell if normal execution continues
# program will not reach here if subshell sleep times out
# $! refers to last process put into background - here the subshell
if [ -n "$result" ]; then echo "kill -HUP $!"; kill -HUP $!; fi
exit 0

Note that killing squishrunner like that will likely leave only partial results behind. I.e. the generated XML file might be missing closing tags or be completely empty.

A safer built-in --timeout switch for squishrunner is available since Squish 6.1 release.