test_helper 2.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
# source this file in a bash

XUNIT_TESTSUITE_START=0
XUNIT_START=0
XUNIT_TOTAL=0
XUNIT_FAILED=0
XUNIT_SUCCESS=0
XUNIT_TESTCASES_XML=""

## Call this at the start of a testcase. 
# \sa xUnit_fail()
# \sa xUnit_success()
xUnit_start() {
  XUNIT_START=$(date +%s.%N)
  if [ $XUNIT_TESTSUITE_START == 0 ]; then
    # very first call: start of a testsuite
    XUNIT_TESTSUITE_START=$XUNIT_START
  fi
}

## Call this after the testcase finished with a failure.
# \sa xUnit_success()
# \pre xUnit_start() must have been called before
# \param $1 classname
# \param $2 testcase name
guptar's avatar
guptar committed
26 27
# \param $3 testcase result
# \param $4 run index
28
xUnit_fail() {
guptar's avatar
guptar committed
29 30 31 32
  class=$1
  test_case=$2
  result=$3
  run_index=$4
33 34
  currtime=$(date +%s.%N)
  time=$(echo "$currtime - $XUNIT_START" | bc -l)
guptar's avatar
guptar committed
35
  xml="<testcase classname='$class' name='$test_case' run='$run_index' time='$time' RESULT='$result'></testcase>"
36
  XUNIT_TESTCASES_XML="$xml \n$XUNIT_TESTCASES_XML"
37 38 39 40
  XUNIT_FAILED=$((XUNIT_FAILED+1))
}

## Call this after the testcase finished successfully.
41
# \sa xUnit_success()
42 43 44
# \pre xUnit_start() must have been called before
# \param $1 classname
# \param $2 testcase name
guptar's avatar
guptar committed
45 46
# \param $3 testcase result
# \param $4 run index
47
xUnit_success() {
guptar's avatar
guptar committed
48 49 50 51
  class=$1
  test_case=$2
  result=$3
  run_index=$4
52 53
  currtime=$(date +%s.%N)
  time=$(echo "$currtime - $XUNIT_START" | bc -l)
guptar's avatar
guptar committed
54
  xml="<testcase classname='$class' name='$test_case' run='$run_index' time='$time' RESULT='$result'></testcase>"
55
  XUNIT_TESTCASES_XML="$xml \n$XUNIT_TESTCASES_XML"
56 57 58 59 60 61 62
  XUNIT_SUCCESS=$((XUNIT_SUCCESS+1))
}

## Call this after all testcases have been completed.
# This functions writes out the test report.
# \param $1 filename
xUnit_write() {
guptar's avatar
guptar committed
63
  filename=$1
64 65 66 67
  tests=$((XUNIT_FAILED+XUNIT_SUCCESS))
  timestamp=$(date --iso-8601=seconds)
  time=$(echo "$currtime - $XUNIT_TESTSUITE_START" | bc -l)
  xml_header="<testsuites><testsuite errors='0' failures='$XUNIT_FAILED' hostname='$(hostname)' name='OAI' skipped='0' tests='$tests' time='$time' timestamp='$timestamp'>"
guptar's avatar
guptar committed
68 69 70
  echo $xml_header > $filename
  echo -e $XUNIT_TESTCASES_XML >> $filename
  echo "</testsuite></testsuites>" >> $filename
71 72 73 74 75 76
  XUNIT_TESTSUITE_START=0
  XUNIT_START=0
  XUNIT_TOTAL=0
  XUNIT_FAILED=0
  XUNIT_SUCCESS=0
  XUNIT_TESTCASES_XML=""
guptar's avatar
guptar committed
77
}